Php failed to load external entity simplexml load file

How to Fix «simplexml_load_file(): I/O warning: failed to load external entity ‘. ‘» PHP Warning?

If you see the following error/warning when using the simplexml_load_file() function:

Warning: simplexml_load_file(): I/O warning : failed to load external entity ". " in /path/to/file.php on line .

Then it means that you have likely provided the XML you wish to load from a file as a string to the simplexml_load_file() function argument (instead of the path to that XML file). It could, for example, happen in the following case:

$xmlStr = file_get_contents('/path/to/file.xml'); $xml = simplexml_load_file($xmlStr); // .

In the example above, using the file_get_contents() function returns a string of loaded XML. When you provide that as an argument to the simplexml_load_file() function, it throws a warning because it is expecting the path of the XML file as an argument and not a string. To solve this, you can do any of the following:

Use the XML String With SimpleXMLElement Object Instance

You could, for example, use file_get_contents() to first load the XML file into a string, and then provide that as an argument to the SimpleXMLElement object instance. For example:

$xmlStr = file_get_contents('/path/to/file.xml'); $xml = new SimpleXMLElement($xmlStr); // .

Load the XML File Directly With simplexml_load_file()

You could directly provide the XML file path to the simplexml_load_file() function, and it will return a SimpleXMLElement object instance on success (and false on failure). For example:

$xml = simplexml_load_file('/path/to/file.xml'); // .

Use the XML String With simplexml_load_string()

You could, for example, use file_get_contents() to first load the XML file into a string, and then provide that as an argument to the simplexml_load_string() function — which would interpret the string of XML into a SimpleXMLElement object instance on success (and return false on failure). For example:

$xmlStr = file_get_contents('/path/to/file.xml'); $xml = simplexml_load_string($xmlStr); // .

Hope you found this post useful. It was published 26 Aug, 2022 . Please show your love and support by sharing this post.

Читайте также:  Numbers patterns in java

Источник

PHP simplexml_load_file — error handling

I tried this but got a warning message, so I simply did $xml = @simplexml_load_file($url); and no warnings and handles error as expected – bestprogrammerintheworld Mar 9 ’16 at 9:17 , 2 mere-teresa, but he needs to suppress the warning here. Of course, you can also use the display_errors setting or convert errors to exceptions and then use try/catch, but this is much simpler. – Ignas R Aug 20 ’09 at 16:28 ,The documentation says that in the case of an error, simplexml_load_file returns FALSE. So, you can use the «shut-up» operator (@) in combination with a conditional statement:, 2 You can do it without calling simplexml_load_file twice: $xml = @simplexml_load_file(«. «); if ($xml) < . >else < return 'Error'; >– Ignas R Aug 20 ’09 at 16:26

I thinks this is a better way

$use_errors = libxml_use_internal_errors(true); $xml = simplexml_load_file($url); if (false === $xml) < // throw new Exception("Cannot load xml source.\n"); >libxml_clear_errors(); libxml_use_internal_errors($use_errors); 

Answer by Ariella Bradley

Dealing with XML errors when loading documents is a very simple task. Using the libxml functionality it is possible to suppress all XML errors when loading the document and then iterate over the errors. , The libXMLError object, returned by libxml_get_errors(), contains several properties including the message, line and column (position) of the error. , Basic SimpleXML usage ,libxml_use_internal_errors()

Failed loading XML Blank needed here parsing XML declaration: '?>' expected Opening and ending tag mismatch: xml line 1 and broken Premature end of data in tag broken line 1 

Answer by Dakota Day

and getting these errors when the stream cannot connect:, php — xml — simplexml ,How can I handle these errors so I can display a user friendly message instead of what is shown above?,I am using the following code:

I am using the following code:

function GetTwitterAvatar($username)< $xml = simplexml_load_file("http://twitter.com/users/".$username.".xml"); $imgurl = $xml->profile_image_url; return $imgurl; > function GetTwitterAPILimit($username, $password)< $xml = simplexml_load_file("http://$username:[email protected]/account/rate_limit_status.xml"); $left = $xml->; $total = $xml->; return $left."/".$total; > 

and getting these errors when the stream cannot connect:

Warning: simplexml_load_file(http://twitter.com/users/****.xml) [function.simplexml-load-file]: failed to open stream: HTTP request failed! HTTP/1.0 503 Service Unavailable Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http://twitter.com/users/****.xml" Warning: simplexml_load_file(http://[email protected]/account/rate_limit_status.xml) [function.simplexml-load-file]: failed to open stream: HTTP request failed! HTTP/1.0 503 Service Unavailable Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http://***:***@twitter.com/account/rate_limit_status.xml" 

Answer by Henrik Baker

I am using the following code:,I’ve found a nice example in the php documentation.,and getting these errors when the stream cannot connect:,http:// wrapper is disabled in the server configuration by allow_url_fopen=0

I am using the following code:

function GetTwitterAvatar($username)< $xml = simplexml_load_file("http://twitter.com/users/".$username.".xml"); $imgurl = $xml->profile_image_url; return $imgurl; > function GetTwitterAPILimit($username, $password)< $xml = simplexml_load_file("http://$username:[email protected]/account/rate_limit_status.xml"); $left = $xml->; $total = $xml->; return $left."/".$total; > 

and getting these errors when the stream cannot connect:

Warning: simplexml_load_file(http://twitter.com/users/****.xml) [function.simplexml-load-file]: failed to open stream: HTTP request failed! HTTP/1.0 503 Service Unavailable Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http://twitter.com/users/****.xml" Warning: simplexml_load_file(http://[email protected]/account/rate_limit_status.xml) [function.simplexml-load-file]: failed to open stream: HTTP request failed! HTTP/1.0 503 Service Unavailable Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "http://***:***@twitter.com/account/rate_limit_status.xml" 

Answer by Ana Kerr

I have a routine to access the webservice of the mails that uses simplexml_load_file, but if for some reason the post office website is out of order or if it takes too long to respond to my site it returns an error, then I would like to handle this error of loading the simplexml_load_file, so that I can present a personalized message to the user, tried in some ways more unsuccessfully, follow my current code: , This should work here, but if you want something more robust I suggest using a third party component that loads xml. , Note: Here he is throwing an exception when he has an error, but if it is not necessary, he is only adptar for his needs. , An important note about the code snippet above is that the finally will always run after a try catch. If you do not want this behavior, just remove block finally

try < $xml = simplexml_load_file($url) >catch (Exception $e) < echo 'Exceção capturada: ', $e->getMessage(), "\n"; > finally

Answer by Ryder Nguyen

Another quick and drity thing to do is just tell PHP to hide errors:,Just remember to handle the error in your code after simplexml_load_file fails — user vardump got the right idea.,Use @simplexml_load_file() or error_reporting(0).,Is there a way to just hide the whole thing in case of an error?

$xmlResult = file_get_contents($strURL); if ($xmlResult === false) < // File dosn't exist :( >try < $xml = new SimpleXMLElement($xmlResult); >catch(Exception $exception) < // XML failed to parse :( >

Answer by Alma Shaffer

Is it possible to catch simplexml file errors? I’m connecting to a webservice that sometimes fails, and I need to make the system skip a file if it returns some http error or something similar.,On error, your simplexml_load_file should return false.,As for HTTP errors, handling those will depend on how you’re connecting to the webservice and retrieving the data.,If you’re not interested in error reporting or logging when the webservice fails you can use the error supression operator:

If you look at the manual, there is an options parameter:

SimpleXMLElement simplexml_load_file ( string $filename [, string $class_name = "SimpleXMLElement" [, int $options = 0 [, string $ns = "" [, bool $is_prefix = false ]]]] ) 

This is the correct way to suppress warnings:

$xml = simplexml_load_file('file.xml', 'SimpleXMLElement', LIBXML_NOWARNING); 

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drush 8.x: simplexml_load_file(): I/O warning : failed to load external entity «» Project.php:72 #1437

Drush 8.x: simplexml_load_file(): I/O warning : failed to load external entity «» Project.php:72 #1437

Comments

Latest Drush 8.x with PHP 5.6.7 (Debian Jessie, Apache2, fresh Drupal 7 minimal install) causes

simplexml_load_file(): I/O warning : failed to load external entity "" Project.php:72 Failed to get available update data from https://updates.drupal.org/release-history/YOURMODULE/7.x 

suddently in the last days when trying to fetch modules like with drush dl. No way out. XML files testing from Drupal.org are ok. Connection seems not officially 100% secure (user reports) but this issue seems not to be connected with this one. Testing with Drush 7.x and php 5.4 DOES NOT cause this. Required php modules are fine.

The text was updated successfully, but these errors were encountered:

thx Moshe! awesome, fast and helpful decision and referencing to the point. And it has confirmed my assumptions. I will try with phpbrew to switch php version and will report back if it helps.

A workaround is to disable certificate checks in wget. For that, just put check_certificate=off in $HOME/.wgetrc

While this should not be recommended for production server enviroments with data sensitive structure, I would take that as a very helpful advice for testing and development enviroment situations, which are the most case anyway when you install modules randomly. Many thanks for the fast replies: @jonhattan @weitzman

Seeing this bug as «simplexml_load_file(): I/O warning : failed to load external entity «» Project.php:42″. Confirming that check_certificate=off in $HOME/.wgetrc works as a workaround.

However, as per the other ticket, I thought that because updates.drupal.org had installed a wildcard SSL cert we’d no longer be seeing this error. Hmm.

Here is command for newbies: echo «check_certificate=off» > ~/.wgetrc , and also confirm this workaround works.

No longer happenning, AFAIK. Please post in #894 if still having problems.

check_certificate=off worked for me. Thanks

Got this error using drush dl drupal (drush 9.0, drupal 8), because I forgot to install xml extension:
apt-get install php7.0-xml
fixed it.
(Posting in case someone else made my mistake in 2016.)

using drush 7.4.0 , php 5.6.x I had the same problem,
the solution was to follow instructions in #894

of course I had to uncomment the line
$command_specific[‘pm-download’][‘source’] = ‘http://updates.drupal.org/release-history’;

Drush 8.1.7
PHP 5.6.16
Mac Sierra 10.12.3 (ie no wget)
(running on Postgres 9.4.5)

Had this problem for a long time. It went away for a while, now its back again. I updated nothing.

$ drush rl simplexml_load_file(): I/O warning : failed to load external entity [warning] «» Project.php:74 Failed to get available update data from [error] https://updates.drupal.org/release-history/drupal/7.x No valid projects given.

$ drush up drupal Update information last refreshed: Never Name Installed Version Proposed version Message drupal Unknown Unknown Specified project not found

If I try curl https://updates.drupal.org/release-history/drupal/7.x
I get
$ curl https://updates.drupal.org/release-history/drupal/7.x curl: (60) SSL certificate problem: unable to get local issuer certificate More details here: http://curl.haxx.se/docs/sslcerts.html curl performs SSL certificate verification by default, using a «bundle» of Certificate Authority (CA) public keys (CA certs). If the default bundle file isn’t adequate, you can specify an alternate file using the —cacert option. If this HTTPS server uses a certificate signed by a CA represented in the bundle, the certificate verification probably failed due to a problem with the certificate (it might be expired, or the name might not match the domain name in the URL). If you’d like to turn off curl’s verification of the certificate, use the -k (or —insecure) option.

So something is off with the Drupal SSL cert for the updates?

To fix this locally I added a ~/.curlrc file with just insecure in it.
Not a good fix, but gets me working.

Источник

Оцените статью