0

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 ?

Ahmet K
  • 713
  • 18
  • 42
  • I'd really suggest using `curl`, as it's easier to configure and debug and much less buggy than `file_get_contents`. – aynber Aug 30 '17 at 15:46
  • I imagine it might be because it is an HTTPS request. Have you tried the answer from https://stackoverflow.com/questions/1975461/how-to-get-file-get-contents-to-work-with-https – Matt Rink Aug 30 '17 at 15:46
  • thats the answer I get: "openssl: no http wrapper: yes https wrapper: no wrappers: array ( 0 => 'php', 1 => 'file', 2 => 'data', 3 => 'http', 4 => 'ftp', 5 => 'compress.zlib', 6 => 'zip', )". Do I have to download curl ? – Ahmet K Aug 30 '17 at 15:50

2 Answers2

0

You're going to need CURL. Try this. Don't forget to include your api key! You can just edit $url.

$url = 'https://newsapi.org/v1/articles?source=bbc-news&sortBy=top';    
$ch = curl_init($url);
curl_setopt($ch,CURLOPT_USERAGENT,$_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json_response = curl_exec($ch);

echo($json_response);
Blaise
  • 330
  • 1
  • 11
-1

file_get_contents error:

Use double quote when passing query string

<?php

  $myapikey = "yourAPIKeyGoesHere";


   $data = file_get_contents("https://newsapi.org/v1/articles?source=bbc-news&sortBy=top&apiKey=$myapikey");

   echo $data;
 ?>

See a live working demo of your code here

Prince Adeyemi
  • 724
  • 6
  • 12