Problem displaying "scraped" XML data


Posted on 16th Feb 2014 07:03 pm by admin

I'm basicly having trouble displaying XML data scraped from an URL using cURL.

What the code should do:
Using Curl the code should "grab" the xml data from a specified URL.
It should then "navigate" to <SONGHISTORY><SONGS> where the two key pairs (PLAYEDAT & TITLE) are located.
From there it should allow me to output as many of the key pair results (up to 10) or as little (no lower than 1) as I need.
Eg 1 result would be -> 14:01 50 Cent - G-Unit and so on up to 10 results.

What the code is actually doing:
The code connects and grabs the data fine.
The problem is, it will only output one pair ie played and title.

What I have done and what I need:
With help, I have determined that the reason for this is because I am asigning all the results into one variable, therefore it will only echo out one set of results when called outside the class / function rather than the whole lot.
What I can't work out is how to remidy this.

I also need to work out how to define constant variables instead of using global.
I just don't understand the syntax
I have variables defined in a config file like, $host, $port, $password but the only way I can use them is by using global which I am told is wrong.

The Code:
This is the code Im having trouble with.
Firstly the way I summon my code (which is actually a part built class) and display the results I require in index.php
Some of the variables (as I said earlier) are taken from config.php


$simpleplaying = new simple();
$simpleplaying = $simpleplaying->simplestats();
if($error == 1)
{
echo '<div id="playing"><span class="status_red"> ' . $error_message1. '</span></div>';
}
else
{
echo '<div id="playing"><span class="title">Now Playing: </span><span class="text"> '.
$simple[6].'</span></div>'."n";
echo '<div id="listen_link">-<span class="text"><a href="'. $listen_link .'"
class="listen_link">Click To Listen</a></span></div>';
}


And lastly, the class code itself


class advanced {

function advancedstats()
{
date_default_timezone_set('Europe/London');
//Globals Required
global $host, $port, $admin_password, $error, $played, $title;
//Get Advanced Statistics
$advancedurl = "http://".$host.":".$port. "/admin.cgi?pass=".$admin_password.
"&mode=viewxml&page=0";
$options = array(
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => false, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "Mozilla/5.0", // who am i
CURLOPT_AUTOREFERER => false, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 2, // timeout on connect
CURLOPT_TIMEOUT => 2, // timeout on response
CURLOPT_MAXREDIRS => 0, // stop after 10 redirects
);
$ch = curl_init( $advancedurl );
curl_setopt_array( $ch, $options );
$advsts = curl_exec( $ch );
$err = curl_errno( $ch );
curl_close( $ch );

if(!$err)
{
$xml = new SimpleXmlElement($advsts);
$i=0;
foreach ($xml->xpath('///SONGHISTORY') as $songhistory)
{
foreach($songhistory->SONG as $songs)
{
$i++;
$songs->PLAYEDAT = date("G:i", (int) $songs->PLAYEDAT);
$played = $songs->PLAYEDAT;
$title = $songs->TITLE;
if($i >= $lastplayed)
break;
}
}
}
else
{
$error = 1;
}

}

}


Oh and the reason for this peice of code:

$songs->PLAYEDAT = date("G:i", (int) $songs->PLAYEDAT);


Is because the XML data (PLAYEDAT) comes in the form of a unix time stamp which requires "decoding" for want of a more accurate word.

Also thought I would add that the reason I made this a class is because I am going to be handeling the data it brings back and displaying it in lots of diferent ways, so it was suggested I used a class and pass the data to various functions within that class which would then handle it how I wanted it so I could display it how I wanted it. If that makes sense?

Thanks for looking guys, I hope someone knowledgable can lend a hand.
I've trawled google for hours but when you don't know the technical name for the problems you're having, it's hard finding solutions.
And some of the examples on the net are not much help if you can't work out how they apply to what you are trying to do when you are only a novice

No comments posted yet

Your Answer:

Login to answer
206 Like 43 Dislike
Previous forums Next forums
Other forums

form variables from database help.
Hi all,

I would like to have a form that gives you options based on the results of an mysql q

Effort Estimation Template for Steady State Support
Hi Everyone,

Is there any SAP provided template/format for system change and enhancements

Putting double spaces instead of single spaces
Im looking at trying to replace all single spacing between fields with double spacing

At pres

file downloaded can't be read !!
<?php
$fileName = 'mypic.jpg';
$mimeType = 'image/jpeg';
header('content-dispositio

Php If in MySql query (hiding labels if a field is empty)
Okay, I've been trying to do this for a while, and I'm finally going to ask for help so I can get th

Need a fuction to count entries in a field
Hi
I need a fuction to count how many times a email address is entered in to a field.

I ma

Why does my php page download images over and over
Can anybody please explain to me why my php page keeps downloading the images and other items every

cstdatomic (c++0x std::atomic) / g++ 4.4
Hello,

I'm trying to use cstdatomic (std::atomic in the upcoming c++0x standard) in g++

Help with usergroups? prolly really quick
So were coding our own forums for a game system we made an we have the users level setup but now I n

difference between datetimes
($row['totime']-$row['fromtime'])/60

this is giving me 0

example of totime and fromtim

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash