dailiip
V2EX  ›  PHP

PHP file_get_contents('http://119.29.29.29/d?dn=baidu.com') 为何失败

  •  
  •   dailiip · Mar 29, 2015 · 6843 views
    This topic created in 4063 days ago, the information mentioned may be changed or developed.

    其它网站可以获取到,为什么这个不可以。curl也不行,求解答,谢谢。

    14 replies    2015-04-11 13:58:26 +08:00
    dailiip
        1
    dailiip  
    OP
       Mar 29, 2015
    这是DNSPod的IP~
    dailiip
        2
    dailiip  
    OP
       Mar 29, 2015
    换了学校的VPS以及自己的虚拟机,都有问题~可以获取到httpheader,就是无法获取内容。
    l12ab
        3
    l12ab  
       Mar 29, 2015
    123.125.114.144;180.149.132.47
    Puteulanus
        4
    Puteulanus  
       Mar 29, 2015

    file_get_contents

    curl
    楼主看看是不是网的问题?
    dailiip
        5
    dailiip  
    OP
       Mar 29, 2015
    @Puteulanus 应该不是网络问题啊,get_headers('http://119.29.29.29/d?dn=baidu.com')可以获取到httpheader,就是file_get_contents不行。
    NewYear
        6
    NewYear  
       Mar 29, 2015
    我用file_get_contents获取某大人站的首页做RSS,结果一段时间后被对方服务器B掉了IP……ping都ping不通了。

    后来自己请求了下自己网站才发现,坑爹的,这玩意发请求不带user-agent,难怪人家要屏蔽,是我我也屏蔽
    txlty
        7
    txlty  
       Mar 29, 2015
    <?php
    $fp = fsockopen("119.29.29.29", 80);
    fwrite($fp,"GET /d?dn=baidu.com HTTP/1.1\r\n");
    fwrite($fp,"Host: 119.29.29.29\r\n");
    fwrite($fp,"Connection: Close\r\n\r\n");
    for($i=0;$i<6;$i++){$data.=fgets($fp, 128);}
    preg_match("/Content-Length:.?(\d+)/", $data, $matches);
    $length = $matches[1];
    $result = fgets($fp, $length+1);
    fclose($fp);
    echo $result;
    ?>
    xbonline
        8
    xbonline  
       Mar 29, 2015
    这是file_get_contents的坑

    因为服务器开启了keep-alive
    file_get_contents默认是不发送connection:close头,服务器以为还有后续http请求,所以继续等待几十秒才close,导致file_get_contents阻塞

    解决方案: http://stackoverflow.com/questions/3629504/php-file-get-contents-very-slow-when-using-full-url
    fising
        9
    fising  
       Mar 29, 2015
    @xbonline 这不能算坑子
    bombless
        10
    bombless  
       Mar 29, 2015
    那就是错误的默认参数吧……
    dailiip
        11
    dailiip  
    OP
       Mar 30, 2015
    @txlty 这个方法有效,添加useragent或者楼上其它方案均无效,不知为何,谢谢。
    mingyun
        12
    mingyun  
       Apr 5, 2015
    擦,我试了也不行
    dailiip
        13
    dailiip  
    OP
       Apr 7, 2015
    @mingyun 用的apache服务器么?不知道楼上测试可以的朋友用的什么……不过 fsockopen 还是可以的。
    dailiip
        14
    dailiip  
    OP
       Apr 11, 2015
    更正,curl可以~
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2817 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 15:30 · PVG 23:30 · LAX 08:30 · JFK 11:30
    ♥ Do have faith in what you're doing.