<?php
$url = 'http://fb.com';
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_HEADER => true,
));
$header = explode("\n", curl_exec($curl));
curl_close($curl);
print_r($header);
结果
HTTP/1.1 301 Moved Permanently
Location: http://www.facebook.com/?_rdr
Vary: Accept-Encoding
Content-Type: text/html
X-FB-Debug: rVg0o+qDt9z/zJu7jTW1gi1WSRC8YIMu3e6XnPagx39zZ4pbV0k2yrNfZmkdTLZyfzg713X+M0Lr2jS2P018xA==
Date: Thu, 25 Feb 2016 08:48:08 GMT
Connection: keep-alive
Content-Length: 0
但我想Location
一次全部得到
I enter > http://fb.com
then 301 redirect: http://www.facebook.com/?_rdr
then 302 redirect: https://www.facebook.com/
我想一次获得所有具有状态的链接 301
302
或任何更好的主意来获取重定向位置url。谢谢
您可以Location
使用以下命令从每个请求中获取所有标头,直到没有发送标头为止:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
$headers = curl_exec($ch);
curl_close($ch);
但是,然后您必须自己提取信息,因为$headers
它只是一个字符串,而不是数组。
如果您只需要最后一个位置,只需执行curl_getinfo($ch,CURLINFO_EFFECTIVE_URL)
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句