从PHP创建XMLHTTPRequest

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

我需要将此JavaScript“翻译”为php,其中req是请求:

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 ,但是由于某种原因,我不理解如何正确设置标头以及如何发送数据...

有人可以帮我吗?

fanqi789 回答:从PHP创建XMLHTTPRequest

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3137995.html

大家都在问