gpt4 book ai didi

php - 检查是否用simplexml_load_string加载了xml

转载 作者:行者123 更新时间:2023-12-03 07:55:29 25 4
gpt4 key购买 nike

我正在用php这样查询xml文件:

 public function trackOrderAction()
{
$request = Mage::getResourceModel( 'order/request' );
$request->setOrder($this->getRequest()->getParam('increment_id'));
$response = $request->submit(true);
$xml = simplexml_load_string($response);
$items = count($xml->OrderItems->OrderItem);
}

xml尚未立即准备好,因此,如果人们在准备好之前尝试使用该功能,则会出现错误,因为它试图获取非对象的属性。我的问题是检查xml响应以查看是否有任何内容并停止该功能(如果没有)的正确方法是什么?

我尝试了一些简单的事情
    if (empty($xml)){
die();
} else {
$items = count($xml->OrderItems->OrderItem);
}

但这无济于事。关于如何检查是否已加载xml的任何想法?

最佳答案

http://us.php.net/manual/en/function.simplexml-load-string.php

Returns an object of class SimpleXMLElement with properties containing the data held within the xml document. On errors, it will return FALSE.


 public function trackOrderAction()
{
$request = Mage::getResourceModel( 'order/request' );
$request->setOrder($this->getRequest()->getParam('increment_id'));
$response = $request->submit(true);
$xml = simplexml_load_string($response);
if ( !$xml ) {
return false;
}
$items = count($xml->OrderItems->OrderItem);
}

如果出现错误,它将返回false。因此,如果 simplexml_load_string失败,则立即失败并返回false。否则,请继续执行其余功能。切勿在函数中使用 die()

关于php - 检查是否用simplexml_load_string加载了xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8300495/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com