Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.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 使用cURL查找网站重定向的位置?_Php_Curl - Fatal编程技术网

Php 使用cURL查找网站重定向的位置?

Php 使用cURL查找网站重定向的位置?,php,curl,Php,Curl,我正在尝试获取服务器重定向url。我试过了 function http_head_curl($url,$timeout=10) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); // in seconds curl_setopt($ch, CURLOPT_HEADER, 1); curl_set

我正在尝试获取服务器重定向url。我试过了

    function http_head_curl($url,$timeout=10)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); // in seconds
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_NOBODY, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $res = curl_exec($ch);
    if ($res === false) {
        throw new RuntimeException("cURL exception: ".curl_errno($ch).": ".curl_error($ch));
    }
    return trim($res);
}


echo http_head_curl("http://www.site.com",$timeout=10);
结果是

HTTP/1.1 301永久移动日期:2013年5月12日星期日23:34:22 GMT 服务器:LiteSpeed连接:关闭X-Powered-By:PHP/5.3.23 设置Cookie:PHPSESSID=0d4b28dd02bd3d8413c92f71253e8b31;路径=/; HttpOnly X-Pingback:内容类型: text/html;字符集=UTF-8位置:HTTP/1.1 200正常 日期:2013年5月12日星期日23:34:23 GMT服务器:LiteSpeed连接: 关闭X-Powered-By:PHP/5.3.23设置Cookie: PHPSESSID=630ed27f107c07d25ee6dbfcb02e8dec;路径=/;HttpOnly X-Pingback:内容类型:text/html; 字符集=UTF-8


它显示几乎所有的头信息,但不显示重定向的位置。如何获取重定向的页面url?

这是
位置的
标题

$headers = array();
$lines = explode("\n", http_head_curl('http://www.site.com', $timeout = 10));

list($protocol, $statusCode, $statusMsg) = explode(' ', array_shift($lines), 3);

foreach($lines as $line){
  $line = explode(':', $line, 2);
  $headers[trim($line[0])] = isset($line[1]) ? trim($line[1]) : '';
}

// 3xx = redirect    
if(floor($statusCode / 100) === 3)
  print $headers['Location'];

完成CURL请求后,使用CURLINFO\u EFFECTIVE\u URL选项。完成了


与其他(复杂的)答案相比,这将为您提供请求“结束时”的完整URL。

如果$statuscode在数组中以字符串形式返回,您可以使用:If($statuscode[0]==3)是的,我在上一次编辑中确实有,但感觉很难看:)
$response = curl_exec($ch);
$info = curl_getinfo($ch);
$response_header = substr($response, 0, $info['header_size']);
$response_header = parseHeaders($response_header, 'Status');
$content = substr(response, $info['header_size']);
$url_redirect = (isset($response_header['Location'])) ? $response_header['Location'] : null;
var_dump($url_redirect);

/*
* or you can use http://php.net/http-parse-headers, 
* but then need to install http://php.net/manual/en/book.http.php
*/
function parseHeaders($headers, $request_line)
{
    $results = array();
    $lines = array_filter(explode("\r\n", $headers));
    foreach ($lines as $line) {
        $name_value = explode(':', $line, 2);
        if (isset($name_value[1])) {
            $name = $name_value[0];
            $value = $name_value[1];
        } else {
            $name = $request_line;
            $value = $name_value[0];
        }
        $results[$name] = trim($value);
    }
    return $results;
}