9

Consistently getting a status of 0 even though if I copy and paste the url sent into my browser, I get a 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; 



?>
3
  • just an unrelated side note: PHP has a very handy http_build_query function. php.net/manual/en/function.http-build-query.php Commented Dec 31, 2010 at 15:53
  • is https, is there any error returned ? Commented Dec 31, 2010 at 15:54
  • Yup, just realized that checking the error would be helpful. Commented Dec 31, 2010 at 16:13

5 Answers 5

25

Realized that I was having SSL issues. Simply set CURLOPT_SSL_VERIFYPEER and CURLOPT_SSL_VERIFYHOST to false. Works.

Sign up to request clarification or add additional context in comments.

2 Comments

If you wish to use SSL, you can add the CURLOPT_CAINFO flag and point to a certificate. Like 'curl_setopt($curl, CURLOPT_CAINFO, BASE_DIR . '/certs/cacert.pem');'
@Xoc: +1 from me for pointing out that a secure connection is an option here.
16

FYI, you can also get a status code of 0 if the curl connection times out before the remote server returns data. In that case you need to set curl time out options to avoid that situation. Just posting this for anyone else having status 0 problems.

1 Comment

Details on how to set the curl time out are here stackoverflow.com/a/11066378/243233
9

I had the same problem, You MUST run the curl_exec($ch) command before you run the curl_getinfo($ch) command.

1 Comment

Perfect! This was the problem for me!
4

so try this you will get positive results i have 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; 



?>

Comments

3

You should always set the option CURLOPT_VERBOSE when you are debugging for curl. Your timeout value looks very low.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.