Is there something wrong with my cURL code (http status of 0)?

Consistently get status 0, even if I copy and paste the url sent to my browser, I get the json object right back

<?php $mainUrl = "https://api.xxxx.com/?"; $co = "xxxxx"; $pa = "xxxx"; $par = "xxxx"; $part= "xxxx"; $partn = "xxxx"; $us= "xxx"; $fields_string; $fields = array( 'co'=>urlencode($co), 'pa'=>urlencode($pa), 'par'=>urlencode($par), 'part'=>urlencode($part), 'partn'=>urlencode($partn), 'us'=>urlencode($us) ); foreach($fields as $key=>$value) { $fields_string .= $key . '=' . $value . '&' ;} $fields_string = rtrim($fields_string, "&"); $fields_string = "?" . $fields_string; $url = "https://api.xxxxx.com/" . $fields_string; $request = $url; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_TIMEOUT,'3'); $content = trim(curl_exec($ch)); $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); print $url; print $http_status; print $content; ?> 
+7
source share
5 answers

I realized that I have problems with SSL. Just set CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST to false. Working.

+21
source

FYI, you can also get a status code of 0 if the hang time expires before the remote server returns the data. In this case, you need to set the curling time settings to avoid this situation. Just post it to anyone with status 0 issues.

+17
source

I had the same problem, you have to run the curl_exec ($ ch) command before running the curl_getinfo ($ ch) command.

+7
source

try this, you will get positive results. I added CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST to false

 <?php $mainUrl = "https://api.xxxx.com/?"; $co = "xxxxx"; $pa = "xxxx"; $par = "xxxx"; $part= "xxxx"; $partn = "xxxx"; $us= "xxx"; $fields_string; $fields = array( 'co'=>urlencode($co), 'pa'=>urlencode($pa), 'par'=>urlencode($par), 'part'=>urlencode($part), 'partn'=>urlencode($partn), 'us'=>urlencode($us) ); foreach($fields as $key=>$value) { $fields_string .= $key . '=' . $value . '&' ;} $fields_string = rtrim($fields_string, "&"); $fields_string = "?" . $fields_string; $url = "https://api.xxxxx.com/" . $fields_string; $request = $url; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_TIMEOUT,'3'); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false); $content = trim(curl_exec($ch)); $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); print $url; print $http_status; print $content; ?> 
+4
source

You should always set the parameter CURLOPT_VERBOSE when debugging for curling. Your timeout value looks very low.

+3
source

All Articles