我的代码是:
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
];