[英]PHP cURL Response Header
I'm trying out HTTP Requests for the first time (also using PostMan).我是第一次尝试 HTTP 请求(也使用 PostMan)。 There, everything works fine.在那里,一切正常。 When exporting it with PHP cURL I get the following:使用 PHP cURL 导出它时,我得到以下信息:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "htt..",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
"Authorization: Basic ..."
),
));
$response = curl_exec($curl);
curl_close($curl);
I am expecting a Key in the response Header, So I'm trying to get the Response Header as a variable (pref. Array).我期待响应标头中有一个键,所以我试图将响应标头作为变量(首选数组)。 How would I do this?我该怎么做? I got no luck so far trying to use the following:到目前为止,我没有运气尝试使用以下内容:
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
Thanks for any help in advance.提前感谢您的任何帮助。
Edit 1:编辑1:
Using https://incarnate.github.io/curl-to-php/ I converted the Postman cURL command to PHP cURL (instead of using Postman's generator) and got the following, which seems to work:使用https://incarnate.github.io/curl-to-php/我将 Postman cURL 命令转换为 PHP cURL(而不是使用 Postman 的生成器)并得到以下内容,这似乎有效:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'htt..');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //manually added
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); //manually added
curl_setopt($ch, CURLOPT_HEADER, 1); //manually added
$headers = array();
$headers[] = 'Authorization: Basic ...';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($result, 0, $header_size);
$body = substr($result, $header_size);
curl_close($ch);
echo $header;
Your'e really close to solving this, but you need to set the CURLOPT_HEADER
to true to retrieve the headers.你真的很接近解决这个问题,但你需要将CURLOPT_HEADER
为 true 来检索标题。 This change should work:此更改应该有效:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "htt..",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HEADER => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_HTTPHEADER => array(
"Authorization: Basic ..."
),
));
$response = curl_exec($curl);
if (curl_errno($curl) !== CURLE_OK) {
throw new \RuntimeException("curl_exec error: " . curl_error($ch) . ": " . curl_error($ch));
}
$header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$header = substr($response, 0, $header_size);
$body = substr($response, $header_size);
curl_close($curl);
$response is always only response body. $response 始终只是响应主体。
Try to look at option for curl_getinfo尝试查看 curl_getinfo 的选项
CURLINFO_HEADER_OUT - The request string sent. For this to work, add the CURLINFO_HEADER_OUT option to the handle by calling curl_setopt()
Here is the documentation https://www.php.net/manual/en/function.curl-getinfo.php这是文档https://www.php.net/manual/en/function.curl-getinfo.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.