douzhi1972
2013-12-14 11:29
浏览 155
已采纳

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

Hi I know its a very common topic on StackOverFlow. I have already spent my entire week to search it out.

I have a url : abc.com/default.asp?strSearch=19875379

this further redirect to this url: abc.com/default.asp?catid={170D4F36-39F9-4C48-88EB-CFC8DDF1F531}&details_type=1&itemid={49F6A281-8735-4B74-A170-B6110AF6CC2D}

I have made my effort to get the final url in my php code using Curl but can't make it.

here is my code:

<?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("
",$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;
?>

it gives me url "abc.com/default.asp?strSearch=19875379" instead of this url "abc.com/default.asp?catid={170D4F36-39F9-4C48-88EB-CFC8DDF1F531}&details_type=1&itemid={49F6A281-8735-4B74-A170-B6110AF6CC2D}"

Thanks in advance for your kind help :)

图片转代码服务由CSDN问答提供 功能建议

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

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

nnn

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

我做了我的 努力使用Curl在我的PHP代码中获取最终URL但无法实现。

这里是我的代码:

 &lt  ;?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);  
 //返回的标题
 $ headers = explode(“
”,$ a); 
 //如果没有重定向,这将是最终的url 
 $ redir = $ url; 
 // 循环遍历标题并检查位置:str 
 $ j = count($ headers); 
for($ i = 0; $ i&lt; $ j; $ i ++){
 // //如果找到位置 标题剥离它并填充redir var 
 // print_r($ headers); 
if(strpos($ headers [$ i],“Location:”)!== false){
 $ redir = trim(str_replace(  “位置:”,“”,$标题[$ i])); 
 break; 
} 
} 
 //使用结果执行任何操作
echo $ redir; 
?&gt; 
    
 
 

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

预先感谢您的帮助:)

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

3条回答 默认 最新

  • dsarttv037029 2013-12-16 08:00
    最佳回答

    Thank you everyone for helping me in my situation.

    Actually I want to develop a scrapper in php for ikea website used in Israel (in Hebrew). After putting a lot of hours I recognize that there is no server side redirection in url which I put to get the redirected url. It may be javascript redirection. I have now implemented the below code and it works for me.

    <?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=(.|
    )*?<\/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; 
    ?>
    

    Thanks again everyone :)

    评论
    解决 无用
    打赏 举报
查看更多回答(2条)

相关推荐 更多相似问题