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

Php 如何通过HTML从发送和接收XML数据,php,xml,wordpress,Php,Xml,Wordpress,我的客户想通过快递服务追踪他的包裹。快递服务公司给了我一个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: leng

我的客户想通过快递服务追踪他的包裹。快递服务公司给了我一个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>
POST/codeapi/service\u api.asmx HTTP/1.1
主持人:webapp.example.com
内容类型:应用程序/soap+xml;字符集=utf-8
内容长度:长度
一串
一串
一串
HTTP/1.1200ok
内容类型:应用程序/soap+xml;字符集=utf-8
内容长度:长度
xml

这显然有两个部分,发送部分需要输入客户详细信息和参考号,而接收部分则显示返回的详细信息

第一部分可以是

$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();
$xmlStr=xpath(//默认值:GetCNDetailsByReferenceNumber”);
$details[0]->userName=“SomeName”;
$details[0]->password=“password”;
$details[0]->customerReferenceNo=“Custref”;
回显“发送:”$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];
$receiveXML=xpath(//默认值:GetCNDetailsByReferenceNumberResult”);
echo“\n\n返回值:”(字符串)$details[0];
如何获取信息取决于他们的API,但这将获取数据并提取内容


一个问题。。。当我尝试使用原始XML时,我发现
xmlns=“IP_ADDRESS/”
变体导致了一个警告(不是绝对路径),因此我出于自己的目的将其更改为
xmlns=”http://IP_ADDRESS/“
。您可能需要对此进行更改,并使用相同的值更新
registerXPathNamespace
行。

这不仅仅是XML,而是SOAP。我建议使用SOAP库。