0

I have this piece of code:-

$finalData  = null;
$url        = trim('https://example.portal.com/staging/api/v1/jobs');
$headers    = array(
                    "Accept:*/*",
                    "xsrf-token:eyJpdiI6IndYd2hkNkJBWGF1OStmRUc4bnllbUE9PSIsInZhbHVlIjoiYk5jdUh1a1lxZTFqK0RPTUxVcnU1UT09IiwibWFjIjoiNGQ5MmVmNjNiOTRjNzkxYjc2MGVkZDRhMWExOTFkOGZmMTE3OWFlZDkxODYyZjk2NDQzOGZhNGZiODcyYWZjNCJ9"
                );
$postType   = 'CURL_POST_DATA_TYPE_X_WWW_FORM_URLENCODE';
$postData   = array(
                'auth_token'    => '30bb573d6a09d2eee2a564d51742252c',
                'version'       => 1,
                'sort_by'       => 'j.title',
                'sort'          => 'desc'
                );
if($postType == 'CURL_POST_DATA_TYPE_X_WWW_FORM_URLENCODE')
{
    $dataString     = '';
    foreach($postData as $keyData => $rowData)
    {
        $dataString .= rawurlencode($keyData) . '=' . rawurlencode($rowData) . '&';
    }
    $postData = trim($dataString, "&");
    if(empty($headers) ||  $headers === null)
    {
        $headers    = array("Content-type:application/x-www-form-urlencoded");
    }
    else
    {
        $headers[]  = "Content-type:application/x-www-form-urlencoded";
    }
}
//$headers[]  = "Content-type:application/json";
$ch             = curl_init();
curl_setopt($ch, CURLOPT_URL, trim($url));
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, TRUE); // remove body
//curl_setopt($ch, CURLOPT_NOBODY, TRUE); // remove body
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 260);
curl_setopt($ch, CURLOPT_TIMEOUT, 260);
$output         = curl_exec($ch);
$httpCode       = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if(curl_errno($ch))
{
    echo 'Curl error: ' . curl_errno($ch) . ' ' . curl_error($ch);
}

curl_close($ch);
if($output !== false)
{
    $finalData  = json_decode($output, true);
}
else
{
    echo "False";
}
//echo $httpCode;
print_r($finalData);

When I run the same code from localhost, it connects to the API and gives me the correct data. However, when I upload the script into the server, I get this message:-

Curl error: 56 TCP connection reset by peer

In the server there are several folders. In one folder say example.portal.com I have many curl scripts which is running fine. However, when I put similar script in other folder say example.test.com, I am getting this error.

What could be the issue?

Saswat
  • 12,320
  • 16
  • 77
  • 156
  • Do any of [these](https://www.google.com/search?q=Curl+error%3A+56+TCP+connection+reset+by+peer) help? – ADyson Mar 29 '23 at 16:15
  • If you can replicate this behaviour consistently, it could be that the target server is rejecting your requests (commonly done for security purposes) – Daniel Mar 29 '23 at 16:18
  • @ADyson already checked on google. No luck. – Saswat Mar 29 '23 at 16:23
  • why disable the SSL parameters and why are you not supplying the cacert.pem as another parameter `CURLOPT_CAINFO`. Also `False` is not really a valid value for `CURLOPT_SSL_VERIFYHOST` – Professor Abronsius Mar 29 '23 at 16:25
  • @Saswat what have you tried, from all that info? – ADyson Mar 29 '23 at 16:29
  • @ProfessorAbronsius what is cacert.pem? I am unaware of this – Saswat Mar 29 '23 at 16:34
  • when you make curl calls to SSL endpoints curl can use a publicly available file that contains many public keys for ssl certificates to aid authentication. You can [download a copy from here](https://curl.haxx.se/docs/caextract.html) - If you want [more info](https://stackoverflow.com/questions/14987857/what-exactly-is-cacert-pem-for) on cacert.pem – Professor Abronsius Mar 29 '23 at 16:39
  • @ProfessorAbronsius thanks for the help Sir. I was unaware. I am grateful for your help. Let me check this. – Saswat Mar 29 '23 at 16:50

0 Answers0