?
Solved

extract data from a feed

Posted on 2014-10-01
7
Medium Priority
?
154 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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 1000 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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 1000 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 111

Expert Comment

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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 how to count occurrences of each item in an array.
Suggested Courses

770 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