首页 > 解决方案 > 从 php 创建 XMLHTTPRequest

问题描述

我正在设置一个小网络服务(本地 LAN)来使用 ePOS 在爱普生热敏打印机上打印一些数据。他们有关于如何从 JavaScript 发送 XML 请求的很好的文档,但我的问题是我希望服务器打印数据库中的数据,所以没有 javascript。

我需要将此 JavaScript“翻译”为 phpreq请求在哪里:

var xhr = new XMLHttpRequest();
xhr.open('POST', url, true);
xhr.setRequestHeader('Content-Type', 'text/xml; charset=utf-8');
xhr.setRequestHeader('If-Modified-Since', 'Thu, 01 Jan 1970 00:00:00 GMT');
xhr.setRequestHeader('SOAPAction', '""');
xhr.onreadystatechange = function () {

   // Receive response document
   if (xhr.readyState == 4) {

      // Parse response document
      if (xhr.status == 200) {
         alert(xhr.responseXML.getElementsByTagName('response') [0].getAttribute('success'));
      }
      else {
         alert('Network error occured.');
      }
   }
};

xhr.send(req);

我已经尝试过这个:

$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => ["Content-type: text/xml; charset=utf-8", "If-Modified-Since: Thu, 01 Jan 1970 00:00:00 GMT", "SOAPAction: ''"],
        'content' => $content
    )
);
# Create the context
$context = stream_context_create($opts);
# Get the response (you can use this for GET)
$result = file_get_contents($url, false, $context);

但似乎不起作用。

我将尝试使用curl,但不知何故我不明白如何正确设置标题以及如何发送数据......

有人能帮我吗?

标签: javascriptphppostxmlhttprequest

解决方案


推荐阅读