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

在php中获取最后一个重定向的url,php,redirect,curl,Php,Redirect,Curl,嗨,我知道这是关于StackOverFlow的一个非常常见的话题。 我已经花了整整一周的时间去寻找它 我有一个url:abc.com/default.asp?strearch=19875379 这将进一步重定向到此url:abc.com/default.asp?catid={170D4F36-39F9-4C48-88EB-CFC8DDF1F531}&details_type=1&itemid={49F6A281-8735-4B74-A170-B6110AF6CC2D} 我已经尽力使用Curl在p

嗨,我知道这是关于StackOverFlow的一个非常常见的话题。 我已经花了整整一周的时间去寻找它

我有一个url:abc.com/default.asp?strearch=19875379

这将进一步重定向到此url: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;
?>

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

提前感谢您的帮助:)

您可以使用


首先,在运行您的代码时,我没有看到任何重定向。无论如何,这里有几件事你可以做(保持你的方法不变):

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

现在,只从整个http响应中分离头部分

// header will be at 0 index, and html will be at 1 index.
$header = explode("\n\r",$a);
将标题字符串分解为标题数组

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

谢谢大家在我的处境中帮助我

实际上,我想用php为以色列使用的宜家网站(希伯来语)开发一个刮板。 在花了很多时间之后,我意识到在url中没有服务器端重定向,我将其放在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; 
?>

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

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

非常感谢您的阅读。我已按照您的建议进行了更改,并获得以下输出:
<?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;
}