Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用PHP';从上次重定向中获取头文件;s旋度函数?_Php_Http_Curl - Fatal编程技术网

如何使用PHP';从上次重定向中获取头文件;s旋度函数?

如何使用PHP';从上次重定向中获取头文件;s旋度函数?,php,http,curl,Php,Http,Curl,如果执行设置为跟随重定向并返回头的cURL请求,它将返回所有重定向的头 我只希望返回最后一个标题(和内容正文)。如何实现这一点?在行首的输出中搜索“HTTP/1.1 200 OK”-这是您最后一个请求的开始。所有其他人都会给出其他HTTP返回码。还有另一种方法: $url = 'http://google.com'; $opts = array(CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION =&g

如果执行设置为跟随重定向并返回头的cURL请求,它将返回所有重定向的头

我只希望返回最后一个标题(和内容正文)。如何实现这一点?

在行首的输出中搜索“HTTP/1.1 200 OK”-这是您最后一个请求的开始。所有其他人都会给出其他HTTP返回码。

还有另一种方法:

$url = 'http://google.com';

$opts = array(CURLOPT_RETURNTRANSFER => true,
              CURLOPT_FOLLOWLOCATION => true,
              CURLOPT_HEADER         => true);
$ch = curl_init($url);
curl_setopt_array($ch, $opts);
$response       = curl_exec($ch);
$redirect_count = curl_getinfo($ch, CURLINFO_REDIRECT_COUNT);
$status         = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$response       = explode("\r\n\r\n", $response, $redirect_count + 2);
$last_header    = $response[$redirect_count];
if ($status == '200') {
    $body = end($response);
} else {
    $body = '';
}
curl_close($ch);

echo '<pre>';
echo 'Redirects: ' . $redirect_count . '<br />';
echo 'Status: ' . $status . '<br />';
echo 'Last response header:<br />' . $last_header . '<br />';
echo 'Response body:<br />' . htmlspecialchars($body) . '<br />';
echo '</pre>';
$url='1!'http://google.com';
$opts=数组(CURLOPT_RETURNTRANSFER=>true,
CURLOPT_FOLLOWLOCATION=>true,
CURLOPT_头=>true);
$ch=curl\u init($url);
curl_setopt_数组($ch,$opts);
$response=curl\u exec($ch);
$redirect\u count=curl\u getinfo($ch,CURLINFO\u redirect\u count);
$status=curl\u getinfo($ch,CURLINFO\u HTTP\u代码);
$response=explode(“\r\n\r\n”,$response$redirect\u count+2);
$last_header=$response[$redirect_count];
如果($status='200'){
$body=结束($response);
}否则{
$body='';
}
卷曲关闭($ch);
回声';
echo“重定向:”$“你算一算。”
; 回显“状态:”$地位。”
; 回显“最后一个响应头:
”$最后一个标题。”
; 回显“响应主体:
”。htmlspecialchars($body)。'
; 回声';
当然,您需要更多的错误检查,例如超时等

  • 执行您的请求

  • curl\u getinfo
    s返回值中获取标题的长度

  • 检索最后一个
    \r\n\r\n
    (但在标题结尾之前)和标题结尾之间的部分作为最后一个标题

  • //步骤1:执行
    $fullResponse=curl\u exec($ch);
    //第二步:测量头的长度
    $headerLength=curl\u getinfo($ch,CURLINFO\u HEADER\u SIZE);
    //步骤3:获取最后一个标题
    $header=substr($fullResponse,0,$headerLength-4);
    $lastHeader=substr($header,(strrpos($header,“\r\n\r\n”)?:-4)+4);
    

    当然,如果你有PHP<5.3,你必须把它扩展到一个if/else结构。

    后面的答案,但可能是更简单的方法


    不过有一件事——HTTP 1.0呢?“OK”是200之后的标准消息吗?我会使用preg_match(“/^HTTP 1\[01]200/”…来确定。HTTP 1.0上的观点很好。OK是事实上的标准,但是RFC只指定结果编号(200)。如果其中一个重定向响应主体包含一个额外的\r\n\r\n会发生什么情况?我最终做的只是跟随重定向,在301和302响应上循环。@m1tkr-这就是为什么对explode()的调用包含限制参数($redirect\u count+2).@George Edison-我相信这会奏效,但使用我发布的代码,您可以将所有标题和内容保持在一个整洁的数组中。@m1tk4-重新阅读后,我明白您的意思。我不相信(但不确定)任何Web服务器都会发送带有重定向响应的正文(仅使用IIS和Apache测试)。否则,需要在实际的、非演示的代码中检查它。
    $result = explode("\r\n\r\n", $result);
    
    // drop redirect etc. headers
    while (count($result) > 2) {
        array_shift($result);
    }
    
    // split headers / body parts
    @ list($headers, $body) = $result;