• 欢迎访问小杰博客网站
  • 欢迎访问小杰博客网站哦

php数据POST提交到API接口

未分类 小杰 8年前 (2015-04-09) 1371次浏览 已收录 0个评论

/**
* php数据POST提交到API接口
*/
static public function post_to_heinz($url_head,$url_param)
{
   $remote_url_address = ‘http://xxxxxx:8000/’.$url_head;
   ClsTools::logRec(‘$remote_url_address:’);
ClsTools::logRec($remote_url_address);
ClsTools::logRec(‘$url_param:’);
ClsTools::logRec($url_param);
$url_param = http_build_query($url_param);
ClsTools::logRec($url_param);

// die();
$result = array();
try {
$context = array(
       ‘http’ => array(
           ‘method’ => ‘POST’,
           ‘header’ => ‘Content-type: application/x-www-form-urlencoded;charset=UTF-8’,
           ‘content’ => $url_param)
       );
   $stream_context = stream_context_create($context);

   $result[‘data’] = file_get_contents($remote_url_address, false, $stream_context);
   if(empty($result[‘data’])) {
    $result[‘error’] = ‘连接接口失败’;
   }
} catch(Exception $e) {
$result[‘error’] = ‘file_get_contents exception:’.$e->getMessage();
}

    ClsTools::logRec(‘post_result:’);
   ClsTools::logRec($result);
   return $result;


小杰博客 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php数据POST提交到API接口
喜欢 (0)
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址