使用 PHP 接收、处理然后返回 XML 数据的最佳方法是什么?

发布于 2024-10-21 00:01:35 字数 1244 浏览 4 评论 0原文

我需要创建一个 PHP 脚本,该脚本通过 HTTP POST 请求接收 XML 输入,对其进行处理,然后返回 XML 响应。我自己花了相当长的时间尝试这个,这就是我到目前为止所得到的。

我首先快速组合了一个 HTML 表单,该表单允许用户以字符串形式提交 XML 数据、以字符串形式提交 XML URI,或者将 XML URI 提交到我的 PHP 脚本。此表单仅用于测试,因为我的任务只是创建此脚本。

在我的 PHP 脚本中,我做了一些输入处理...

// Checks that some XML input has been given
if (!(isset($_POST['xmlinput']))) {
    handleErrors("No XML Input Given");
}

// Creates a new DOM Document
$domdoc = new DomDocument;

// Sets the URL of the XML Schema
$xmlschema = "xmlschema.xsd";

// If XML input is a file, tries to load it
if (file_exists($_POST['xmlinput'])) {
    if (!$domdoc->load($_POST['xmlinput'])) {
        handleErrors("Error in XML File");
    }
}
// If XML input is not a file, tries to load it as a string
else {
    if (!$domdoc->loadXML($_POST['xmlinput'])) {
        handleErrors("Error in XML Document");
    }
}

// Validates the XML against the schema
if (!($domdoc->schemaValidate($xmlschema))) {
    handleErrors("XML Does Not Conform To Schema");
}

然后我根据 XML 数据做了一些事情,然后我想生成一个 XML 响应。我相当确定我可以在 DOM 或 SimpleXML 中创建 XML,但我根本不明白如何将其返回到原始页面。另外,这是在 PHP 中处理 XML 输入的最佳方法吗?我看过很多关于 php://input 或 $HTTP_RAW_POST_DATA 的帖子,但这些似乎并不比我使用的方法更好。您能给我的任何信息都会有很大的帮助。如果我可以进一步澄清,请告诉我。

I need to create a PHP script that receives XML input via an HTTP POST request, processes it, and then returns an XML response. I have spent quite a while attempting this on my own, and here is what I have so far.

I first quickly threw together an HTML form that allows a user to submit XML data as a string, an XML URI as a string, or an XML URI to my PHP script. This form is for testing only, as I am only tasked with creating this script.

In my PHP script I do some input handling...

// Checks that some XML input has been given
if (!(isset($_POST['xmlinput']))) {
    handleErrors("No XML Input Given");
}

// Creates a new DOM Document
$domdoc = new DomDocument;

// Sets the URL of the XML Schema
$xmlschema = "xmlschema.xsd";

// If XML input is a file, tries to load it
if (file_exists($_POST['xmlinput'])) {
    if (!$domdoc->load($_POST['xmlinput'])) {
        handleErrors("Error in XML File");
    }
}
// If XML input is not a file, tries to load it as a string
else {
    if (!$domdoc->loadXML($_POST['xmlinput'])) {
        handleErrors("Error in XML Document");
    }
}

// Validates the XML against the schema
if (!($domdoc->schemaValidate($xmlschema))) {
    handleErrors("XML Does Not Conform To Schema");
}

I then do some stuff based on what the XML data is, and then I want to generate an XML response. I am fairly certain I can create XML in DOM or SimpleXML, but I simply do not understand how to return it to the original page. Also, is this the best method to handle XML input in PHP? I have seen a ton of posts about php://input or $HTTP_RAW_POST_DATA but these don't seem to be any better than the method I use. Any information you can give me would be a big help. If I can clarify any more, just let me know.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

还给你自由 2024-10-28 00:01:35

非常简单,您使用 SimpleXml 创建 xml 文档,

然后只需

echo $xml->asXML();

如果您需要将 xml 数据发布到 Web 服务,

您可以使用以下代码来实现。

function do_post_request($url, $data, $optional_headers = null)
{
  $params = array('http' => array(
              'method' => 'POST',
              'content' => $data
            ));
  if ($optional_headers !== null) {
    $params['http']['header'] = $optional_headers;
  }
  $ctx = stream_context_create($params);
  $fp = @fopen($url, 'rb', false, $ctx);
  if (!$fp) {
    throw new Exception("Problem with $url, $php_errormsg");
  }
  $response = @stream_get_contents($fp);
  if ($response === false) {
    throw new Exception("Problem reading data from $url, $php_errormsg");
  }
  return $response;
}

Very simply, you create your xml document using SimpleXml,

then just

echo $xml->asXML();

If you need to post xml data to a webservice,

You can use the following code to acheive that.

function do_post_request($url, $data, $optional_headers = null)
{
  $params = array('http' => array(
              'method' => 'POST',
              'content' => $data
            ));
  if ($optional_headers !== null) {
    $params['http']['header'] = $optional_headers;
  }
  $ctx = stream_context_create($params);
  $fp = @fopen($url, 'rb', false, $ctx);
  if (!$fp) {
    throw new Exception("Problem with $url, $php_errormsg");
  }
  $response = @stream_get_contents($fp);
  if ($response === false) {
    throw new Exception("Problem reading data from $url, $php_errormsg");
  }
  return $response;
}
短暂陪伴 2024-10-28 00:01:35

您不想使用错误抑制运算符,但添加正确的 选项那:

$dom = new DOMDocument;
$dom->loadXML($source, LIBXML_NOERROR);

另一种方法是使用 libxml_use_internal_errors< /代码>

响应基本上就是 Web 服务器发送回请求客户端的内容,因此要发送 XML 响应,您只需使用 XML 标头回显您的 XML,例如

header ("Content-Type:text/xml; charset=utf-8");
echo $xml->asXML();

You dont want to use the Error Suppression Operator but add the proper Option for that:

$dom = new DOMDocument;
$dom->loadXML($source, LIBXML_NOERROR);

An alternative would be to use libxml_use_internal_errors.

A response is basically just what the webserver sends back to the requesting client, so to send an XML response, you simply echo your XML it with an XML header, e.g.

header ("Content-Type:text/xml; charset=utf-8");
echo $xml->asXML();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文