PHP将cURL和GET请求与标头一起使用 [英] PHP Using cURL and GET request with a header

查看:129
本文介绍了PHP将cURL和GET请求与标头一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去像下面这样更苗条的问题。

There are slimier questions in the past like below.

如何通过php发送带有标头的GET请求?

但是我不知道为什么我的代码无法正常工作。
我想通过使用带有标头的cURL和GET请求获得状态码200 OK和二进制图像数据。

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引用: 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] ��


推荐答案

根据 CURLOPT_HEADER 的PHP文档:


TRUE 将标头包含在输出中。

TRUE to include the header in the output.

您的 $ response 可能看起来像这样:

Your $response will probably look like this:

HTTP/1.1 200 OK
Some: headers
More: header lines

{
    "real": "json content"
}

这是因为您添加了 CURLOPT_HEADER 选项。

This is because you added the CURLOPT_HEADER option.

您无需设置任何选项即可让curl请求发送标头。只要您设置 CURLOPT_HTTPHEADER 选项,将发送标题。

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.

如果您确实也想接收响应标题,请检查现有问题,例如PHP cURL可以在单个请求中检索响应标头和正文吗?

If you really want to receive the response headers too, check existing questions like "Can PHP cURL retrieve response headers AND body in a single request?"

这篇关于PHP将cURL和GET请求与标头一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆