2017-07-15 71 views
0

我的客戶希望通過快遞服務跟蹤他的數據包。 Courier Service公司給我一個XML代碼來使用他們的API。我是XML新手。如何通過表單向此XML發送跟蹤編號並獲取跟蹤詳細信息?我想在我的WordPress網站上使用它。提前致謝。由於代碼是在這裏下面如何通過HTML以XML格式發送和接收數據從

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> 
+0

這不僅僅是XML,而是SOAP。我建議使用一個SOAP庫。 – ThW

回答

0

明明有2個部分這一點,這需要投入的客戶詳細信息和參考號碼和接收的屏幕上顯示的信息回送。

第一部分可以像...

$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行。

相關問題