php curl 设置请求头headers和请求体body - Go语言中文社区

php curl 设置请求头headers和请求体body


啥也不说,直接上代码。

我这里是post请求。


$url = "http://www.example.com";
//headers数组内的格式
$headers = array();
$headers[] = "app-id:xxxxx";
$headers[] = "Content-Type:application/json";
$body   = array(
            "username" => "username",
            "password" => "password"
       );
$postBody    = json_encode($body);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);//设置请求头
curl_setopt($curl, CURLOPT_POSTFIELDS, $postBody);//设置请求体
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');//使用一个自定义的请求信息来代替"GET"或"HEAD"作为HTTP请求。(这个加不加没啥影响)
$data = curl_exec($curl);
echo $data;

 

转载于:https://my.oschina.net/u/3714085/blog/1573924

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/weixin_34220834/article/details/92402744
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-06-06 10:05:03
  • 阅读 ( 2198 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢