Issue:
When making an API call with
$json = file_get_contents($jsonurl);
You get the following error message:
file_get_contents(your.api.call) request failed to open stream: HTTP request failed!
Description: In my case the above function works perfectly well when the php file is called from a web browser but return most of the time the aforementioned error message when run through cron.
Solution:
Use curl to call the api.
$curl_handle=curl_init(); curl_setopt($curl_handle, CURLOPT_URL,$jsonurl); curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2); curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl_handle, CURLOPT_USERAGENT, 'YOUR APP NAME'); $json = curl_exec($curl_handle); curl_close($curl_handle);