当前位置:Gxlcms > PHP教程 > 一个接口,能得到返回的xml文件,如何提取有用的内容?

一个接口,能得到返回的xml文件,如何提取有用的内容?

时间:2021-07-01 10:21:17 帮助过:18人阅读

接口调用后,$result= 能得到以下内容:

stdClass Object
(
[out] =>







T

)

以前做这直接读取 xml的,可这种带了一个 stdClass Object( [out] =>

不知道应该如何读到各节点内容了


回复讨论(解决方案)

红色代码 是由 print_r($result);得到的, 如果换成echo $result;就会出错 Catchable fatal error: Object of class stdClass could not be converted to string in

如果你对对象操作不熟悉,可以先转换为数组,这样会方便一些。
你获取的对象很简单, $result->out 就是这个XML字符串,若是想提取其中的内容,可以尝试字符串提取,或者解析XML为对象再转数组(推荐)。

http://www.php.com/manual/zh/language.oop5.properties.php

直接打印
echo $result->out;

作为xml解析
$xml = simplexml_load_string($result->out);
print_r($xml);

SimpleXMLElement Object(    [flights] => SimpleXMLElement Object        (            [flight] => SimpleXMLElement Object                (                    [@attributes] => Array                        (                            [orgCity] => SHA                            [departTerm] => T2                            [dstCity] => CAN                            [arrivalTerm] => --                            [airComp] => MU                            [flightno] => MU5307                            [planeType] => 320                            [startTime] => 2012-07-20 07:30                            [endTime] => 2013-07-20 09:50                            [stopNumber] => 0                            [mealCode] => S                        )                    [cabins] => SimpleXMLElement Object                        (                            [0] => SimpleXMLElement Object                                (                                    [@attributes] => Array                                        (                                            [cabinCode] => Y                                            [cabinType] => 0                                            [cabinDiscount] => 100                                            [cabinName] => 经济舱                                            [cabinSales] => A                                            [ibePrice] => 1280                                            [encryptString] => 79fccacuigjjdskf                                        )                                )                        )                    [cabin] => SimpleXMLElement Object                        (                            [@attributes] => Array                                (                                    [cabinCode] => A                                    [cabinType] => 0                                    [cabinDiscount] => 250                                    [cabinName] => 头等折扣舱                                    [cabinSales] => A                                    [ibePrice] => 2830                                    [encryptString] => 1c9cc629e7f7b1a                                )                        )                )        )    [is_success] => T)

如果你对对象操作不熟悉,可以先转换为数组,这样会方便一些。
你获取的对象很简单, $result->out 就是这个XML字符串,若是想提取其中的内容,可以尝试字符串提取,或者解析XML为对象再转数组(推荐)。
PHP code?1http://www.php.com/manual/zh/language.oop5.properties.php

请问如何解析XML为对象再转数组

用DOMDocument的方法来解析吧

$xml = simplexml_load_string($result->out);// 转换对象所有属性为数组function obj2arr($obj){    $arr = get_object_vars($obj);    foreach($arr as &$child){        is_object($child) && ( $child = obj2arr($child) );    }    return $arr;}$data = obj2arr($xml);

接着你就可以用
$data['flights']['flight']['cabin']['@attributes']['cabinName'];
得到 头等折扣舱

当然,不转换为数组也是可以直接操作的,不过这就需要考验你的基本功

PHP code?1234567891011$xml = simplexml_load_string($result->out);// 转换对象所有属性为数组function
显示这一句出错:
simplexml_load_string() [function.simplexml-load-string]: input conversion failed due to input error

你的 php 小于 5.3
只能识别 utf-8 的 xml
所以报错

有什么办法没?

$result->out = str_replace('encoding="GB2312"', 'encoding="utf-8"', iconv('gbk', 'utf-8', $result->out));

echo $data['flights']['flight']['cabin']['@attributes']['cabinName'];

显示不出来任何数据,怎么回事

人气教程排行