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

php 打开 url 响应时间长达 10 秒,同网络环境下浏览器秒开

  •  
  •   imydou · 2015-09-23 13:07:53 +08:00 · 3344 次点击
    这是一个创建于 3146 天前的主题,其中的信息可能已经有所发展或是发生改变。

    微信企业号 api
    比如用 file_get_contents 或者 fopen 打开接口链接获取 token ,
    函数执行前打印的时间戳和执行后打印的时间戳相差 10 秒(多网段服务器、本地网络测试均如此)。
    但链接直接用浏览器打开却秒开。

    为什么?

    9 条回复    2018-09-27 18:58:21 +08:00
    explon
        1
    explon  
       2015-09-23 13:12:41 +08:00
    用 CURL 试试
    gaitana
        2
    gaitana  
       2015-09-23 13:14:32 +08:00
    php 不会缓存 DNS ,把 php 访问的域名 IP 地址写入 hosts 即可解决此问题。
    iyaozhen
        3
    iyaozhen  
       2015-09-23 13:16:32 +08:00
    可能是这两个问题:
    1. DNS 解析过慢,换个 DNS ,或者直接写死 hosts
    2. file_get_contents 有时容易夯住,建议使用 curl
    imydou
        4
    imydou  
    OP
       2015-09-23 13:17:00 +08:00
    @gaitana 并非,测试给用户发送信息接口时,触发瞬间微信收到信息, 10 秒后接口才返回执行字符串。
    imydou
        5
    imydou  
    OP
       2015-09-23 13:18:12 +08:00
    @explon
    @iyaozhen 感谢指点, curl 可以了,
    本来偷懒不用 curl 能少写几行代码,不想遇到诡异问题。
    iyaozhen
        6
    iyaozhen  
       2015-09-23 13:21:44 +08:00   ❤️ 1
    @imydou file_get_contents 有其它设置可以避免夯住。你可以把 curl 封装下呗,到时也需要一些 POST 请求需要 curl 处理的。
    explon
        7
    explon  
       2015-09-23 13:23:05 +08:00   ❤️ 1
    @imydou

    function curl_get_contents($url, $timeout = 10)
    {
    if (!function_exists('curl_init'))
    {
    die('CURL not support');
    }

    $curl = curl_init();

    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curl, CURLOPT_HEADER, FALSE);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);

    if (defined('WECENTER_CURL_USERAGENT'))
    {
    curl_setopt($curl, CURLOPT_USERAGENT, WECENTER_CURL_USERAGENT);
    }
    else
    {
    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/600.7.12 (KHTML, like Gecko) Version/8.0.7 Safari/600.7.12');
    }

    if (substr($url, 0, 8) == 'https://')
    {
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);

    curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
    }

    $result = curl_exec($curl);

    curl_close($curl);

    return $result;
    }
    Rimifon
        8
    Rimifon  
       2018-09-27 18:54:32 +08:00
    遇到了同样的问题,file_get_contents、fsocketopen、curl 三个都试了,都需要 10 秒钟。但是用浏览器访问秒开(同一主机)。用 fsocketopen 监控的时候发现在打开端口的时候就花掉了 9 秒钟,后续其实是很快的。为什么?
    Rimifon
        9
    Rimifon  
       2018-09-27 18:58:21 +08:00
    排除 DNS 问题,因为使用 fsocketopen 的时候直接使用 IP 地址也一样要 9 秒。而且只有访问这台服务器的时候后卡这么久,其他服务器正常。代码在本地主机上调试正常。有点感觉对方的服务器阻止了我们服务器的请求一样。但如果被阻止了,为什么浏览器又可以秒开呢?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1471 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 16:51 · PVG 00:51 · LAX 09:51 · JFK 12:51
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.