Home > Net >  Why am I getting a timeout error on my curl?
Why am I getting a timeout error on my curl?

Time:11-26

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?

CodePudding user response:

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

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

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

CodePudding user response:

my code which runs some functionality, but sometimes I get a timeout error in my curl, why?

Can you not provide a more detailed description? Like what Error? What works?

Without documentation and such a lean description, there is not much I can do to help.

All I could do is look at what is actually being sent.

BODY=
db=newDatabaseName&user=newDatabaseUser&password=newDatabasePassword&host=serverHost&prefix=newDatabasePrefix&cPUser=me
BODY urldecoded
db=newDatabaseName&user=newDatabaseUser&password=newDatabasePassword&host=serverHost&prefix=newDatabasePrefix&cPUser=me

$_SERVER[\'QUERY_STRING\'])=


argv

$_POST
array (
  \'db\' => \'newDatabaseName\',
  \'user\' => \'newDatabaseUser\',
  \'password\' => \'newDatabasePassword\',
  \'host\' => \'serverHost\',
  \'prefix\' => \'newDatabasePrefix\',
  \'cPUser\' => \'me\',
)
$_GET
array (
)
$_REQUEST
array (
  \'db\' => \'newDatabaseName\',
  \'user\' => \'newDatabaseUser\',
  \'password\' => \'newDatabasePassword\',
  \'host\' => \'serverHost\',
  \'prefix\' => \'newDatabasePrefix\',
  \'cPUser\' => \'me\',
)
$_FILES
array (
)
$_SERVER
array (
  \'CONTENT_LENGTH\' => \'119\',
  \'CONTENT_TYPE\' => \'application/x-www-form-urlencoded\',
  \'GATEWAY_INTERFACE\' => \'CGI/1.1\',
  \'HTTPS\' => \'on\',
  \'HTTP_ACCEPT\' => \'*/*\',
  \'HTTP_X_HTTPS\' => \'1\',
  \'PATH\' => \'/bin:/usr/bin\',
  \'QUERY_STRING\' => \'\',
  \'REDIRECT_STATUS\' => \'200\',
  \'REMOTE_ADDR\' => \'xx.111.132.xxx\',
  \'REMOTE_PORT\' => \'34604\',
  \'REQUEST_METHOD\' => \'POST\',
  \'REQUEST_SCHEME\' => \'https\',
  \'SERVER_PROTOCOL\' => \'HTTP/1.1\',
  \'SERVER_SIGNATURE\' => \'\',
  \'SERVER_SOFTWARE\' => \'Apache\',
  \'SSL_TLS_SNI\' => \'eatled.com\',
  \'TZ\' => \'America/New_York\',
  \'UNIQUE_ID\' => \'Y4D1xtY21afmqR7lMxnX2gAAAAY\',
  \'PHP_SELF\' => \'/receiveheader.php\',
  \'REQUEST_TIME_FLOAT\' => 1669395910.817426,
  \'REQUEST_TIME\' => 1669395910,
  \'argv\' => 
  array (
  ),
  \'argc\' => 0,
)'

This is the code used to get the above:

<?php
header('Content-Type: text/plain; charset=UTF-8');
$decode = false;
foreach (getallheaders() as $name => $value) {
    if(strpos($value,'urlenc')){$decode = true;};
    echo "$name: $value\n";
}
echo "\nBODY=\n";
$body = file_get_contents('php://input');
echo $body;
if($decode){$body = urldecode($body);}
echo "\nBODY urldecoded\n";
echo urldecode($body);
echo "\n\n\$_SERVER['QUERY_STRING'])\n";
echo urldecode($_SERVER['QUERY_STRING']);
echo "\n\nargv\n";
foreach($_SERVER['argv']as $key=>$value){
  echo "\n$key = " . urldecode($value) . "\n";
}
echo "\n\$_POST\n";
var_export($_POST);

echo "\n\$_GET\n";
var_export($_GET);

echo "\n\$_REQUEST\n";
var_export($_REQUEST);

echo "\n\$_FILES\n";
var_export($_FILES);
echo "\n\$_SERVER\n";
var_export($_SERVER);
?>
  • Related