V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
hqfzone
V2EX  ›  PHP

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

  •  
  •   hqfzone · 2015-03-29 21:28:11 +08:00 · 6299 次点击
    这是一个创建于 3308 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    14 条回复    2015-04-11 13:58:26 +08:00
    hqfzone
        1
    hqfzone  
    OP
       2015-03-29 21:28:49 +08:00
    这是DNSPod的IP~
    hqfzone
        2
    hqfzone  
    OP
       2015-03-29 21:37:31 +08:00
    换了学校的VPS以及自己的虚拟机,都有问题~可以获取到httpheader,就是无法获取内容。
    l12ab
        3
    l12ab  
       2015-03-29 21:47:30 +08:00
    123.125.114.144;180.149.132.47
    Puteulanus
        4
    Puteulanus  
       2015-03-29 22:13:47 +08:00

    file_get_contents

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

    后来自己请求了下自己网站才发现,坑爹的,这玩意发请求不带user-agent,难怪人家要屏蔽,是我我也屏蔽
    txlty
        7
    txlty  
       2015-03-29 23:17:52 +08:00
    <?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  
       2015-03-29 23:40:58 +08:00
    这是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  
       2015-03-29 23:42:29 +08:00
    @xbonline 这不能算坑子
    bombless
        10
    bombless  
       2015-03-29 23:55:10 +08:00
    那就是错误的默认参数吧……
    hqfzone
        11
    hqfzone  
    OP
       2015-03-30 08:49:11 +08:00
    @txlty 这个方法有效,添加useragent或者楼上其它方案均无效,不知为何,谢谢。
    mingyun
        12
    mingyun  
       2015-04-05 19:50:28 +08:00
    擦,我试了也不行
    hqfzone
        13
    hqfzone  
    OP
       2015-04-07 23:03:48 +08:00
    @mingyun 用的apache服务器么?不知道楼上测试可以的朋友用的什么……不过 fsockopen 还是可以的。
    hqfzone
        14
    hqfzone  
    OP
       2015-04-11 13:58:26 +08:00
    更正,curl可以~
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4334 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 01:04 · PVG 09:04 · LAX 18:04 · JFK 21:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.