Solved

extract data from a feed

Posted on 2014-10-01
7
151 Views
Last Modified: 2014-10-02
Hi all,
I have been given a url that shows a live stream of "what's playing" from a radio station.
The feed can be seen here http://brian1.streamon.fm/card
The data contains info such as what's playing now, the album cover of what's playing now, artist etc as well as what's playing next.
I can see the data on the page however Id like to exrtact some of the information as variables so I can echo them on the page of the radio stations website (preferably using php however d be open to java solutions).
Could anyone assist?
Cheers,
N
0
Comment
Question by:Neil_Bradley
  • 3
  • 2
  • 2
7 Comments
 
LVL 9

Expert Comment

by:Brian Tao
ID: 40356415
The content is actually a JSON string.  You can use json_decode function to extract the data into either an array or an object

$given_url = "http://brian1.streamon.fm/card";
$json_str = file_get_contents($given_url);
// parse it into an array
$result_array = json_decode($json_str, true);
echo "To Array:\n";
print_r($result_array);
// or parse it into an object
$result_obj = json_decode($json_str);
echo "\nTo Object:\n";
print_r($result_obj);

Open in new window

0
 
LVL 5

Author Comment

by:Neil_Bradley
ID: 40356428
Thanks for that however it prints all of the data.
I only need:
the title of the song playing now,
the artists
album name
album cover
How would I go about extracting only that info and declaring them as simple variables.
Cheers,
N
0
 
LVL 5

Author Comment

by:Neil_Bradley
ID: 40356509
Would it be simpler to convert the JSON string into xml first then extract the data from that?
N
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Accepted Solution

by:
Brian Tao earned 250 total points
ID: 40356624
No, the data is already extracted as array elements.  To display what you intended:
$given_url = "http://brian1.streamon.fm/card";
$json_str = file_get_contents($given_url);

// parse it into an array
$result_array = json_decode($json_str, true);

$title = $result_array["title"];
$artist = $result_array["artist"];
$album = $result_array["album"];
$album_cover_url = $result_array["album_art"]["src"];

echo "Now playing: $title<br>\n";
echo "The artists: $artist<br>\n";
echo "Album Name: $album<br>\n";
echo "<img src='$album_cover_url'>";

Open in new window

0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 40356637
Would it be simpler to convert the JSON string into xml first then extract the data from that?
No, and you can't reliably do this conversion.  XML has data in string format only, whereas JSON provides for boolean and numeric values, etc.  The JSON data format is curated here: http://json.org

Please see: http://iconoun.com/demo/temp_neil_bradley.php

<?php // demo/temp_neil_bradley.php
error_reporting(E_ALL);
echo '<meta charset="utf8" />';
echo '<pre>';

// ACQUIRE THE JSON STRING AND MAKE AN OBJECT
$url = 'http://brian1.streamon.fm/card';
$jso = file_get_contents($url);
$obj = json_decode($jso);

// ACTIVATE THIS TO SEE THE ENTIRE OBJECT
// var_dump($obj);

// GET SOME OF THE PROPERTIES OF THE OBJECT
$t = (string)$obj->title;
$p = (string)$obj->publisher;
$a = (string)$obj->album_art->src;
echo PHP_EOL . "$t FROM $p";
echo PHP_EOL . '<img src="' . $a . '" />';

Open in new window

0
 
LVL 5

Author Closing Comment

by:Neil_Bradley
ID: 40358377
Thanks to you both for your input and the advice re xml.
The solutions you offered both worked extremely well and solved the problem perfectly.
All the best.
Neil
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 40358423
Glad to help -- thanks for the points, ~Ray
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question