从PHP cURL响应中获取标题
我是PHP新手。 我试图在发送php curl POST请求后从响应中获取Header。 客户端发送请求到服务器,服务器用Header发回响应。 以下是我发送POST请求的方式。
$client = curl_init($url);
curl_setopt($client, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($client, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($client, CURLOPT_HEADER, 1);
$response = curl_exec($client);
var_dump($response);
这是来自浏览器的来自服务器的标题响应
HTTP/1.1 200 OK
Date: Wed, 01 Feb 2017 11:40:59 GMT
Authorization: eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1c2Vycy9CYW9CaW5oMTEwMiIsIm5hbWUiOiJhZG1pbiIsInBhc3N3b3JkIjoiMTIzNCJ9.kIGghbKQtMowjUZ6g62KirdfDUA_HtmW-wjqc3ROXjc Content-Type: text/html;charset=utf-8 Transfer-Encoding: chunked Server: Jetty(9.3.6.v20151106)
我如何从头文件中提取授权部分? 我需要将它存储在cookies中
它将所有标题转换为数组
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, "example.com");
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//enable headers
curl_setopt($ch, CURLOPT_HEADER, 1);
//get only headers
curl_setopt($ch, CURLOPT_NOBODY, 1);
// $output contains the output string
$output = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
$headers = [];
$data = explode("n",$output);
$headers['status'] = $data[0];
array_shift($data);
foreach($data as $part){
$middle=explode(":",$part);
$headers[trim($middle[0])] = trim($middle[1]);
}
// Print all headers as array
echo "<pre>";
print_r($headers);
echo "</pre>";
你只需在curl请求中包含这个编码
curl_setopt($curl_exec, CURLOPT_HEADER, true);
curl_setopt($curl_exec, CURLOPT_NOBODY, true);
curl执行后使用$header_data= curl_getinfo($curl_exec);
然后你得到所有的标题
print_r($header_data);
或者使用shell_exec
echo shell_exec("curl -I http://example.com ");
链接地址: http://www.djcxy.com/p/57803.html
上一篇: Get Header from PHP cURL response
下一篇: How to send 500 Internal Server Error error from a PHP script