-1
wget -d --header="Content-Type:application/xml" --post-data="$(cat <your xml file>)" http://sample.sample.com/api

我如何在 php 中使用这个函数?我也想得到这个函数的响应。我在php中有一个变量可行的是这样的

$xml = '<sample>
    <Request target="test">
    </Request>
</sample>'

这是我要发布的 xml。

我尝试了以下方法:

$url = 'sample.sample.com/api';;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "$(cat <".$xml.">)"); // receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
echo $server_output = curl_exec ($ch);
curl_close ($ch);

但它返回了这个错误:

解析 XML 失败:需要开始标记,未找到“<”

4

2 回答 2

1

您应该能够遵循使用 PHP 发送和接收 XML中所示的类似场景。该站点的第二部分(发送 XML)使用 curl 来处理此操作,该操作具有与 wget 相似的属性,但使用 PHP 库而不是命令行二进制文件和参数。为了长寿,我将包含该站点的此代码段。

<?php
  /*
   * XML Sender/Client.
   */
  // Get our XML. You can declare it here or even load a file.
  $xml_builder = '
                  <?xml version="1.0" encoding="utf-8"?>
                  <Test>
                      <String>I like Bharath.co.uk!</String>
                  </Test>
                 ';
  // We send XML via CURL using POST with a http header of text/xml.
  $ch = curl_init('http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']);
  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.bharath..co.uk');
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  $ch_result = curl_exec($ch);
  curl_close($ch);
  // Print CURL result.
  echo $ch_result;
?>
于 2013-03-23T14:17:11.200 回答
0

我认同 ...

$cmd = "wget -d --header=\"Content-Type:application/xml\" --post-data=\"$(cat <your xml file>)\" http://sample.sample.com/api";
exec($cmd);
$outputfile = "dl.html";
echo file_get_contents($outputfile);
于 2013-03-23T14:17:42.487 回答