Solved

extract data from a feed

Posted on 2014-10-01
7
153 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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

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 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 count occurrences of each item in an array.
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 …

705 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