php使用curl模拟异步请求,毫秒级超时设置

技术探讨  2019-04-24 21:12   8356 curl php异步

    使用curl请求设置超时模拟php异步请求:

    /**
     * 1、curl 异步请求不需要等待返回值(秒级)
     * 
     * @param  [type] $url [description]
     * @return [type]      [description]
     */
    function curl_request_noreply ($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 1);    // 秒级
        $content = curl_exec($ch);
        curl_close($ch);
        // echo $content;
    }
    
    
    /**
     * 2 、 curl 异步请求不需要等待返回值(毫秒级)
     * 
     * @param  [type] $url [description]
     * @return [type]      [description]
     */
    function curl_request_noreply ($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_NOSIGNAL, true);   // 注意,毫秒超时一定要设置这个  
        curl_setopt($ch, CURLOPT_TIMEOUT_MS, 100);  // 超时时间200毫秒
        $content = curl_exec($ch);
        curl_close($ch);
        // echo $content;
    }


注:转载请注明出处为http://www.www.xtaike.com/article/98.html。

沙豆网 站长

追求卓越,奋斗不息!

167
文章
8438
点赞

更多文章