Im trying to get a JSON response from a URL in PHP to just echo it out. But its not working and gives me the error code:
...[function.file-get-contents]: failed to open stream: No error in...
So I googled a bit and find out that maybe allow_url_fopen
in php.ini is switched off but in my case it is turned on (I checked it with phpinfo()).
My Code looks like this:
<?php
$data = file_get_contents('https://newsapi.org/v1/articles?source=bbc-news&sortBy=top&apiKey=myapikey');
echo $data;
?>
A normal response of this site looks like this:
{
"status": "ok",
"source": "bbc-news",
"sortBy": "top",
"articles": [
{
"author": "BBC News",
"title": "Houston reels as Storm Harvey bears down on Louisiana",
"description": "The tropical storm makes landfall in the state after leaving large parts of Houston under water.",
"url": "http://www.bbc.co.uk/news/world-us-canada-41096565",
"urlToImage": "https://ichef.bbci.co.uk/images/ic/1024x576/p05dnlwm.jpg",
"publishedAt": "2017-08-30T14:56:39Z"
},
{
"author": "BBC News",
"title": "Houston's volunteer navy",
"description": "Local volunteers are stepping up to help with search and rescue missions in Houston.",
"url": "http://www.bbc.co.uk/news/av/world-us-canada-41092624/houston-s-volunteer-navy",
"urlToImage": "https://ichef-1.bbci.co.uk/news/1024/cpsprodpb/A77F/production/_97597824_houston-volunteer_300817_cps-bbc-2.jpg",
"publishedAt": "2017-08-30T05:23:59Z"
},
{
"author": "BBC News",
"title": "North Korea: 'Japan missile was first step in Pacific operation'",
"description": "North Korea says a missile fired over Japan was the \"first step\" in military operations in the Pacific.",
"url": "http://www.bbc.co.uk/news/world-asia-41091563",
"urlToImage": "https://ichef.bbci.co.uk/images/ic/1024x576/p05dp42x.jpg",
"publishedAt": "2017-08-30T06:11:24Z"
}, ...
What am I doing wrong ?