Solved

extract data from a feed

Posted on 2014-10-01
7
149 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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 109

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 109

Expert Comment

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

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
This article discusses how to create an extensible mechanism for linked drop downs.
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)
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

808 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