V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
lissome
V2EX  ›  问与答

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

  •  
  •   lissome · 2015-07-23 16:28:40 +08:00 · 3423 次点击
    这是一个创建于 3202 天前的主题,其中的信息可能已经有所发展或是发生改变。
    http://i.doovoo.biz/1.php

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

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

    如何才能用代码获得真实的调整url?
    7 条回复    2015-07-24 09:40:39 +08:00
    Jeremial
        1
    Jeremial  
       2015-07-23 16:33:00 +08:00
    feiyuanqiu
        2
    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
        3
    lissome  
    OP
       2015-07-23 16:40:12 +08:00
    @Jeremial
    @feiyuanqiu

    我也只能得到这两个url

    但是我想要的是最后 “草榴” 的url
    feiyuanqiu
        4
    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
        5
    lissome  
    OP
       2015-07-23 17:14:22 +08:00
    @feiyuanqiu
    谢谢尝试,不过这个url 也是“假”的
    lissome
        6
    lissome  
    OP
       2015-07-23 17:15:02 +08:00
    @feiyuanqiu
    居然还仿了一个页面。。。
    lissome
        7
    lissome  
    OP
       2015-07-24 09:40:39 +08:00
    还没有搞定
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1261 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 17:50 · PVG 01:50 · LAX 10:50 · JFK 13:50
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.