Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何处理警告:SimpleXMLElement::_construct()?_Php_Xml - Fatal编程技术网

Php 如何处理警告:SimpleXMLElement::_construct()?

Php 如何处理警告:SimpleXMLElement::_construct()?,php,xml,Php,Xml,我在本地主机上运行时遇到这个错误,如果internet断开连接(如果internet连接正常),我希望处理这个错误,“错误可以显示”,但希望处理PHP页面上的非致命错误中断 Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: php_network_getaddresses: getaddrinfo failed: No such host is known. in F:\xampp\htdoc

我在本地主机上运行时遇到这个错误,如果internet断开连接(如果internet连接正常),我希望处理这个错误,“错误可以显示”,但希望处理PHP页面上的非致命错误中断

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
但我正在尝试使用try-catch来处理。下面是我的代码

$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();
}

如何处理错误,我的页面才能执行项目结束?

使用set\u error\u handler,可以执行以下操作,将SimpleXMLElement引发的任何通知/警告转换为可捕获的异常

采取以下行动:-

<?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'
*/

如果出于任何原因您不想设置错误处理程序,您也可以使用一些libxml函数来禁止
E\u警告
引发:


//记住旧设置并启用libxml内部错误处理程序的使用
$previousSetting=libxml\u use\u internal\u errors(true);
//仍然需要尝试/捕获,因为无效XML引发异常
试一试{
//XML缺少根节点
新的SimpleXMLElement(“”,null,true);
}捕获(例外$e){
echo$e->getMessage();//这不会有多大帮助:字符串无法解析为XML
$xmlError=libxml_get_last_error();//返回类LibXMLError或FALSE的对象
如果($xmlError){

echo$xmlError->message;//这更有帮助:需要开始标记,'这是警告而不是异常,因此您无法捕获它。如果您实现自定义
set\u error\u处理程序
,您可以根据需要将其转换为异常。您好,Anthony,好的,您是对的,但是,是否可以处理警告错误?我们可以自定义显示吗?
<?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'
*/