繁体   English   中英

如何通过HTML通过XML发送和接收数据

[英]How to Send and Receive Data with XML by HTML From

我的客户想通过快递服务跟踪他的包裹。 快递服务公司给我一个XML代码以使用其API。 我是XML的新手。 如何通过表单将跟踪号发送到此XML,并获取跟踪详细信息? 我想在我的WordPress网站上使用它。 提前致谢。 给定的代码在这里

POST /codeapi/service_api.asmx HTTP/1.1
Host: webapp.example.com
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <GetCNDetailsByReferenceNumber xmlns="IP_ADDRESS/">
      <userName>string</userName>
      <password>string</password>
      <customerReferenceNo>string</customerReferenceNo>
    </GetCNDetailsByReferenceNumber>
  </soap12:Body>
</soap12:Envelope>

HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <GetCNDetailsByReferenceNumberResponse xmlns="IP_ADDRESS">
      <GetCNDetailsByReferenceNumberResult>xmlxml</GetCNDetailsByReferenceNumberResult>
    </GetCNDetailsByReferenceNumberResponse>
  </soap12:Body>
</soap12:Envelope>

显然有两部分,需要输入客户详细信息和参考号的发送,然后显示详细信息的接收。

第一部分可以是类似...

$xmlStr =<<< XML
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <GetCNDetailsByReferenceNumber xmlns="http://IP_ADDRESS/">
      <userName></userName>
      <password></password>
      <customerReferenceNo></customerReferenceNo>
    </GetCNDetailsByReferenceNumber>
  </soap12:Body>
</soap12:Envelope>
XML;

$xml= simplexml_load_string($xmlStr);
$xml->registerXPathNamespace("default", "http://IP_ADDRESS/");
$details = $xml->xpath("//default:GetCNDetailsByReferenceNumber");
$details[0]->userName="SomeName";
$details[0]->password="password";
$details[0]->customerReferenceNo="Custref";
echo 'Send: ' . $xml->asXML();

这一切都是在输入值,当然,您需要为您的特定请求输入详细信息,但这为您提供了构想和XML,然后可以将其发送给他们的服务。

一旦返回值被发送回,那么它就只是提取数据并显示它...

$receiveXML = <<< XML
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <GetCNDetailsByReferenceNumberResponse xmlns="http://IP_ADDRESS">
      <GetCNDetailsByReferenceNumberResult>xmlxml</GetCNDetailsByReferenceNumberResult>
    </GetCNDetailsByReferenceNumberResponse>
  </soap12:Body>
</soap12:Envelope>
XML;

$xml= simplexml_load_string($receiveXML);
$xml->registerXPathNamespace("default", "http://IP_ADDRESS");
$details = $xml->xpath("//default:GetCNDetailsByReferenceNumberResult");
echo "\n\nReturn value:".(string)$details[0];

您如何获取信息取决于他们的API,但这将获取该数据并提取内容。

一个问题...当我尝试使用原始XML时,我发现xmlns="IP_ADDRESS/"变体引起了警告(不是绝对路径),因此出于个人目的将其更改为xmlns="http://IP_ADDRESS/" 您可能需要改回原先的状态,并使用相同的值更新registerXPathNamespace行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM