【问题标题】:get the last redirected url in curl php在 curl php 中获取最后一个重定向的 url
【发布时间】:2014-01-02 04:17:45
【问题描述】:

您好,我知道这是 StackOverFlow 上非常常见的话题。 我已经花了整整一周的时间来搜索它。

我有一个网址:abc.com/default.asp?strSearch=19875379

这进一步重定向到这个网址:abc.com/default.asp?catid={170D4F36-39F9-4C48-88EB-CFC8DDF1F531}&details_type=1&itemid={49F6A281-8735-4B74-A170-B6110AF6CC2D}

我已努力使用 Curl 在我的 php 代码中获取最终 url,但无法实现。

这是我的代码:

<?php
$name="19875379";
$url = "http://www.ikea.co.il/default.asp?strSearch=".$name;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$a = curl_exec($ch);
curl_close( $ch ); 
// the returned headers
$headers = explode("\n",$a);
// if there is no redirection this will be the final url
$redir = $url;
// loop through the headers and check for a Location: str
$j = count($headers);
for($i = 0; $i < $j; $i++){
// if we find the Location header strip it and fill the redir var     
//print_r($headers);
if(strpos($headers[$i],"Location:") !== false){
        $redir = trim(str_replace("Location:","",$headers[$i]));
        break;
    }
}
// do whatever you want with the result
echo $redir;
?>

它给了我网址“abc.com/default.asp?strSearch=19875379”而不是这个网址“abc.com/default.asp?catid={170D4F36-39F9-4C48-88EB-CFC8DDF1F531}&details_type=1&itemid= {49F6A281-8735-4B74-A170-B6110AF6CC2D}"

提前感谢您的帮助:)

【问题讨论】:

    标签: php redirect curl


    【解决方案1】:

    感谢大家在我的情况下帮助我。

    其实我想在 php 中为以色列使用的宜家网站(希伯来语)开发一个 scraper。 花了很多时间后,我意识到 url 中没有服务器端重定向,我将其用于获取重定向的 url。它可能是javascript重定向。 我现在已经实现了下面的代码,它对我有用。

    <?php
    $name="19875379";
    $url = "http://www.ikea.co.il/default.asp?strSearch=".$name;
    
    $ch = curl_init();
    $timeout = 0;
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_HEADER, TRUE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    $header = curl_exec($ch);
    $redir = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
    //print_r($header);
    
    $x = preg_match("/<script>location.href=(.|\n)*?<\/script>/", $header, $matches);
    $script = $matches[0];
    $redirect = str_replace("<script>location.href='", "", $script);
    $redirect = "http://www.ikea.co.il" . str_replace("';</script>", "", $redirect);
    
    echo $redirect; 
    ?>
    

    再次感谢大家:)

    【讨论】:

      【解决方案2】:

      接受的答案适用于非常具体的场景。所以,我们大多数人最好有一个更笼统的答案。虽然您可以从接受的答案中提取更一般的答案,但单独拥有该部分可能会更有帮助。

      因此,如果您只想获取最后重定向的 URL,此代码会有所帮助。

      <?php
      
      function redirectedUrl($url) {
          $ch = curl_init();
      
          curl_setopt($ch, CURLOPT_URL, $url);
          curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // set browser info to avoid old browser warnings
          curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // allow url redirects
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // get the return value of curl execution as a string
          
          $html = curl_exec($ch);
          
          // store last redirected url in a variable before closing the curl session 
          $lastUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
          
          curl_close($ch);
      
          return $lastUrl;
      }
      

      【讨论】:

        【解决方案3】:

        【讨论】:

        • 非常感谢您的阅读。我已按照您的建议进行了更改,并得到了以下输出:
        【解决方案4】:

        首先,我在运行您的代码时没有看到任何重定向。无论如何,您可以为此做一些事情(保持您的方法不变):

        首先,确保标头将返回到您的 curl 输出(在本例中为 $a)。

        curl_setopt($ch, CURLOPT_HEADER, true);
        

        现在,仅将标头部分与整个 http 响应分开。

        // header will be at 0 index, and html will be at 1 index.
        $header = explode("\n\r",$a);
        

        将标题字符串分解为标题数组。

        $headers = explode("\n", $header[0]);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-03-05
          • 1970-01-01
          • 2018-09-11
          • 2016-11-14
          • 2012-05-21
          • 2010-12-29
          • 2011-05-03
          相关资源
          最近更新 更多