开发者

How get the url of the returned page?

$curl = curl_init();
 curl_setopt($curl, CURLOPT_URL, 'http://foo.com');
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
 curl_set开发者_Python百科opt($curl, CURLOPT_FOLLOWLOCATION, 1);
 curl_setopt($curl, CURLOPT_TIMEOUT, 30);
 curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9.2) Gecko/20100115 Firefox/3.6");
 $page = curl_exec($curl);

Now the site foo.com sends different redrects and I don't know what a page is inside $page variable, foo.com/bar.php or foo.com/baz.php or other. My question is how get the url of the page inside $page variable.

Thanks.


Set the option CURLOPT_HEADER to 1 and parse the returned header.

Or use curl_getinfo() with option CURLINFO_EFFECTIVE_URL.


curl_getinfo($curl, CURLINFO_EFFECTIVE_URL);


You could use curl_getinfo();

$info = curl_getinfo($curl);
print_r($info); // prints all the info array

if you want to only get the url, use:

echo $info['url'];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜