Can't Parse RSS XML with PHP - php

I have some RSS that I am trying to parse from this URL:
http://weather.yahooapis.com/forecastrss?w=12797541
But when I try to parse it using PHP doing this:
$yahoo_response = new SimpleXMLElement($yahoo_url , 0, true);
echo $yahoo_response->rss->channel->item->title;
echo $yahoo_response->rss->channel->item->description;
Nothing gets outputed. Does anyone know what I am doing wrong here? I just need the current forecast bit.
Thanks,
Alex

The root element is <rss>, which is represented by the SimpleXmlElement you loaded into $yahoo_response.
echo $yahoo_response->getName(); // rss
You are trying to do <rss><rss> when you should do:
echo $yahoo_response->channel->item->title;
echo $yahoo_response->channel->item->description;

I've found that RSS feeds are best parsed using a library.
I've had much success with magpieRSS.
But your immediate problem is that you are passing a URL to simplexml, instead of the xml itself.
try $xml = file_get_contents($yahoo_url); first.

Related

Zoopla API - Output Data (XML)

I'm attempting to use the Zoopla API (http://developer.zoopla.com/docs/read/Property_listings) to output specific data.
I have tested the API using a simple echo after the "file_get_contents() method, which shows the data. Example code shown below (API Key Removed)
$url = "http://api.zoopla.co.uk/api/v1/property_listings.xml?postcode=CF11&api_key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$zoopla = file_get_contents($url);
echo $zoopla;
What Im trying to code is a loop that will allow me to add html tags so that I can style them. I've done similar for a RSS feed but can't figure out a way for this XML.
I have also tried an alternative approach using simplexml_load_file()
$xml = simplexml_load_file($url);
$agent_address = $xml->agent_address->agent_address[1]->agent_address;
echo $agent_address;
Any help would be greatly appreciated!
I found the answer to my own question!
Basically the $URL is a string and not a file "simplexml_load_file()"
So first, we need to get the xml file as a string and then parse the file. Code as followed! Works like a treat!
$zoopla = file_get_contents('http://api.zoopla.co.uk/api/v1/property_listings.xml?postcode=CF64&api_key=xxxxxxxxxxxxxxxxx');
$properties = simplexml_load_string($zoopla);
echo $properties->listing[2]->agent_phone;

get data from xml url using php

I am trying to get data from xml url using php but its not working for me. Please check what is wrong in my code.
$result_xml = simplexml_load_file(
"https://maps.googleapis.com/maps/api/geocode/xml?address=UB67JJ"
);
echo $result_xml->GeocodeResponse->result->formatted_address;
I want to get "formatted_address" from xml file.
Don't know where the GeocodeResponse comes from, but it is not in the XML response.
It should just be
echo $result_xml->result->formatted_address;
See https://developers.google.com/maps/documentation/geocoding/
If i remember right, the $result_xml already referes to the outer xml-container, so try:
echo $result_xml->result->formatted_address;

why this error `String could not be parsed as XML` is seen when tried to create a object of simplexmlelement?

I have a functionality like below and getting an error String could not be parsed as XML
$category_feed_url = "http://www.news4u.com/blogs/category/articles/feed/";
$file = file_get_contents($category_feed_url);
$xml = new SimpleXMLElement($file);
foreach($xml->channel->item as $feed)
{
echo $feed->link;
echo $feed->title;
...
why this error has occurred.
The URL points to an HTML document.
It is possible for a document to be both HTML and XML, but this one isn't.
It fails because you are trying to parse not-XML as if it was XML.
See How to parse and process HTML with PHP? for guidance in parsing HTML using PHP.
You seem to be expecting an RSS feed though, and that document doesn't resemble one or reference one. The site looks rather spammy, possibly that URI used to point to an RSS feed but the domain has now fallen to a link farm spammer. If so, you should find an alternative source for the information you were collecting.
"String could not be parsed as XML", your link is an html page.

How to parse this NOAA Weather Alert CAP in PHP?

Greetings,
I am having some difficulty understanding how to parse NOAA's Weather Alert CAP in PHP. I need to do the following:
Locate the proper county in the feed
Verify that there is an active alert
Display the alert's description
The feed I am working with is at this address - http://www.weather.gov/alerts/va.cap
I have used simplexml_load_string() in the past for this sort of thing but it does not seem to work for this feed.
Thanks!
After some more time on Google I came across a script that does exactly what I am trying to do. Rather than try to reinvent the wheel, I am going to go with it. http://saratoga-weather.org/scripts-atom.php#atomadvisory
You are probably having an issue due to the namespace
<cap:alert xmlns:cap='http://www.incident.com/cap/1.0'>
This should give you an idea of how to extract information
$sxe = simplexml_load_file('http://www.weather.gov/alerts/va.cap');
foreach ($sxe->getDocNamespaces() as $ns => $uri) {
$sxe->registerXPathNamespace($ns, $uri);
}
foreach($sxe->xpath('//cap:areaDesc') as $areaDesc) {
echo $areaDesc;
}
On a sidenote, SimpleXml is for simple XML only. Consider using DOM instead.

working with XML in PHP

I have an url return an XML page result. When I use this command:
print_r(file($url));
Its done, but when I use command:
$doc = load($url);
after that I :
print_r($doc);
it out. Its print_r out nothing. I'm quite new in work with XML in PHP someone give advise, please!
Thank you for your attention!
I am not really sure what you trying to do but for parsing an xml file in PHP there two main ways: DOM
$doc = new DOMDocument();
$doc->loadXML(file_get_contents($url));
SimpleXML
$xml = new SimpleXMLElement(file_get_contents($xmlstr));
file_get_contents Reads entire file into a string
#deceze and RageZ:
I'm using load() to get its attribute like this
$url = 'web address return an XML result';
$xml = load($url);
$node1 = $xml->getElmentsByTagName('tagname');
$value = $node1->getAttribute('attribute1');
But I have an error $xml is not an object and I check out by print_r and I get nothing but with print_r(file($url)) its print out an array as I expect!
#Franz: May be I get an error tag in XML file but I could not fixed this just work with the result!
You could also unserialize the xml into a php array and use print_r(array). Take a look here: http://articles.sitepoint.com/article/xml-php-pear-xml_serializer/3#
You will need a PEAR package for this

Categories