下面的代码使用带有 text/xml 的 http 标头的 POST 通过 CURL 发送 XML。服务器通过回显 $ch_result 进行响应。我正在通过 AJAX 调用此文件,因为返回的 XML 文件的内容需要显示在客户端。无论如何我可以将这个 XML 文件放入一个数组中,这样我就可以使用 json_encode 并让它像这样返回吗?我需要使用 JavaScript 轻松操作此内容,因为返回的 XML 文件很大......所以数组可能是最好的?
谢谢!
<?php
  $xml_builder = '
                  MY XML POSTED TO SERVER GOES HERE';
  $ch = curl_init('http://username:password@myserver.blabla/api');
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_builder);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
  curl_setopt($ch, CURLOPT_REFERER, 'http://www.mydomain.co.uk');
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  $ch_result = curl_exec($ch);
  curl_close($ch);
  echo $ch_result;
?>