Solved

extract data from a feed

Posted on 2014-10-01
7
141 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
Comment Utility
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
Comment Utility
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
Comment Utility
Would it be simpler to convert the JSON string into xml first then extract the data from that?
N
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 9

Accepted Solution

by:
Brian Tao earned 250 total points
Comment Utility
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 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
Comment Utility
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
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
Glad to help -- thanks for the points, ~Ray
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now