API Data Pull + PHP

Permabanned
Joined
9 Aug 2008
Posts
35,707
I am just learning API's and PHP, Here is an example;

Code:
http://newsapi.org/v2/top-headlines?country=gb&apiKey=api_key_here

Code:
{"status":"ok","totalResults":38,"articles":[{"source":{"id":null,"name":"The Sun"},"author":"Holly Christodoulou","title":"UK coronavirus death toll rises to at least 40,726 after 129 more die in England in 24 hours - The Sun","description":"","url":"http://www.thesun.co.uk/news/11819311/coronavirus-deaths-uk-england-total-daily/","urlToImage":"https://www.thesun.co.uk/wp-content/uploads/2020/06/NINTCHDBPICT000587963546.jpg?strip=all&quality=100&w=1200&h=800&crop=1","publishedAt":"2020-06-09T16:44:07Z","content":"CORONAVIRUS deaths in the UK have risen to 40,883 after 286 more people died - although the true number is likely to be much higher.\r\nPositive Covid-19 infections have also risen by 1,387 to 289,140,… [+3102 chars]"},


There are 38 articles in this API currently. (The one above is the first example from the api). I want to be able to call a random topic using php but it's not working. Does anyone know why? (I could give someone the api key in here if they would be willing to test to see if they could pull data from it.

Code:
<?php
$content =     file_get_contents("http://newsapi.org/v2/top-headlines?country=gb&apiKey=api_key_here");
$result  = json_decode($content);
print_r( $articles->source->name);
?>


Any help would be appreciated, I just need to be able to pull a random topic from the API news headlines if possible..

Example Pull Data From API: https://i.imgur.com/WgaYVZ3.png
 
Associate
Joined
18 Feb 2008
Posts
1,006
You're not accessing the variable you're decoding the json into. Also articles is an array so you need to specify which element you want. i.e to get the first one.

Code:
$result  = json_decode($content);
print_r( $result->articles[0]->source->name);

If you want a random one you can do it the same as you would to get a random element from any other array, i.e:

Code:
$result  = json_decode($content);
$random_el = rand(0, count($result->articles) - 1);
print $result->articles[$random_el]->title;

Hopefully that's what you meant, I didn't test it and haven't used PHP for about 12 years :p
 
Associate
Joined
18 Feb 2008
Posts
1,006
Not sure if this is the 'best practice' for parsing RSS feeds anymore since as I say, my PHP knowledge is very outdated, but as long as the RSS feed is well-formed XML you should just be able to use simplexml_load_string in place of json_decode, i.e.

Code:
<?php
$content = file_get_contents('https://devblogs.microsoft.com/dotnet/feed/');
$result  = simplexml_load_string($content);
$rand = rand(0, count($result->channel->item) - 1);
print $result->channel->item[$rand]->title;
 
Permabanned
OP
Joined
9 Aug 2008
Posts
35,707
:) I hope that works. I’ll test it n let you know shortly. You sure a dab hand at the api stuff.

Ill show you what it’s for shortly.

What would I do if I wanted to only pull the first link + title so it displayed;

.NET Blog

Code:
<?php
$content = file_get_contents('https://devblogs.microsoft.com/dotnet/feed/');
$result  = simplexml_load_string($content);
print_r($result->channel->item[0]->title->link);
 
Last edited:
Permabanned
OP
Joined
9 Aug 2008
Posts
35,707
Edit: I did it it's working :D (not the .net Blog title with link) but to pull the link into the page!

@PaulM - This is the result of the above scripts... it's an IRC Bot to pull certain things into OcUK IRC chat room.

m3j9Etn.png

g2M9XOy.png

fRlTb6G.png

Happy with the results as your scripts work perfectly great! Thank you.
 
Last edited:
Back
Top Bottom