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 :

this further redirect to this url:{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:

$url = "".$name;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
$a = curl_exec($ch);
curl_close( $ch ); 
// the returned headers
$headers = explode("
// 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     
if(strpos($headers[$i],"Location:") !== false){
        $redir = trim(str_replace("Location:","",$headers[$i]));
// do whatever you want with the result
echo $redir;

it gives me url "" instead of this url "{170D4F36-39F9-4C48-88EB-CFC8DDF1F531}&details_type=1&itemid={49F6A281-8735-4B74-A170-B6110AF6CC2D}"

Thanks in advance for your kind help :)

  • 写回答

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.

    $url = "".$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);
    $x = preg_match("/<script>location.href=(.|
    )*?<\/script>/", $header, $matches);
    $script = $matches[0];
    $redirect = str_replace("<script>location.href='", "", $script);
    $redirect = "" . str_replace("';</script>", "", $redirect);
    echo $redirect; 

    Thanks again everyone :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



  • ¥15 调用函数时,无关变量的改变引起函数值的改变
  • ¥15 xy坐标转化为经纬度坐标
  • ¥15 一般三角模糊数的上界值和下届值取中值的多少比较合理?
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥20 Hbase启动失败,无法启动HMaster
  • ¥20 Lumerical FDTD solutions 中模型的相对阻抗,有效介电常数和有效磁导率的实部和虚部的数据如何获得?
  • ¥100 sql reporting service 远程smtp服务器配置支持
  • ¥15 ppyoloe_r带角度目标检测,loss_cls没法收敛
  • ¥15 淘宝交易指数如何解读,其关联的数据指标是什么
  • ¥15 Linux下安装搜狗拼音不能显示中文