获取curl php中最后一个重定向的url [英] get the last redirected url in curl php

查看:276
本文介绍了获取curl php中最后一个重定向的url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道它是一个非常常见的主题StackOverFlow。
我已经花了整整一个星期来搜索它。

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

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

I have a url : 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}

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}

我已经努力使用Curl获取我的PHP代码中的最终网址,但不能使用。

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

这里是我的代码:

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

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

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}"

感谢您的帮助:)

推荐答案

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

Thank you everyone for helping me in my situation.

实际上,我想为以色列(希伯来语)使用的ikea网站的php开发一个剪贴板。
经过了很多时间,我意识到没有服务器端重定向在url,我放置得到重定向的url。它可能是javascript重定向。
我现在实现了下面的代码,它适用于我。

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

再次感谢大家:)

这篇关于获取curl php中最后一个重定向的url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆