I've seen different versions of this question asked but nothing that specifically answered mine.
I'm trying to parse this Rss feed that pulls the results from a search on a Pet adoption site and turns it into a RSS/Atom feed.
<?php
//RSS solution
$feed = simplexml_load_file('http://www.serverstrategies.com/rss.php?sid=WV87&len=20&rand=0&drop_1=');
$children = $feed->children('http://www.w3.org/2005/Atom');
echo $children->entry[1]->item[0]->title;
?>
I've tried a lot of different variations of this but I've yet to get anything to print out.
Hope this solution will be helpful in getting all the titles and descriptions.
<?php
$feed = simplexml_load_file('http://www.serverstrategies.com/rss.php?sid=WV87&len=20&rand=0&drop_1=');
$result=array();
foreach($feed->channel->item as $node)
{
$result[]=array("title"=>(string)$node->title,
"description"=> strip_tags((string)$node->description));
}
print_r($result);
Related
I'm all new to twig and I got involved in this project where I'm forced to figure a solution out. And that's why I had to ask this question here.
I want to display the videos on a youtube playlist which is easy enough when it comes to pure PHP and the use of (for example) simplexml_load_file($feed).
$xml = simplexml_load_file($feed);
foreach ($xml->entry as $key => $x) {
$title = $x->title;
$id = $x->children('yt', True)->videoId;
$uri = $x->author->uri;
$desc = $x->children('media', True)->group->description;
}
But since I know little to nothing about twig I can't really get an understanding how to do the similar thing. Or if it is even possible.
I know there are many questions here about DOM traversal with XPATH. I have done a good amount of research before bringing my question here, but I am still having an issue. I'm trying to pull the number of downloads for a given app on the android market. So for instance if the app were the stack exchange app, I would want to pull the numbers: 50,000 - 100,000 from this page:
https://play.google.com/store/apps/details?id=com.stackexchange.marvin
I am attempting to target the div with an itemprop of "numDownloads" to little avail. I have no trouble targeting other items on page I have tried (various classes, etc) but this specific item never returns results. I have checked to make sure the value is, in fact, in the source and not being inserted by JS. Here is my code:
// Load up the document so we can parse the dom
$dom = new DomDocument();
$dom->loadHTML($this->html);
// XPath so we can do some specific searches
$finder = new DomXPath($dom);
// Find all the number of downloads item on page
$installs = $finder->query("//*[#itemprop='numDownloads']");
echo "<pre>"; var_dump($installs); echo "</pre>";
foreach($installs as $install) {
echo "<pre>"; var_dump($install->nodeValue); echo "</pre>";
}
Any suggestions would be greatly appreciated!
Actually you are already on the right track.
$url = 'https://play.google.com/store/apps/details?id=com.stackexchange.marvin';
$contents = file_get_contents($url);
$dom = new DOMDocument();
#$dom->loadHTML($contents);
$finder = new DomXPath($dom);
$installs = $finder->query("//div[#itemprop='numDownloads']");
// directly point it to a div since it is a div
foreach($installs as $install) {
echo $install->nodeValue; // 50,000 - 100,000
}
So lets say i have a google news feed, like this: https://news.google.com/news/feeds?pz=1&cf=all&ned=no_no&hl=no&q=%22something%22&output=atom&num=1
Grabbing the title, author and link would be easy, but how would i go around getting say the first 200 characters of the content? its full of html, and mixed in with the title and author aswell.
i could use strip_tags on it, but it would still be a mess.
Any way to make google return a ['description'] maybe?
or is there perhaps any other good news feeds that gives me the content in a way thats easier to manage?
[edit]
Update on how i ended up doing it.
$news = #simplexml_load_string(file_get_contents('https://news.google.com/news/feeds?pz=1&cf=all&ned=no_no&hl=no&q=%22molde+fotballklubb%22+OR+%22tornekrattet%22+OR+%22mfk%22+OR+%22oddmund+bjerkeset%22+-%22moss%22&output=atom&num=1'), 'SimpleXMLElement', LIBXML_NOCDATA);
$data = get_object_vars($news->{'entry'});
$test = explode('<font size="-1">', $data['content']);
$link = get_object_vars($data['link']);
$return['title'] = strip_tags($test[0]);
$return['author'] = strip_tags($test[1]);
$return['description'] = strip_tags($test[2]);
$return['link'] = $link['#attributes']['href'];
It is still not working properly, but thats because the feed gives me the content in different ways all the time. Sometimes the content of the news article itself will just be metadata like the authors and image descriptions.
And the breaking it up by html tags when the html have changes from time to time causes some problems. But i cant figure out any othe way of doing it with this feed.
You could try loading the HTML in a DOMDocument instance and extract the parts you need, or use a wrapper for it like Goutte which makes it a lot easier to extract portions you need.
http://php.net/manual/en/class.domdocument.php
https://github.com/fabpot/Goutte
Hello I would like to get the XML feed for my site:
http://buildworx-mc.com/forum/syndication.php?fid=4&limit=5
And display them in this format:
<ul>
<li> Topic 1 </li>
<li> Topic 2 </li>
<li> Topic 3 </li>
</ul>
I guess the best/easiest method to do this is using PHP, so how can I get the XML and display them in list items? It'll be displayed on http://example.com while the feed is http://example.com/forum
I tried the other answers from other questions asked, but nothing seems to work for me.
You may need to use the command "file_get_contents" to get a copy of the remote file in order to parse it using PHP. I'm surprised this step is necessary since you say you want to display items from your forum on your site so you might be able to set the 'feed' variable to the direct link, assuming everything is under the same domain. If not, this ought to work.
$feed = file_get_contents('http://buildworx-mc.com/forum/syndication.php?fid=4&limit=5');
$xml = simplexml_load_string($feed);
$items = $xml->channel->item;
foreach($items as $item) {
$title = $item->title;
$link = $item->link;
$pubDate = $item->pubDate;
$description = $item->description;
echo $title . "<br>";
// continue to format as an unordered list
}
You may try SimpleXML once you are using PHP:
http://php.net/manual/en/book.simplexml.php
Just load that URL, so that it will convert the XML file into an object:
http://www.php.net/manual/en/function.simplexml-load-file.php
Then you may iterate the objects with a simple "foreach", to generate that HTML list you want.
For testing purposes, and to understand how the object is created, you may use "print_r()".
I'm trying to use an xml file and retrieve all links from it. so far I have:
$xmlHeadline = $xml->channel[0]->item[3]->link;
print($xmlHeadline);
This works ok to print the single headline link for item[3] in the xml. But as you can see it is 2 levels deep. Then next one will be at channel[0]->item[4]->link. There is no channel 1, just channel[0].
All the examples on the internet only deal with 1 level deep. They all used a foreach loop, but I am not sure if that can be used here...
How can I cycle through all item's in the xml and echo all links?
Try
foreach ($xml->channel[0]->item as $item) {
echo $item->link;
}
or
foreach ($xml->xpath('/channel/item/link') as $link) {
echo $link;
}
I think you want a DOM parser, that will allow you to load the xml as a structured hierarchy and then use a function like getElementById http://php.net/manual/en/domdocument.getelementbyid.php to parse the xml and get the specific items you want at any depth.
If you provide the structure of the xml file I may be able to help with the specific use of a DOM function.
$str = '<channels><channel>
<item><link>google.com</link></item>
<item><link>google.com.cn</link></item>
<item><link>google.com.sg</link></item>
<item><link>google.com.my</link></item>
</channel></channels>';
$xml = simplexml_load_string($str);
$objs = $xml->xpath('//channel/item/link');
PS: Please include some example of your xml