【问题标题】:cURL request freeze the browsercURL 请求冻结浏览器
【发布时间】:2023-03-10 10:05:01
【问题描述】:

我尝试请求 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 选项,执行是否应该停止并返回它无法连接到服务器?

感谢您的帮助

【问题讨论】:

  • 你确定 curl_exec 是执行被冻结的地方吗?如果返回的数据太大,处理它们可能需要一些时间,并且也会冻结 chrome 插件。
  • 您为什么不尝试使用 Postman 应用程序而不是 Chrome 中的 Advanced Rest Client?这是一个更好的工具来查看您的请求发生了什么,并且如果响应需要很长时间才能到达,它不会冻结。
  • 是的,我确定 curl_exec 是执行冻结的地方。当我评论它时,没有冻结...
  • 我也必须告知,当服务器给出答案时,一切都像魅力一样。我的问题是只有当服务器没有回答时......
  • @Fabiano 如你所说,我尝试使用 postam。同样的问题,服务器没有响应,超时不起作用......

标签: php curl freeze php-curl


【解决方案1】:

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

不过,请查看Php cURL Manual

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

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();




【讨论】:

  • 我不明白你的建议。在 cURL Php 手册中,有几个在 curl_close() 之后返回的示例。如果不是,我如何管理来自 curl 的数组接收数据?
  • 就像我在另一条评论中所说:当服务器给出答案时,一切都像魅力一样。我的问题是只有当服务器没有回答时......
猜你喜欢
  • 2012-05-29
  • 1970-01-01
  • 2014-10-02
  • 1970-01-01
  • 2016-01-11
  • 2013-03-19
  • 2020-09-16
  • 1970-01-01
相关资源
最近更新 更多