Bikash Nayak
Bikash Nayak

Reputation: 185

How do I handle Warning: SimpleXMLElement::__construct()?

I'm getting this error when I run in local host, if internet is disconnected (if internet is connect its ok) I want to handle this error, "error can show " but want to handle not fatal error break on PHP page.

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]:
  php_network_getaddresses: getaddrinfo failed: No such host is known.
  in F:\xampp\htdocs\shoptpoint\sections\docType_head_index.php on line 30

but I'm trying to handle using try-catch. Below is my code

$apiurl="http://publisher.usb.api.shopping.com/publisher/3.0/rest/GeneralSearch?apiKey=78b0db8a-0ee1-4939-a2f9-d3cd95ec0fcc&trackingId=7000610&categoryId='5855855'";

try{
  new SimpleXMLElement($apiurl,null, true);
}catch(Exception $e){
  echo $e->getMessage();
}

How do I handle the error and my page can execute end of the project?

Upvotes: 4

Views: 13753

Answers (2)

muffir
muffir

Reputation: 447

If for any reason you don't want to set up the error handler you can also use some libxml functions to suppress E_WARNING from raising:


// remembers the old setting and enables usage of libxml internal error handler
$previousSetting = libxml_use_internal_errors(true);

// still need to try/catch because invalid XML raises an Exception
try {
    // XML is missing root node
    new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?>',null, true);
} catch(Exception $e) {
    echo $e->getMessage(); // this won't help much: String could not be parsed as XML
    $xmlError = libxml_get_last_error(); // returns object of class LibXMLError or FALSE
    if ($xmlError) {
        echo $xmlError->message; // this is more helpful: Start tag expected, '<' not found
    }
}

// sets libxml usage of internal error handler to previous setting
libxml_use_internal_errors($previousSetting);

Alternatively you can use libxml_get_errors() instead of libxml_get_last_error() to get all errors. With it you can get all errors regarding parsing the XML as an array of LibXMLError objects.

Some helpful links:

Upvotes: 0

Anthony Sterling
Anthony Sterling

Reputation: 2441

Using set_error_handler, you can do the following to convert any notices/warnings raised by SimpleXMLElement into a catchable Exception.

Take the following:-

<?php
function getData() {
    return new SimpleXMLElement('http://10.0.1.1', null, true);
}

$xml = getData();

/*
    PHP Warning:  SimpleXMLElement::__construct(http://10.0.1.1): failed to open stream: Operation timed out
    PHP Warning:  SimpleXMLElement::__construct(): I/O warning : failed to load external entity "http://10.0.1.1"
    PHP Fatal error:  Uncaught exception 'Exception' with message 'String could not be parsed as XML'
*/

See how we get 2 Warnings before the the Exception from SimpleXMLElement is thrown? Well, we can convert those to an Exception like this:-

<?php
function getData() {

    set_error_handler(function($errno, $errstr, $errfile, $errline) {
        throw new Exception($errstr, $errno);
    });

    try {
        $xml = new SimpleXMLElement('http://10.0.1.1', null, true);
    }catch(Exception $e) {
        restore_error_handler();
        throw $e;
    }

    return $xml;
}

$xml = getData();

/*
    PHP Fatal error:  Uncaught exception 'Exception' with message 'SimpleXMLElement::__construct(http://10.0.1.1): failed to open stream: Operation timed out'
*/

Good luck,

Anthony.

Upvotes: 6

Related Questions