php - 从 curl POST 响应解析 XML

标签 php xml post curl simplexml

我正在使用 curl 将 XML 文件发布到服务器。一切都很完美,但我不确定如何最好地处理响应。我想用 simpleXML 解析它。有了这个请求:

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_VERBOSE, 1); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 40); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $XPost);
curl_setopt($ch, CURLOPT_POST, 1);

$data = curl_exec($ch); 

if(curl_errno($ch))
print curl_error($ch);
else
print_r($data);
curl_close($ch);

我得到这个作为响应字符串(浏览器源代码 View )

HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Expires: Sat, 6 May 1995 12:00:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Cache-Control: post-check=0, pre-check=0
Pragma: no-cache
Set-Cookie: JSESSIONID=5FD40C8D8F5EDC85E57AF39E877BE564; Path=/upp/; Secure; HttpOnly
Content-Type: text/xml;charset=UTF-8
Content-Length: 829
Date: Sun, 22 Apr 2012 08:59:10 GMT

<?xml version='1.0' encoding='UTF-8'?>
<authorizationService version='1'>
<body merchantId='635634545' status='accepted'>
<transaction refno='1234987' trxStatus='response'>
  <request>
    <amount>2000</amount>
    <currency>USD</currency>
  </request>
  <response>
    <responseCode>01</responseCode>
    <responseMessage>Authorized</responseMessage>
  </response>
</transaction>

如何使用 simpleXML 正确解析 $data。我的意思是去掉 header 部分?

最佳答案

如果不需要 header ,请将 CURLOPT_HEADER 设置为 false

关于php - 从 curl POST 响应解析 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10266742/

相关文章:

java - Angular 4 http POST 请求到 java Spring Controller

objective-c - AFNetworking 2.0 的 POST 请求 - AFHTTPSessionManager

php - 从mysql中选择匹配的 session 到表

php - 避免对某些自动完成的订单重复发送电子邮件通知

java - 文件和图像未加载 Spring MVC

Android Kotlin Retrofit + SimpleXMLConverter ElementList 无法正确解析

c# - 更新文件中的 xml 的最佳方法是什么?

php - 将 'checked' 设置为来自数据库 radio 输入的值

javascript - getElementsByClassName 从 php foreach 获取一个元素

PHP shell_exec 等待脚本完成?