duanpu1111
duanpu1111
2016-12-30 13:04
浏览 195
已采纳

PHP使用带标头的cURL和GET请求

There are slimier questions in the past like below.

How do I send a GET request with a header from PHP?

But I don't know why my code is not working. I want to get "status code 200 OK and image data in binary" by using cURL and GET request with a header.

I may make mistake on debugging too. I would appreciate your any help. Thanks in advance!

API refrence: https://devdocs.line.me/en/#get-content

$url = "https://api.line.me/v2/bot/message/". $message_id. "/content";
$curl = curl_init("$url");
error_log(var_export($curl));

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 証明書の検証を行わない
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
    'Authorization: Bearer ' . $accessToken,
));

$response = curl_exec($curl);
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$image_binary = substr($response, $header_size);
curl_close($curl);

error_log(print_r("xxx...",true));
error_log(var_export($response));
error_log(print_r("aaa...",true));
error_log(print_r($response,true));
error_log(print_r("bbb...",true));
error_log(print_r($header,true));
error_log(print_r("ccc...",true));
error_log(print_r($image_binary,true));

Then.. I get this...

2017-01-01T01:04:48.272544+00:00 app[web.1]: [01-Jan-2017 10:04:48 Asia/Tokyo] 
2017-01-01T01:04:48.911005+00:00 app[web.1]: [01-Jan-2017 10:04:48 Asia/Tokyo] xxx...
2017-01-01T01:04:48.911023+00:00 app[web.1]: [01-Jan-2017 10:04:48 Asia/Tokyo] 
2017-01-01T01:04:48.911063+00:00 app[web.1]: [01-Jan-2017 10:04:48 Asia/Tokyo] aaa...
2017-01-01T01:04:48.911125+00:00 app[web.1]: [01-Jan-2017 10:04:48 Asia/Tokyo] ����
2017-01-01T01:04:48.911165+00:00 app[web.1]: [01-Jan-2017 10:04:48 Asia/Tokyo] bbb...
2017-01-01T01:04:48.911201+00:00 app[web.1]: [01-Jan-2017 10:04:48 Asia/Tokyo] ����
2017-01-01T01:04:48.911239+00:00 app[web.1]: [01-Jan-2017 10:04:48 Asia/Tokyo] ccc...
2017-01-01T01:04:48.911273+00:00 app[web.1]: [01-Jan-2017 10:04:48 Asia/Tokyo] ��

图片转代码服务由CSDN问答提供 功能建议

过去有一些比较简单的问题。

如何发送带有标头的GET请求 PHP?

但我不知道为什么我的代码无效。 我希望通过使用cURL获取“状态代码200 OK和二进制图像数据” 带标题的GET请求。

我也可能在调试时出错。 我将非常感谢您的帮助。 提前谢谢!

API参考: https://devdocs.line.me/en/#get-content

  $ url =“https:// api。  line.me/v2/bot/message/”。  $ MESSAGE_ID。  “/ content”; 
 $ curl = curl_init(“$ url”); 
error_log(var_export($ curl)); 
 
 ncurl_setopt($ curl,CURLOPT_RETURNTRANSFER,true); 
curl_setopt($ curl,CURLOPT_SSL_VERIFYPEER,false  );  //证明书の検证を行わない
curl_setopt($ curl,CURLOPT_HTTPHEADER,array(
'授权:Bearer'。$ accessToken,
)); 
 
 $ response = curl_exec($ curl); 
  $ header_size = curl_getinfo($ curl,CURLINFO_HEADER_SIZE); 
 $ header = substr($ response,0,$ header_size); 
 $ image_binary = substr($ response,$ header_size); 
curl_close($ curl); 
  
error_log(的print_r( “XXX ......”,真)); 
error_log(var_export($响应)); 
error_log(的print_r( “AAA ...”,真)); 
error_log(的print_r($响应, 真)); 
error_log(的print_r( “BBB ...”,真)); 
error_log(的print_r($头,真)); 
error_log(的print_r( “CCC ...”,真)); 
error_log  (print_r($ image_binary,true)); 
   
 
 

然后..我明白了......

   2017-01-01T01:04:48.272544 + 00:00 app [web.1]:[01-Jan-2017 10:04:48 Asia / Tokyo] 
2017-01-01T01:04:48.911005 + 00:00 app  [web.1]:[01-Jan-2017 10:04:48 Asia / Tokyo] xxx ... 
2017-01-01T01:04:48.911023 + 00:00 app [web.1]:[01-Jan  -2017 10:04:48亚洲/东京] 
2017-01-01T01:04:48.911063 + 00:00 app [web.1]:[01-Jan-  2017 10:04:48亚洲/东京] aaa ... 
2017-01-01T01:04:48.911125 + 00:00 app [web.1]:[01-Jan-2017 10:04:48 Asia / Tokyo]  ����
2017-01-01T01:04:48.911165 + 00:00 app [web.1]:[01-Jan-2017 10:04:48 Asia / Tokyo] bbb ... 
2017-01-01T01  :04:48.911201 + 00:00 app [web.1]:[01-Jan-2017 10:04:48 Asia / Tokyo]����
2017-01-01T01:04:48.911239 + 00:00 app [  web.1]:[01-Jan-2017 10:04:48 Asia / Tokyo] ccc ... 
2017-01-01T01:04:48.911273 + 00:00 app [web.1]:[01-Jan-  2017 10:04:48亚洲/东京]��
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanou3868
    duanou3868 2016-12-30 13:14
    已采纳

    According to PHP documentation for CURLOPT_HEADER:

    TRUE to include the header in the output.

    Your $response will probably look like this:

    HTTP/1.1 200 OK
    Some: headers
    More: header lines
    
    {
        "real": "json content"
    }
    

    This is because you added the CURLOPT_HEADER option.

    You don't need to set any options to let the curl request send your headers. As long as you set the CURLOPT_HTTPHEADER option, the headers will be sent.

    If you really want to receive the response headers too, check existing questions like https://stackoverflow.com/a/9183272/3990767

    点赞 评论

相关推荐