Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.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 从重定向获取url_Php_Url_Redirect_Curl - Fatal编程技术网

Php 从重定向获取url

Php 从重定向获取url,php,url,redirect,curl,Php,Url,Redirect,Curl,我目前正在使用cURL尝试从重定向中获取网站的URL。我只需要从网站的网址。在过去的几天里,我一直在研究stackoverflow和其他网站,但都没有成功。我当前使用的代码来自此网站: $url = "http://www.someredirect.com"; $ch = curl_init($url); curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8

我目前正在使用cURL尝试从重定向中获取网站的URL。我只需要从网站的网址。在过去的几天里,我一直在研究stackoverflow和其他网站,但都没有成功。我当前使用的代码来自此网站:

  $url = "http://www.someredirect.com";
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1');         
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_HEADER, true);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
  curl_setopt($ch, CURLOPT_NOBODY, true);
  $response = curl_exec($ch);
  preg_match_all('/^Location:(.*)$/mi', $response, $matches);
  curl_close($ch);
  echo !empty($matches[1]) ? trim($matches[1][0]) : 'No redirect found';
任何帮助都将不胜感激

尝试使用以下代码:

function curl_last_url(/*resource*/ $ch, /*int*/ &$maxredirect = null) { 
$mr = $maxredirect === null ? 5 : intval($maxredirect); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); 
    if ($mr > 0) { 
        echo $mr;
        echo $newurl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); 

        $rch = curl_copy_handle($ch); 
        curl_setopt($rch, CURLOPT_HEADER, true); 
        curl_setopt($rch, CURLOPT_NOBODY, true); 
        curl_setopt($rch, CURLOPT_FORBID_REUSE, false); 
        curl_setopt($rch, CURLOPT_RETURNTRANSFER, true); 
        do { 
            curl_setopt($rch, CURLOPT_URL, $newurl); 
            $header = curl_exec($rch); 
            if (curl_errno($rch)) { 
                $code = 0; 
            } else { 
                $code = curl_getinfo($rch, CURLINFO_HTTP_CODE); 
                echo $code;
                if ($code == 301 || $code == 302) { 
                    preg_match('/Location:(.*?)\n/', $header, $matches); 
                    $newurl = trim(array_pop($matches)); 
                } else { 
                    $code = 0; 
                } 
            } 
        } while ($code && --$mr); 
        curl_close($rch); 
        if (!$mr) { 
            if ($maxredirect === null) { 
                trigger_error('Too many redirects. When following redirects, libcurl hit the maximum amount.', E_USER_WARNING); 
            } else { 
                $maxredirect = 0; 
            } 
            return false; 
        } 
        curl_setopt($ch, CURLOPT_URL, $newurl); 
    } 
return $newurl; 

}

在您的特定情况下,服务器正在检查某些用户代理字符串

当服务器检查用户代理字符串时,仅当服务器看到“有效”(根据服务器)用户代理时,才会使用
302
重定向状态代码进行响应。任何“无效”用户代理都不会收到
302
重定向状态代码响应或
位置:
标题

在您的特定情况下,当服务器收到来自“无效”用户代理的请求时,它会以
200
OK状态代码进行响应,响应正文中没有文本

(注意:在下面的代码中,提供的实际URL已替换为示例。)

假设
http://www.example.com
的服务器检查用户代理字符串,并且
http://www.example.com/product/123/
重定向到
http://www.example.org/abc

在PHP中,您的解决方案是:

<?php

$url = 'http://www.example.com/product/123/';

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Linux x86_64; rv:21.0) Gecko/20100101 Firefox/21.0"); // Necessary. The server checks for a valid User-Agent.
curl_exec($ch);

$response = curl_exec($ch);
preg_match_all('/^Location:(.*)$/mi', $response, $matches);
curl_close($ch);

echo !empty($matches[1]) ? trim($matches[1][0]) : 'No redirect found';

preg_match_all('/^Location:(.*)$/mi',$response,$matches),是否尝试匹配
标题('位置:http://google.com');例如?:/是 啊试图从标题中解析位置,但标题中没有返回位置标记。在这种情况下,您的代码对我来说很好。是的,我注意到了,应该说点什么。如果它不是一个技术重定向,但仍然指向另一个站点,我将如何获取我想要的url?在命令行上,您可以使用
curl
验证重定向。例如,
curl-ILhttp://microsoft.com
将首先为您提供状态代码301,然后是状态代码200。当我输入你给我的url时,第一个url上的状态码是200 OK。@Josh取决于重定向的方式。如果重定向是在HTML或JavaScript中完成的,那么这个链接应该很有用:更新了解决方案,使其适用于这个特定的服务器。此外,我用示例替换了@Josh提供的实际URL。