I am trying to read JSON from a URL and I can't get cURL to retrieve the data.
I have the following PHP:
$curl = curl_init($url);
$options = array(
CURLOPT_URL => $url,
CURLOPT_HEADER => false,
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_TIMEOUT => 5,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER =>array('Accept: application/json', "Content-type: application/json"),
CURLOPT_FAILONERROR => FALSE,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_SSL_VERIFYHOST => FALSE
);
curl_setopt_array($curl, $options);
$response = curl_exec($curl);
print "curl_error returned: ".curl_error($curl)."
<br />
";
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
print "Status of HTTP: ".$status."
";
$response = json_decode($response);
print_r($response)
The problem is that this returns as:
curl_error returned: couldn't connect to host
Status of HTTP: 0
The JSON data I am reading is about 300 characters total, and is not formatted (does this matter?) What I mean by not formatted is that the JSON is all on one line.
I tried using fopen() and file_get_contents(), but that did the same thing (returned nothing).
Another problem I am encountering is the time it takes to connect. When I visit the URL in browser it renders the JSON immediately, but when I use cURL (or file_get_contents() for that matter) it takes a very long time to complete. That's why I specifically limited the timeouts. If I let the time outs go with not limit, the page renders after about 2 minutes with the same results as with the time out.
I'm not sure how I can read this URL now. To give a description of the URL contents, it is literally just JSON data rendered on one line (and it's not a large file, it's actually fairly small). If it matters, there is some 'Get' information in the URL.
Thanks for any help.
Someone suggested that this might be a firewall/connectivity issue, but from my end I am able to send requests to other websites and check their status codes accurately. On the URL's end, I am able to access it through my browser...?