SimplePie Getting an invalid XML document - 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.

Related

SimplePie through CraftCMS adding #? to feed URL

Okay - simple details first:
Feed URL: http://feeds.wired.com/wired/index
When I access this directly through simplepie and not CraftCMS, the feed loads without issue. When I access it through the CMS template system it throws an error saying the returned XML is effectively garbage. Looking into this, its because the feed host is returning a page not found.
I have dug through the code to find out why it is a page not found on a valid URL and found that in the file:
/library/simplepie/registry.php
public function &create($type, $parameters = array()) (around line 160)
Now in this function I see that a new reflection class is created and new InstanceArgs are sent the parameters. At this point the Feed URL gets changed from:
http://feeds.wired.com/wired/index
to
http://feeds.wired.com/wired/index?#
Effectively this new URL with the added ?# on the end is causing the feed site to return a page not found. And hence all my errors...
This is beginning to hit the end of my abilities - and I would like to know what is causing the ?# to be added and if there is a way to prevent it? Or any other possible solutions to my problem.
I can't explain why it works outside of Craft and not inside, but it definitely looks like a SimplePie bug to me.
Just created an issue and pull request with the fix for them (https://github.com/simplepie/simplepie/issues/366) and will see about updating it in Craft itself.
Update: looks my fix didn't broke some of their tests, so we won't be putting that into Craft, but they have acknowledged it needs to be fixed.

php simple_load_string always returning zero data on well formed xml docs

I have been trying to load a number of XML files into a PHP page using a simple load string. Strange as this might sound, not one loads, and all return as empty.
I can load them using $xmldoc = new DomDocument( '1.0' ); and I can view all directly in the browser. I'm getting really frustrated because I can't see why they should fail to load.
I am using offline development - Win7 proff, XAMPP Version: 1.8.1 with PHP 5.4 loaded.
Example of my simple loading code:
$xml = simplexml_load_string('menu45.xml');
if ( !$xml ) {
echo "empty!!!!";
// return 'Error';
}
I am hoping someone will let me know if there is a problem with using a simple load string on a Windows machine or if there's a bug in PHP 5.4, or XAMPP has issues with it, or something.
I've Google'd and read everything I could for an 18 hour stint without joy. Any suggestion would be greatly appreciated.
simplexml_load_string() loads strings, not files.... and "menu45.xml" isn't a well formed xml string. Try simplexml_load_file() instead.

Solr for PHP: getDigestedResponse not working

I've managed to install Solr for PHP on my Windows 7 64bit Machine using the plugin I found here:
downloads.php.net/pierre/
It was linked to on this site:
wiki.apache.org/solr/SolPHP
(links are not clickable because I'm a new user)
I've got everything up and running, searches and indexing are working, but only when I use the getRawResponse() Method and parse it through SimpleXml (http://de.php.net/manual/en/book.simplexml.php).
The getDigestedResponse() method, which ist supposed to return a PHP-Object, just returns string(1) " ".
The method getResponse() (http://docs.php.net/manual/en/solrresponse.getresponse.php) just times out.
It wouldn't be that much of a problem, but some of the XML from the Raw Response doesn't seem to be valid and parsed with simpleXML, some of the attributes are missing, using regular expressions to get the needed data would be too much of a hassle.
Has anyone get this to work yet? Help is greatly appreciated!
Depends on how you are parsing the response. Try code below and drop the PHP/PECL solr libs and go CURL (ex: hostNameHere:8983/solr/select/?q=solr&start=0&rows=10&indent=on and send the result XML to the function below).
If you can access a resource (solr) via a URL, then there is no need to use an ancillary library to do what CURL can do:
function makeSimpleXML($xml) {
$dom = new DOMDocument;
$dom->loadXML($xml);
if (!$dom) {
// ErrorUtility::throwFatal("could not parse xml. please check the format", "XMLParisng Error");
}
return simplexml_import_dom($dom);
}

simplexml_load_file problems while browsing securely

I'm getting a lot of errors when I browse a clients site securely that have to do with the simplexml_load_file function. Here's an example:
Warning: simplexml_load_file() [function.simplexml-load-file]: https://xxxxxxxx/settings.xml:1: parser error : Document is empty in /xx/xx/xx/xx/xx/ on line 0
The site is on a dedicated server from SingleHop. It only happens when I browse the site with https://, and works fine when browsed with http://.
Seems to be loading the XML file just fine with https:// as well:
https://consumerstrust.org/wp-content/plugins/easyfanpagedesign/framework/settings.xml
The XML is parsed from a class:
public function efpd_load_settings($xmlfile){
$xmlparse=simplexml_load_file($xmlfile);
$settings=array();
$setint=0;
foreach($xmlparse->option as $xml){
$option[$setint]=(array)$xml;
array_push($settings,$option[$setint]);
$setint++;
}
return $settings;
}
and ran like this:
$efpdxml=plugins_url('settings.xml',__FILE__); // plugins_url() is a WP function - returns the value just fine.
$efpdsettings=Efpd::efpd_load_settings($efpdxml);
Is this something that happens commonly? Also anything to fix it? If you need any more info to help me solve this just let me know and I will provide it.
Thanks.
I just confirmed file_get_contents works just fine.
$content = file_get_contents("https://consumerstrust.org/wp-content/plugins/easyfanpagedesign/framework/settings.xml");
if ( empty($content) ) {
die('XML is empty');
}
$xml = simplexml_load_string($content);
Edit
Using simplexml_load_file with https URL also worked for me. This is only an alternative.
I figured out my own problem, but thank you to those of you who tried helping me. I appreciate it.
My solution:
This was being run in WordPress, so I really should have posted this in the WPSE site, but I don't blame anyone for not knowing all about WP from this site if they didn't.
The function plugins_url() should not have been used to define the path to the XML file, I should have been using dirname(__FILE__).'/settings.xml' - which is pretty obvious as to why if you know how plugins_url() works, I overlooked it completely but in the end solved my problem.

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.

Categories