I'm trying to parse the Last.fm feed of my last 10 tracks played onto my website.
This is what I have so far,
<?php
$doc = new DOMDocument();
$doc->load('http://ws.audioscrobbler.com/1.0/user/nathanjmassey/recenttracks.xml');
$arrFeeds = array();
foreach ($doc->getElementsByTagName('track') as $node) {
$itemRSS = array (
'artist' => $node->getElementsByTagName('artist')->item(0)->nodeValue,
'name' => $node->getElementsByTagName('name')->item(0)->nodeValue,
'url' => $node->getElementsByTagName('url')->item(0)->nodeValue,
);
array_push($arrFeeds, $itemRSS);
}
?>
<?php
foreach ($arrFeeds as $i => $values) {
foreach ($values as $key => $value) {
print "<p>$value\n</p>";
}
}
?>
This basically gives me all 10 tracks in the feed in the format,
Linkin Park
In Between
http://www.last.fm/music/Linkin+Park/_/In+Between
But I need to format the results in list of links such as,
$artist - $track
How would I extend my script to achieve this?
For your output, use this:
<?
foreach ($arrFeeds as $i => $values)
{
print "<a href='" . $values['url'] . "'>" . $values['artist'] . " - " . $values['name'] . "</a>";
}
?>
UPDATE: How to limit # of parsed items
(Responding to the comment via edit so I can use the code display tags.)
I'm at work at the moment, but I'd try changing your initial parsing code something like so:
array_push($arrFeeds, $itemRSS); // existing line
if (count($arrFeeds) >= 5) { break; } // add this line
Related
I'm trying to parse an RSS feed in PHP for the first time. It seems to go fine until I actually try to display anything! This example is me trying to pull out four random organization names from the feed (I actually want to display more, but am keeping it simple here...)
$xml = file_get_contents('https://rss.myinterfase.com/rss/oxford_RSS_Jobs_xml.xml');
foreach($xml->Row as $job) {
$item[] = array(
'OrganizationName' => (string)$job->OrganizationName,
'job_JobTitle' => (string)$job->job_JobTitle,
'job_expiredate' => strtotime($job->job_expiredate),
'ExternalLink' => $job->ExternalLink
);
}
$rand_job = array_rand($item, 4);
$i=0;
echo '<ul>';
while($i<=3) {
echo '<li>';
echo $item[$i]['OrganizationName'];
echo '</li>';
$i++;
}
echo '</ul>'
What do I need to do differently? Thanks!
You have to use simplexml_load_file($url); or similar.
$url = 'https://rss.myinterfase.com/rss/oxford_RSS_Jobs_xml.xml';
$xml = simplexml_load_file($url);
foreach($xml->row as $job) { // be sure about $xml->row. If it's full path to this elements
//..... your code
}
I've got a list of "sites" in an SQL database that I'd like to display on a web page. I need to create a div ID for every site. For the time being I'm doing this (fetch the table into a $_SESSION array, get and display the name of the sites in a loop and do an echo):
<div id='leftnavigation1' class="leftnavigation">
<ul>
<?php
$maxKeys = max(array_keys($_SESSION['usersmeter'])); //check how many sites are in the usersmeter table
for ($i = 0; $i <= $maxKeys; $i++) { //loop every key in the array
$siteName = $_SESSION['usersmeter'][$i]['siteName'];
echo "<li id='siteID$i'><a href='#'><span>" . $siteName . "</span></a></li>"; //display the name of the sites
}
?>
</ul>
</div>
It works well but I think it is a bad practice to include php code into the View.
How else can I do? An AJAX in Javascript ?
Thanks,
Like that you will br try:
<?php
$my_array = array(
'GSU4300' => 'Lili Markina',
'GSU4301' => 'John Kokina',
'GSU4304' => 'Bill Clinong',
'GSU4305' => 'Obamark Chiko'
);
echo '<ul id="somethig">';
foreach ($my_array as $k => $v) {
echo '<li id="' . $k . '">' . $v . '</li>';
}
echo '</ul>';
?>
I am trying to display items from an XML feed on a page
This is my code...
$feed = 'http://awebsite.co.uk/directory/rssfeed.php?thecondition=(title%20LIKE%20'%british%'%20OR%20description%20LIKE%20'%british%')';
$xml = simplexml_load_file("$feed");
if ( $xml->channel->item !='' ) {
foreach ($xml->channel->item as $item) {
echo '<li>' . $item->title . '</li>';
}
}
else { echo 'None'; }
However, due to the complicated feed URL it is giving lots of errors.
Note: I have changed the domain in the URL for this example.
How can I make this work?
Can't judge anything without seeing the XML source first but if it's a standard RSS channel file you should be iterating through your items something like this:
$items = $xml->xpath('/channel/item');
foreach ($items as $item) {
echo '<li>' . $item->title . '</li>';
}
Also the initial condition: if ( $xml->channel->item !='' ) {/*...*/} isn't really going to work.
I'm accessing a search API that gives json result like: Free Search API
I want to retrieve only the records title, kwic, and url in the object results into my code. But the title and url in the object related are standing in the way.
I have tried doing some if function:
foreach ($json->results as $item) {
if (isset($item->kwic)) {
$rss_item = array(
'title' => $item->title,
'kwic' => $item->kwic,
'url' => $item->url,
);
array_push($desArray, $item->kwic);
}
else {
return false;
}
array_push($rss_array, $rss_item);
for ($i = 0; $i < 50; $i++) {
echo '' . $rss_array[$i]['title'] . '' . '<img src="http://corrupteddevelopment.com/wp-content/uploads/2012/10/open-new-tab-window-icon.jpg" width="15px" height="15px"><br/>';//LINE 66
echo '<hr/>';
echo '<p>' . $rss_array [$i]['kwic'] . '</p></br>';
}
It gave me partly results, and partly : Notice: Undefined offset: 31 in C:\xampp\htdocs\MSP\SignIn\cariFree.php on line 66. When I wrote:
if (isset($item->related)) {
return false}
It gave me entirely blank page.
What have I missed?
Thank you.
Here is the screen-shoot of the full code: mycode.php
I rewrote your code a bit, I think that this is what you actually should be doing, but I'm not sure since I dont have the entire code and don't really understand what your problem is (apart from bad coding).
<?php
//first off use arrays instead of objects:
$json = json_decode($data, true);
foreach ( $json['results'] as $item ) {
if ( isset($item['kwic']) ) {
//we can push $item into the rss_array directly
array_push($rss_array, $item); #I assume you want this here instead of outside the loop
}
//I removed the else since it seemed completly stupid
}
foreach ( $rss_array as $item ) {
echo '' . $item['title'] . '' . '<img src="http://corrupteddevelopment.com/wp-content/uploads/2012/10/open-new-tab-window-icon.jpg" width="15px" height="15px"><br/>';//LINE 66
echo '<hr/>';
echo '<p>' . $item['kwic'] . '</p></br>';
}
Is there a easy way to separate the output from SimpleXML into pages? Let's say the XML file got 200 elements but I want to split it into 20 elements on each page. So it would be like index.php?page=1?
$xml = new SimpleXMLElement(file_get_contents("demofile.xml"));
$per_no = $xml->children();
echo count($per_no) . ' are now active<br />';
$cnt = 0;
foreach ($xml->xpath('/webcams_online/webcam') as $node) {
if ($cnt == 10) {
break;
}
$itemXML = array(
'account' => $node['account'],
'nickname' => $node['nickname'],
'number_visitors' => $node['number_visitors']
);
$cnt++;
?>
<?php echo $itemXML['account']; ?>
<?php
}