使用php处理XML响应
我正在使用下面的代码来获取xml数据。我无法以任何其他方式做它,因为我将它用于我的iOS应用程序,并且我正如我预期的那样将该响应作为xml元素接收。当我在浏览器中运行它时,我只看到xml节点的值。使用php处理XML响应
例如,这是我的xml:
<Message version="1.0" messageId="12">
<SaleResponse>
<OrderId>1234</OrderId>
<OrderAmount>1.0</OrderAmount>
</SaleResponse>
</Message>
这正是输出我在iOS应用程序,但在我的浏览器中得到,我得到这样的:
12341.0
这是我的php代码:
<?php
$url = 'https://something.com';
$ch = curl_init($url);
$inp = $_REQUEST["xmlpost"];
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-type: application/xml',
'TX_URL: something.com',
'TX_TokenExID: something',
'TX_APIKey: something'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $inp);
$result = curl_exec($ch);
echo $result;
?>
那么,我该如何处理result
作为XML并分别取每个标签?
在此先感谢
运行通过PHP函数simplexml_load_string
您的字符串:
$result = simplexml_load_string($result);
echo $result->SaleResponse->OrderId
<?php
//or in an actual example
$string = '<Message version="1.0" messageId="12">
<SaleResponse>
<OrderId>1234</OrderId>
<OrderAmount>1.0</OrderAmount>
</SaleResponse>
</Message>';
$obj= simplexml_load_string($string);
echo '<pre>';
print_r($obj);
感谢您的回答,但是在我测试之前,我没有看到格式它所需要的xml必须像这样开始
好吧,我发现我错过了什么,我的xml开始与vpos标记(这种方式我也把它给我的ios应用程序),但是当我用print_r打印结果时,我看到它没有采取第一个标记(vpos)谢谢寻求帮助 –
http://php.net/manual/en/refs.xml.php –
我建议删除避免downvotes的问题 –
@AlexBlex我在这个网站上搜索了几个小时,但唯一我能得到的是一个准备好的xml,具体的开始和结束,如果你能告诉我更具体的东西,我会很高兴 –