php 发送post请求且header中带参数bug调试

发布时间:2020-08-09 21:40:39 作者:jinjiang2009
来源:网络 阅读:1128

  通常get方式header中带参数如下通过curl调用即可:

    function send_get_curl_header($url, $data){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $data);
    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
}

  注意$data格式为数组['AppToken: '.$token],而post发送时要注意postData要进行http_build_query才能成功代码如下:

function send_post_curl_header($url, $header_data , $post_data){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header_data);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
    $sResult = curl_exec($ch);
    if($sError=curl_error($ch)){
        die($sError);
    }
    curl_close($ch);
    return $sResult;
}

  大家注意,此处花费了我一定的时间去调试。

推荐阅读:
  1. php服务端响应post请求的方法
  2. php利用socket发送GET,POST请求的案例分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php post get

上一篇:bitShark对Android版本的支持

下一篇:MongoDB升级--从3.4到3.6

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》