How to read json file from url in php/laravel?

Tags: , ,

I want to open a JSON from url in php/laravel file. this is my code :

{{ini_set("allow_url_fopen", 1)}}
{{$id_ = $blog_post->featured_media}}
{{$url_ = ''.$id_}}
{{$data = @file_get_contents($url_)}}
{{$json = @json_decode($data, true)}}

when i try reload page i get this error :

something went wrong

how can i read JSON from url ?


Use cURL to get the json data. Like this

$url = '';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv: Gecko/20080623 Firefox/") ); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result= curl_exec ($ch);
curl_close ($ch);
$info = json_decode($result, true); 
print_r($info); // print all data

Source: stackoverflow