dream fly
dream fly

Reputation: 1

Why am I getting a timeout error on my curl?

My code is:

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;
}

After creating an account on HostBill, I use my code which runs some functionality, but sometimes I get a timeout error in my curl. Why?

Upvotes: 0

Views: 433

Answers (1)

Markus Zeller
Markus Zeller

Reputation: 9090

Your string interpolation is totally wrong. Object properties must be enclosed within curly braces.

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

It needs to be done in all code lines.

As it is a POST request, you should provide an array:

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

Upvotes: 1

Related Questions