如何在curl/PHP中发送和检索XML


How to send and retrieve XML in curl/PHP?

我必须重现这个流:

page request.php用curl调用page response.php,附带xml->页面响应读取附加的xml并打印另一个xml->页面请求读取并打印响应。

这是我用于页面请求的代码。hp:

    $xmlRequest = '<Main >
   <First>
      <Second>
         Detail 1
      </Second>
      <Second>
        Detail 2
      </Second>
   </First>
</Main>';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://localhost:8888/response.php");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlRequest);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
$server_output = curl_exec ($ch);
curl_close ($ch);

echo "Result :".$server_output;

在pageresponse.php中,我无法检索刚刚从request.php 发送的xml

header('Content-type: application/xml');
var_dump($_REQUEST);

Var_dump返回一个空值,print_r($_REQUEST)也是如此。

如何读取发送的XML?

FROM:phpdocs,CURLOPT_POSTFIELDS必须是array/urlencoded字符串。请注意,如果使用数组Content-Type,则必须为multipart/form-data。下面是一个例子。我希望这能有所帮助。

$postFields = array ( 'xml' => '<Main><First><Second>Detail 1</Second><Second>Detail 2</Second></First></Main>',);
...
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
...
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
...