I have a web page in json format, whose url is: http://radiojoven.6te.net/teste/now.json.
I’ve been researching ways to extract the information I want, using echo php. I made a code, but I’m not able to extract the information I want, which is "song_name": "MAKIN’ WHOOPEE","artist_name": "YANNICK BOVY".
Attached, I show the code so that you can tell me where my error is:
<?php
$content = file_get_contents("http://radiojoven.6te.net/teste/now.json");
$content = utf8_encode($content);
$result = json_decode($content, true);
$url = $result['zenon']['song_name'];
echo $url;
?>
<?php
$content = file_get_contents("http://radiojoven.6te.net/teste/now.json");
$content = utf8_encode($content);
$result = json_decode($content, true);
$url = $result['zenon']['artist_name'];
echo $url;
?>
Is there any way to remove the remaining "song_name" and "artist_name", I try to use [‘artist_name’][1] (…) with no result.
Thank you very much for your attention and help.
3
Answers
its the other way round?
you have to [1][‘artist_name’] instead of [‘artist_name’][1]?
because you have a list of played entries each has a artist_name
the correct access would be:
I would advise you to use Curl instead of file_get_contents.
If you want to keep using file_get_contents this is the way to do it:
In case you want to switch to Curl you can use this
Note: The code is not checking if there is no song playing at all.
It seems you are trying to access the values using invalid keys. You’re attempting to access
$result['zenon']['song_name']
but the correct path to access these values is$result['now_playing_record'][0]['zenon']['song_name']
for the first record in the array. Try this