Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing cURL to get JSON data from a URL
    text
    copied!<p>I am trying to read JSON from a URL and I can't get cURL to retrieve the data.</p> <p>I have the following PHP:</p> <pre><code>$curl = curl_init($url); $options = array( CURLOPT_URL =&gt; $url, CURLOPT_HEADER =&gt; false, CURLOPT_CONNECTTIMEOUT =&gt; 5, CURLOPT_TIMEOUT =&gt; 5, CURLOPT_RETURNTRANSFER =&gt; true, CURLOPT_HTTPHEADER =&gt;array('Accept: application/json', "Content-type: application/json"), CURLOPT_FAILONERROR =&gt; FALSE, CURLOPT_SSL_VERIFYPEER =&gt; FALSE, CURLOPT_SSL_VERIFYHOST =&gt; FALSE ); curl_setopt_array($curl, $options); $response = curl_exec($curl); print "curl_error returned: ".curl_error($curl)."\n &lt;br /&gt;\n"; $status = curl_getinfo($curl, CURLINFO_HTTP_CODE); print "Status of HTTP: ".$status."\n"; $response = json_decode($response); print_r($response) </code></pre> <p>The problem is that this returns as:</p> <pre><code>curl_error returned: couldn't connect to host Status of HTTP: 0 </code></pre> <p>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.</p> <p>I tried using fopen() and file_get_contents(), but that did the same thing (returned nothing).</p> <p>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.</p> <p>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.</p> <p>Thanks for any help.</p> <p>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...?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload