Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anything wrong with my cURL code (http status of 0)?

Tags:

php

curl

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; 



?>
like image 819
Ilya Avatar asked Dec 31 '10 15:12

Ilya


2 Answers

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

like image 141
Ilya Avatar answered Sep 20 '22 17:09

Ilya


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.

like image 42
Henry Avatar answered Sep 19 '22 17:09

Henry