load xml content from external xml file to php - php

I have written simple code to get content from xml file to php.
$xml = simplexml_load_file("http://localhost/xml_load/test_xml.xml");
print_r($xml);
First time It has run successfully but right now it is giving me warning and not executing properly.
Warning: simplexml_load_file(http://localhost/xml_load/test_xml.xml) [function.simplexml-load-file]: failed to open stream: HTTP request failed! <?xml version="1.0" encoding="ISO-8859-1"?> in C:\xampp\htdocs\XML_load\load_file.php on line 2
Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http://localhost/xml_load/test_xml.xml" in C:\xampp\htdocs\XML_load\load_file.php on line 2

No file found on your respective location. Now, either check if your file exist. However, do try to make your local path doesn't based over BASEURL. Add that path further like
$xml = simplexml_load_file("xml/files/myxml.xml"); // PATH TO YOUR FILE.
echo "<pre>"; print_r($xml); "</pre>";

Related

[function.simplexml-load-file]: I/O warning : failed to load external entity (cakephp)

I have some part of codes that they do some work on a xml file.
they work perfectly out of cakephp.
but when i put it in my framework it not work. "simplexml_load_file" function in my framework cant find the xml file .but the xml file is in the same folder which the class caller is in it!
code :
$xmlObj = simplexml_load_file("certificate.xml");
error :
Warning (2): simplexml_load_file() [function.simplexml-load-file]: I/O
warning : failed to load external entity "certificate.xml"
[APP\libs\r_s_a_processor.php, line 17]
I cant set complete path like "http://www.sample.com/app/libs/certificate.xml" beacuse htaccess change the path to the 404 page.
what is the solution?
I even put the xml file in the webroot/files path! but nothing changed :(
Try to echo gwtcwd(); just before, you'er sure that's the right directory

simplexml_load_file: failed to open stream: HTTP request failed

I am trying to display rss feeds for this link. http://vuconnect.com/controls/cms_v2/components/rss/rss.aspx?sid=1643&gid=2&calcid=664&page_id=13
But it is showing this error
Warning:
simplexml_load_file(http://vuconnect.com/controls/cms_v2/components/rss/rss.aspx?sid=1643&gid=2&calcid=664&page_id=13):
failed to open stream: HTTP request failed! in
G:\wamp\www\scripts\rss_feeds\demo-1.php on line 48
Here is the code i am using.
$url = 'http://vuconnect.com/controls/cms_v2/components/rss/rss.aspx?sid=1643&gid=2&calcid=664&page_id=13';
$xml = simplexml_load_file($url);
echo '<pre>';
print_r($xml->channel);
die;
?>
needs allow_url_fopen to be On.
Check you php.ini and change it to On, if it is Off.
I hope this solves your problem

PHP XML Get Error

I wrote a PHP Application (that works) and I have moved it to a different directory. As a result it needs to get a file from a directory above it, but the file must remain hidden, ie:
Root
--config
---users.xml (Permissions 600)
--lib
----loginUtil.php
I have tried two different methods of getting the file:
$xml = file_get_contents("../config/users.xml");
Which returns the following Error:
[13-Jun-2012 08:54:04] PHP Warning: file_get_contents(../config/users.xml) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: No such file or directory in public_html/test/lib/loginUtil.php on line 18
and
$xml = simplexml_load_file("../config/users.xml");
which returns
[13-Jun-2012 08:56:17] PHP Warning: simplexml_load_file() [<a href='function.simplexml-load-file'>function.simplexml-load-file</a>]: I/O warning : failed to load external entity
I have tried using the full URL, and that works, but I must then make the users.xml file public (as PHP is doing a URL Request).
What can I do to resolve this issue? (Thanks for your help)
try setting the full path and not the relative
$path = '/var/full/path_dir/file.xml';
if(!is_file($path)){
// if this is the is output then you need to check permissions
// double check the file exists also.
// check the web service can read the file
echo "FILE NOT FOUND FOR $path";
}
else{
$xml = file_get_contents($path);
echo $xml;
}

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O
warning : failed to load external entity
"USD_en_productdata/USD_en_productdata.xml"
the code
$src=simplexml_load_file("USD_en_productdata/USD_en_productdata.xml");
foreach($src->ProductItem as $i){
}
Try to pass full directory path if you are trying to load xmls held at your server
simplexml_load_file($_SERVER['DOCUMENT_ROOT'].'/uproYourFoldet/USD_en_productdat/USD_en_productdata.xml')
or if you want to access xml by http protocol you will need to set allow_url_fopen ON in php.ini or
ini_set('allow_url_fopen ','ON');
in your code. or you can also do this if you are using php version <5
$temp = file_get_contents($url);
$XmlObj = simplexml_load_string($temp);
Looks like your path might not be correct.
In my experience, it's best to surround simplexml_load_file and subsequent functions with if statements with file_exists(...) as the condition.
So in your case:
if(file_exists($_SERVER['DOCUMENT_ROOT'].'/yourPathToFile/...')) {
simplexml_load_file($_SERVER['DOCUMENT_ROOT'].'/urltoYourFolder/USD_en_productdat/USD_en_productdata.xml')
}
EDIT: spelling

YouTube API implementation with SimpleXML no longer working

I had a website that pulled videos from a user's account using a public XML feed. The feed's URL is:
https://gdata.youtube.com/feeds/api/users/USERNAME/uploads
Where USERNAME is dynamically assigned via a PHP variable. The code to hit this URL and grab the contents is:
$file = file_get_contents('https://gdata.youtube.com/feeds/api/users/'.$channel.'/uploads');
$xml = simplexml_load_file($file);
However, this is now throwing a weird error:
Warning: file_get_contents() [function.file-get-contents]: Couldn't
resolve host name in /home/dpw/public_html/inc/modules/youtube.php on
line 46
Warning:
file_get_contents(https://gdata.youtube.com/feeds/api/users/dpwrestling/uploads)
[function.file-get-contents]: failed to open stream: operation failed
in /home/dpw/public_html/inc/modules/youtube.php on line 46
Warning: simplexml_load_file() [function.simplexml-load-file]: I/O
warning : failed to load external entity "" in
/home/dpw/public_html/inc/modules/youtube.php on line 47
Warning: Invalid argument supplied for foreach() in
/home/dpw/public_html/inc/modules/youtube.php on line 49
I'm not sure what the problem is, as visiting the URL in my web browser returns the XML fine. Why is my PHP script now having trouble retrieving it?
For the record, the allow_url_fopen is still enabled in my hosting environment as I checked using echo ini_get('allow_url_fopen'), which printed 1.

Categories