一个接口,能得到返回的xml文件,如何提取有用的内容?
接口调用后,$result= 能得到以下内容:
stdClass Object
(
[out] =>
)
以前做这直接读取 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.net/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.net/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'];
显示不出来任何数据,怎么回事

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Alipay PHP...

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...
