在curl php中获取最后一个重定向的url

时间:2013-12-14 11:29:01

标签: php redirect curl

嗨,我知道它是StackOverFlow上一个非常常见的主题。 我已经花了整整一周的时间来搜索它。

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

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

我已经努力使用Curl在我的PHP代码中获取最终网址,但无法实现。

这是我的代码:

<?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;
?>

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

提前感谢您的帮助:)

4 个答案:

答案 0 :(得分:2)

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

实际上我想在以色列使用的ikea网站(希伯来语)中为php开发一个剪贴簿。 在花了很多时间后,我发现在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; 
?>

再次感谢大家:)

答案 1 :(得分:0)

答案 2 :(得分:0)

首先,当我对代码进行运行时,我没有看到任何重定向。无论如何,这里有一些你可以做的事情(保持你的方法完好无损):

首先,确保标题将返回到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]);

答案 3 :(得分:0)

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

因此,如果您只想获取最后一个重定向的 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;
}
相关问题