为什么我在curl上得到超时错误?

  • 本文关键字:超时 错误 curl php curl
  • 更新时间 :
  • 英文 :

我的代码是:
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中得到超时错误,为什么?

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

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

需要在所有代码行中执行。

作为POST请求,你应该提供一个数组

$data = [
'db' => $this->newDatabaseName,
'user' => $this->newDatabaseUser,
'password' => $this->newDatabasePassword,
'host' => $this->serverHost,
'prefix' => $this->newDatabasePrefix,
'cPUser' => $cPUser
];

最新更新