cURL请求冻结浏览器

时间:2019-08-27 16:34:44

标签: php curl freeze php-curl

我尝试请求URL宽度cURL,但没有答案,选项CURLOPT_CONNECTTIMEOUT不起作用。

我根本没有答案...我想服务器没有答案。我尝试使用称为Restlet Client-REST API Testing的Chrome插件来测试URL,但也没有答案。


    private function curlInterrogation(){
            try {       
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, WS_L2_URL.$this->code);
                curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
                curl_setopt($ch, CURLOPT_USERPWD, WS_USER . ':' . WS_PASS);
                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
                curl_setopt($ch, CURLOPT_TIMEOUT, 10);
                curl_setopt($ch, CURLINFO_HEADER_OUT, false );
                $result = curl_exec($ch);
                curl_close($ch);
                return $result;
            } catch (Exception $ex) {
                Noyau::setError('Curl error #%d: %s', $e->getCode(), $e->getMessage() );
                return array();
            }
        }

每次运行功能时,我都会冻结该域的浏览器(可以在其他网站上浏览),并且必须重新启动浏览器。

使用CURLOPT_CONNECTTIMEOUT选项,执行应该停止并返回它无法连接到服务器的信息吗?

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

如果您要检查cURL信息,请尝试检查cURL Php Manual

尽管如此,只需检查Php cURL Manual

然后转到您的代码(检查我的评论):

private function curlInterrogation(){
            try {       
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, WS_L2_URL.$this->code);
                curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
                curl_setopt($ch, CURLOPT_USERPWD, WS_USER . ':' . WS_PASS);
                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
                curl_setopt($ch, CURLOPT_TIMEOUT, 10);
                curl_setopt($ch, CURLINFO_HEADER_OUT, false );
                $result = curl_exec($ch);
                curl_close($ch); //You've just closed the execution early before return
                return $result; //Returning just the execution is most likely an empty page
            } catch (Exception $ex) {
                Noyau::setError('Curl error #%d: %s', $e->getCode(), $e->getMessage() );
                return array();
            }
        }

尝试一下

function TestCurl(){ //Just function

        try{
            $base_url = 'https://www.example.com'; //Replace this with your client URL
            $user_name = 'user'; //Replace with username
            $user_pass = 'pass'; //Replace with password
            $ch = curl_init(); //Initializes a new session and return a cURL handle

            curl_setopt($ch, CURLOPT_URL, $base_url);
            curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
            curl_setopt($ch, CURLOPT_USERPWD, $user_name.":".$user_pass);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
            curl_setopt($ch, CURLOPT_TIMEOUT, 10);
            curl_setopt($ch, CURLINFO_HEADER_OUT, false );

            curl_exec($ch);

            $info = curl_getinfo($ch);

            echo 'Took ', $info['total_time'], ' seconds to send a request to ', $info['url'], "\n";

            curl_close($ch); // <- Always put this at the end of the execution
        } 
        catch (Exception $e) {
            echo $e->getMessage();
        }
    }

    //Call TestCurl function
    TestCurl();