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.
Related
I used the following code to parse the HTML of another site but it display the fatal error:
$html=file_get_html('http://www.google.co.in');
Fatal error: Call to undefined function file_get_html()
are you sure you have downloaded and included php simple html dom parser ?
You are calling class does not belong to php.
Download simple_html_dom class here and use the methods included as you like it. It is really great especially when you are working with Emails-newsletter:
include_once('simple_html_dom.php');
$html = file_get_html('http://www.google.co.in');
As everyone have told you, you are seeing this error because you obviously didn't downloaded and included simple_html_dom class after you just copy pasted that third party code,
Now you have two options, option one is what all other developers have provided in their answers along with mine,
However my friend,
Option two is to not use that third party php class at all! and use the php developer's default class to perform same task, and that class is always loaded with php, so there is also efficiency in using this method along with originality plus security!
Instead of file_get_html which not a function defined by php developers use-
$doc = new DOMDocument();
$doc->loadHTMLFile("filename.html");
echo $doc->saveHTML(); that's indeed defined by them. Check it on php.net/manual(Original php manual by its devs)
This puts the HTML into a DOM object which can be parsed by individual tags, attributes, etc.. Here is an example of getting all the 'href' attributes and corresponding node values out of the 'a' tag. Very cool....
$tags = $doc->getElementsByTagName('a');
foreach ($tags as $tag) {
echo $tag->getAttribute('href').' | '.$tag->nodeValue."\n";
}
P.S. : PLEASE UPVOTE IF YOU LIKED MY ANSWER WILL HELP MY REPUTATION ON STACKOVERFLOW, THIS PEOPLES THINK I'M NOOB!
It looks like you're looking for simplexml_load_file which will load a file and put it into a SimpleXML object.
Of course, if it is not well-formatted that might cause problems. Your other option is DomObject::loadHTMLFile. That is a good deal more forgiving of badly formed documents.
If you don't care about the XML and just want the data, you can use file_get_contents.
$html = file_get_contents('http://www.google.co.in');
to get the html content of the page
in simple words
download the simple_html_dom.php from here Click here
now write these line to your Php file
include_once('simple_html_dom.php');
and start your coading after that
$html = file_get_html('http://www.google.co.in');
no error will be displayed
Try file_get_contents.
http://www.php.net/manual/en/function.file-get-contents.php
I am using a very cool php library(whatever it is called) called SimplePie. I am using the latest version.
I have this code:
$url = 'http://www.seobook.com/feeds.shtml';
$SimplePieFeed->set_feed_url($url);
$SimplePieFeed->force_feed(true);
$SimplePieFeed->enable_order_by_date(true);
$success = $SimplePieFeed->init();
if( !$SimplePieFeed->error() ) {
foreach( $SimplePieFeed->get_items() as $item ) {
......
}
} else {
print_r( $SimplePieFeed->error() );
}
Why is it that when I run this code I'm getting this kind of error:
This XML document is invalid, likely due to invalid characters. XML error: not well-formed (invalid token) at line 8, column 76
I try to run this one on Simplepie's demo and everything is going well. Why is it that when I run it on my end i'm having that kind of error? Is it because of a cache? I noticed that Simplepie is storing feeds in a cache. I have tried $SimplePieFeed->enable_cache(false); but still i'm getting that error. I'm not even sure if that's related to that kind of error. LOL!
Your help would be greatly appreciated and rewarded! :Thank you very much!
Simple there is problem in your xml file you should remake the .xml file,if you are using wordpress simple use plugin called google site maps its pretty good.
If you using some thing else like php, or html base site you should make valid xml document maker like some listed here,
xml-sitemaps.com
xmlgrid.net (editor viewer)
web-site-map.com
May it help you little in your case.
I'm working on getting my new website up and I cannot figure out the best way to do some parsing.
What I'm doing is trying to parse this webpage for the comments (last 3) the "whats new" page, permissions page, and the right-bar (the one with the ratings etc).
I have looked at parse_url and a few other methods, but nothing is really working at all.
Any help is appreciated, and examples are even better! Thanks in advance.
I recommend to use the DOM to this job, here it is an example to fetch all the urls in a web page:
$doc = new DOMDocument();
$doc->loadHTMLFile('http://www.theurlyouwanttoscrape.com');
foreach( $doc->getElementsByTagName('a') as $item){
$href = $item->getAttribute('href');
var_dump($href);
}
Simple HTML DOM
I use it and it works great. Samples at the link provided.
parse_url parses the actual URL (not the page the URL points to).
What you want to do is scrape the webpage it is pointing to, and pick up content from there. You would need to use fopen, which will give you the HTML source of the page and then parse the HTML and pick up what you need.
Disclaimer: Scraping pages is not always allowed.
PHP SimpleXML extension is your friend here: http://php.net/manual/en/book.simplexml.php
Are there any good, free ways to do this?
I found http://fonefinder.net, which looks okay. If that's my best best, how can I query it with a phone number and get the returned carrier? (I don't see an API).
Well, it looks like your query URL is as follows:
http://www.fonefinder.net/findome.php?npa={First Three}&nxx={Next Three}&thoublock={Last Four}
I would just get that page, use PHP's XML parser on the document. This should get you started:
<?php
$xmlDOC = simplexml_load_file(/* Your Request URL */);
print_r($xmlDOC->center->table[1]->tbody->tr[1]->td[4]->a->attributes());
?>
There is this website http://phonenumberprovider.com, for some reason it doesn't have a public API yet, but the developer told me that I can use this at the moment:
http://phonenumberprovider.com/api/json/+187605591648
Returns JSON like this:
{"result":"success","description":"Phone number provider found","operator":"JAMAICA","operatorPrefix":"1876","operatorCountry":"JM"}
I wanted to use PHP Simple HTML DOM Parser to grab the Google Apps Status table so I can create my own dashboard that will only include Google Mail and Google Talk service status, as well as change the presentation (html,css).
As a test, I wanted to find/output the table element but it's not displaying any results.
$html = file_get_html('http://www.google.com/appsstatus');
$e = $html->find("table", 0);
echo $e->outertext;
Although, if I find/output the div elements it will display results.
$html = file_get_html('http://www.google.com/appsstatus');
$e = $html->find("div", 0);
echo $e->outertext;
Any help would be much appreciated.
It's way easier than that. All of this data is tied up in a JSON feed.
http://www.google.com/appsstatus/json/en
On a simple level, you could do a file_get_contents() of that, knock that bit off the front that says dashboard.jsonp, and then to a json_decode() (doc), and you will have yourself a nice array with all the information you'd ever want to know about Google's service status. Dump it with print_r() to see where everything is.
Finding these types of things is super easy with Fiddler. I highly recommend it.