请教如何获得 PHP 跳转最终的 url

2015-07-23 16:28:40 +08:00
 lissome
http://i.doovoo.biz/1.php

试过用 curl 和 socket 都取不到真实的最终url

作者是否是用这种方式增加“假”的目标网址的流量?

如何才能用代码获得真实的调整url?
3513 次点击
所在节点    问与答
7 条回复
Jeremial
2015-07-23 16:33:00 +08:00
feiyuanqiu
2015-07-23 16:37:45 +08:00
卧槽!我在公司啊,给我跳转到草榴去了!
...

file_get_contents('http://i.doovoo.biz/1.php');
var_dump($http_response_header);exit;

请求完成之后直接拿头信息就可以得到最终跳转的url了
注意前面几个 302 跳转

array (size=26)
0 => string 'HTTP/1.1 302 Found' (length=18)
1 => string 'Date: Thu, 23 Jul 2015 08:33:26 GMT' (length=35)
2 => string 'Server: Apache' (length=14)
3 => string 'X-Powered-By: PHP/5.5.9-1ubuntu4.4' (length=34)
4 => string 'Set-Cookie: iutobe=yes; expires=Sun, 26-Jul-2015 08:33:26 GMT; Max-Age=259200' (length=77)
5 => string 'Location: http://ad.www.clwaiting.com/so/all.php' (length=48)
6 => string 'Content-Length: 0' (length=17)
7 => string 'Connection: close' (length=17)
8 => string 'Content-Type: text/html' (length=23)
9 => string 'HTTP/1.1 302 Found' (length=18)
10 => string 'Date: Thu, 23 Jul 2015 08:33:26 GMT' (length=35)
11 => string 'Server: Apache/2.2.27 (CentOS)' (length=30)
12 => string 'X-Powered-By: PHP/5.2.17' (length=24)
13 => string 'Set-Cookie: iutobe=yes; expires=Sun, 26-Jul-2015 08:33:26 GMT' (length=61)
14 => string 'Set-Cookie: page=https%3A%2F%2Fwww.caodsma.com%2Ft%2F; expires=Sun, 26-Jul-2015 08:33:26 GMT' (length=92)
15 => string 'Location: https://www.caodsma.com/t/' (length=36)
16 => string 'Content-Length: 0' (length=17)
17 => string 'Connection: close' (length=17)
18 => string 'Content-Type: text/html; charset=UTF-8' (length=38)
19 => string 'HTTP/1.1 200 OK' (length=15)
20 => string 'Server: nginx' (length=13)
21 => string 'Date: Thu, 23 Jul 2015 08:33:28 GMT' (length=35)
22 => string 'Content-Type: text/html' (length=23)
23 => string 'Connection: close' (length=17)
24 => string 'Vary: Accept-Encoding' (length=21)
25 => string 'X-Powered-By: PHP/5.4.41' (length=24)
lissome
2015-07-23 16:40:12 +08:00
@Jeremial
@feiyuanqiu

我也只能得到这两个url

但是我想要的是最后 “草榴” 的url
feiyuanqiu
2015-07-23 17:06:47 +08:00
拿到了,他的服务器稍微检查了下header看是不是爬虫,所以你爬的时候需要带一个header:

$opt = array(
'http' => array(
'method' => 'GET',
'header' => "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8\r\n" .
"User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.89 Safari/537.36\r\n"
),
);
stream_context_get_default($opt);
print_r(get_headers('http://i.doovoo.biz/1.php'));exit;

然后拿最后一个Location就行了

[25] => Location: https://www.lettoelectronics.com/site/www.bd.com/
[26] => HTTP/1.1 200 OK
[27] => Date: Thu, 23 Jul 2015 09:05:23 GMT
[28] => Server: Apache/2.2.27 (CentOS)
lissome
2015-07-23 17:14:22 +08:00
@feiyuanqiu
谢谢尝试,不过这个url 也是“假”的
lissome
2015-07-23 17:15:02 +08:00
@feiyuanqiu
居然还仿了一个页面。。。
lissome
2015-07-24 09:40:39 +08:00
还没有搞定

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/207831

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX