I'm making RSS feed reader with PHP and i get all feed content, but i can't figure out hot to target RSS values so i can echo them, style them, etc!
this is the feed
my script so far:
foreach($feed_list as $from => $feeds) {
$xml = simplexml_load_file($feeds);
echo "<pre>";
print_r($xml);
echo "</pre>";
}
I get this out after code executes
Sorry for trouble, but i found out how
<?php
$rss = simplexml_load_file('http://www.riga.lv/rss/lv/PressRelease/');
?>
<h1><?php echo $rss->channel->title; ?></h1>
<ul>
<?php
foreach($rss->channel->item as $e) {
echo "<li><a href=\"".$e->link['href']."\">";
echo $e->title;
echo "</a></li>\n";
}
?>
Thanks to this :) - Thanks Stack Overflow :)
You need to traverse the $xml object returned and get the values for each single node... check http://www.php.net/manual/en/class.simplexmlelement.php for more info.
Related
Iam trying to loop a XML file with Foreach but doesn't work. There are like 30 reviews in the XML File but only shows one. It shows the first person in the list but then on the bottom.
Iam trying to get better at PHP so dont know allot about it for now.
This is the code that i use.
<?php
$url = 'https://mobiliteit.klantenvertellen.nl/xml/autorijschool-
wezemer%20' or die ('Niet verbonden');
$xml = simplexml_load_file($url);
foreach ($xml as $rijschool){
echo 'Voornaam: '.$rijschool->beoordeling->voornaam.'<br>';
echo 'Achternaam: '.$rijschool->beoordeling->achternaam.'<br>';
echo 'Woonplaats: '.$rijschool->beoordeling->woonplaats.'<br>';
echo 'Beschrijving: '.$rijschool->beoordeling->beschrijving.'<br>';
echo 'Aanbeveling: '.$rijschool->beoordeling->aanbeveling.'<br>';
echo 'Service: '.$rijschool->beoordeling->service.'<br>';
echo 'Deskundigheid: '.$rijschool->beoordeling->deskundigheid.'<br>';
echo 'Prijskwaliteit: '.$rijschool->beoordeling-
>prijskwaliteit.'<br>';
echo 'Gemiddelde: '.$rijschool->beoordeling->gemiddelde.'<br>'.'<br>';
}
?>
Edit: here is the XML file Link https://mobiliteit.klantenvertellen.nl/xml/autorijschool-wezemer%20
And here is what iam getting what current code shows
I think this is what you're trying to do:
<?php
$url = 'https://mobiliteit.klantenvertellen.nl/xml/autorijschool-wezemer%20';
$xml = simplexml_load_file($url);
foreach ($xml->beoordelingen->beoordeling as $rijschool){
echo 'Voornaam: '.$rijschool->voornaam.'<br>';
echo 'Achternaam: '.$rijschool->achternaam.'<br>';
echo 'Woonplaats: '.$rijschool->woonplaats.'<br>';
echo 'Beschrijving: '.$rijschool->beschrijving.'<br>';
echo 'Aanbeveling: '.$rijschool->aanbeveling.'<br>';
echo 'Service: '.$rijschool->service.'<br>';
echo 'Deskundigheid: '.$rijschool->deskundigheid.'<br>';
echo 'Prijskwaliteit: '.$rijschool->prijskwaliteit.'<br>';
echo 'Gemiddelde: '.$rijschool->gemiddelde.'<br>'.'<br>';
}
?>
The problem you are having is that your foreach is iterating over the topmost node, but you want to iterate over a node lower down in the tree.
i'm successfully managing to pull in the information in this feed:
http://api.zoopla.co.uk/api/v1/zed_index?area=yo1&output_type=outcode&api_key=XXXXMYAPIKEYGOESHEREXXXXX
I'm doing this with the following code:
<p><?php $postcode = get_the_title(); $str = urlencode($postcode); $url = "http://api.zoopla.co.uk/api/v1/zed_index?area=$str&output_type=outcode&api_key=5dj2d5x8kd2z2vnk9g52gpap"; $string = file_get_contents($url); echo $string;?></p>
However, this just echos the following output:
DE45 http://www.zoopla.co.uk/home-values/de45 53.258037 53.138911 -1.580861 -1.79776 England Derbyshire 53.198474 -1.6893105 DE45 368929 375424 362103 372926 333441 329349 322644 368056
How could i adapt my existing code to successfully echo individual elements from the feed, for example just the following fields wrapped in tags:
zed_index
zed_index_1year
zed_index_2year
Thanks for your help!
You could use simplexml_load_file() to get an array which will contains every of your XML tags :
<p>
<?php
$XML_url = 'http://api.zoopla.co.uk/api/v1/zed_index?area=yo1&output_type=outcode&api_key=XXXXMYAPIKEYGOESHEREXXXXX';
$XML_parsed = simple_xml_load($XML_url);
// print for debug
echo '<pre>';
print_r( $XML_parsed );
echo '</pre>';
// Access one of the tag
$tagName = $XML_parsed['tagName'];
// Access a nested tag
$nestedTag = $XML_parsed['first_tag']['second_tag'];
?>
</p>
I'm trying to read the xml information that tumblr provides to create a kind of news feed off the tumblr, but I'm very stuck.
<?php
$request_url = 'http://candybrie.tumblr.com/api/read?type=post&start=0&num=5&type=text';
$xml = simplexml_load_file($request_url);
if (!$xml)
{
exit('Failed to retrieve data.');
}
else
{
foreach ($xml->posts[0] AS $post)
{
$title = $post->{'regular-title'};
$post = $post->{'regular-body'};
$small_post = substr($post,0,320);
echo .$title.;
echo '<p>'.$small_post.'</p>';
}
}
?>
Which always breaks as soon as it tries to go through the nodes. So basically "tumblr->posts;....ect" is displayed on my html page.
I've tried saving the information as a local xml file. I've tried using different ways to create the simplexml object, like loading it as a string (probably a silly idea). I double checked that my webhosting was running PHP5. So basically, I'm stuck on why this wouldn't be working.
EDIT: Ok I tried changing from where I started (back to the original way it was, starting from tumblr was just another (actually silly) way to try to fix it. It still breaks right after the first ->, so displays "posts[0] AS $post....ect" on screen.
This is the first thing I've ever done in PHP so there might be something obvious that I should have set up beforehand or something. I don't know and couldn't find anything like that though.
This should work :
<?php
$request_url = 'http://candybrie.tumblr.com/api/read?type=post&start=0&num=5&type=text';
$xml = simplexml_load_file($request_url);
if ( !$xml ){
exit('Failed to retrieve data.');
}else{
foreach ( $xml->posts[0] AS $post){
$title = $post->{'regular-title'};
$post = $post->{'regular-body'};
$small_post = substr($post,0,320);
echo $title;
echo '<p>'.$small_post.'</p>';
echo '<hr>';
}
}
First thing in you code is that you used root element that should not be used.
<?php
$request_url = 'http://candybrie.tumblr.com/api/read?type=post&start=0&num=5&type=text';
$xml = simplexml_load_file($request_url);
if (!$xml)
{
exit('Failed to retrieve data.');
}
else
{
foreach ($xml->posts->post as $post)
{
$title = $post->{'regular-title'};
$post = $post->{'regular-body'};
$small_post = substr($post,0,320);
echo .$title.;
echo '<p>'.$small_post.'</p>';
}
}
?>
$xml->posts returns you the posts nodes, so if you want to iterate the post nodes you should try $xml->posts->post, which gives you the ability to iterate through the post nodes inside the first posts node.
Also as Needhi pointed out you shouldn't pass through the root node (tumblr), because $xml represents itself the root node. (So I fixed my answer).
I'm trying to use Simplepie to filter a feed for items that match a regex keyword filter, and then use those items to create another feed. However, I'm having trouble moving the items from the $matches array into the rss block. I'm still rather a novice with PHP, so maybe I'm missing something obvious, but would appreciate the help.
<?php
$feed = new SimplePie();
$feed->set_feed_url('feed://stackoverflow.com/feeds');
$feed->init();
$feed->set_cache_duration (3600);
$feed->set_timeout(30);
$feed->handle_content_type();
$countItem = 0;
foreach ($feed->get_items() as $item):
$checktitle = $item->get_title();
//Regex keyword filter
$pattern = '/php/i';
//If the there is a keyword match, store in $matches array
if (preg_match($pattern, $checktitle)) {
$matches[$countItem]= $item;
$countItem++;
}
endforeach
?>
<?php if ($success) {
$itemlimit=0;
foreach($matches as $item) {
if ($itemlimit==20) { break; }
?>
//rss block
<item>
<title><?php $item->get_title()); ?></title>
<link><?php echo $item->get_permalink(); ?></link>
<pubDate><?php echo $item->get_date();></pubDate>
<description><![CDATA[<?php echo $item->get_description(); ?>]]></description>
<content:encoded><![CDATA[<?php $item->get_content(); ?>]]></content:encoded>
</item>
<?
$itemlimit = $itemlimit + 1;
}
}
?>
Is your $success being set somewhere? (You did ask if you were missing something obvious! :D)
Are you properly setting the headers, doctype, all that kind of stuff? You might want to use a framework or XML/RSS writer to build the responses, which will be a lot easier than building them manually.
I'm trying to retrieve feeds from a twitter search to display on various parts of the site. I modified this function to do this yet it works for every feed a try it on except twitter.
function getFeed($feed_url) {
$content = file_get_contents($feed_url);
$x = new SimpleXmlElement($content);
echo "<ul>";
foreach($x->channel->item as $entry) {
echo "
<li>
<a href='$entry->link' title='$entry->title'>" . $entry->title . "</a>
</li>";
}
echo "</ul>";
}
I am after only the content of the various associative posts. Here is a sample call.
<?php getFeed("feed://search.twitter.com/search.atom?q=berkshire+golf"); ?>
Any ideas,
Marvellous
Twitter's search API is just a simple HTTP request, so your URL should be http:// and not feed:// if you're using file_get_contents
Edit:
You're also using .atom, use rss instead:
http://search.twitter.com/search.rss?q=berkshire+golf