【问题标题】:Why am I getting a timeout error on my curl?为什么我的 curl 会出现超时错误?
【发布时间】:2022-11-25 18:31:13
【问题描述】:

我的代码是:

public function sendPostData()
    {
        $url = "http://$this->cPdomain/$this->serverScriptFile";
        $cPUser = $this->cPanel->user;
        $data = "db=$this->newDatabaseName&user=$this->newDatabaseUser&password=$this->newDatabasePassword&host=$this->serverHost&prefix=$this->newDatabasePrefix&cPUser=$cPUser";

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        // curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 20);

        // Receive server response ...
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $server_output = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        $responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $curlData = [
            'url' => $url, 
            'cPUser' => $cPUser, 
            'data' => $data,
            'HTTP Error' => $responseCode,
            'HTTP code' => $httpCode
            ];
        $this->setLog($curlData, 'curl_data.txt');
        
        if ($server_output === false) {
            $this->setLog("CURL Error: " . curl_error($ch), 'curl_err.txt');
            return 0;
        }
        
        curl_close ($ch);

        return 1;
    }

在 Hostbill 上创建帐户后,我使用运行某些功能的代码,但有时我的 curl 会出现超时错误,为什么?

【问题讨论】:

    标签: php curl


    【解决方案1】:

    你的字符串插值是完全错误的。对象属性必须用大括号括起来。

    $url = "http://{$this->cPdomain}/{$this->serverScriptFile}";
    

    需要在所有代码行中完成。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-27
      • 2022-06-15
      • 2013-02-11
      • 2020-08-20
      • 1970-01-01
      • 2020-11-04
      • 1970-01-01
      相关资源
      最近更新 更多