簡體   English   中英

php soap客戶端為英國郵件webservice api?

[英]php soap client for uk mail webservice api?

我正在一個訂購商品的商業網站上工作。 要跟蹤該傳遞,我需要向用戶提供一個鏈接,其中包含用戶填寫的表單中的所有參數,以創建傳遞並使用帶有郵件中提供的鏈接的英國郵件跟蹤其狀態。

我必須使用英國郵件Web服務API。 誰能告訴我怎么做? 我是SOAP WSDL的新手。

從我的理解,我現在做了這個如何進一步? 我的代碼低於它的基本客戶端我需要:

  1. 驗證登錄並使用驗證令牌

  2. 我需要發送參數來創建國內作業

  3. 我也需要跟蹤交付狀態

這是我更新的代碼:

<?php 

$LoginWebRequest = new stdClass();
$LoginWebRequest->Username = 'xxx cant show here xxx';
$LoginWebRequest->Password = 'xxx cant show here xxx';

//echo "<pre>";  print_r($LoginWebRequest); "</pre>"; exit;

$Login = new stdClass();
$Login->loginWebRequest = $LoginWebRequest;

//echo "<pre>";  print_r($Login); "</pre>"; exit; 

$soapClient = new SoapClient('somewsdl?wsdl');
$LoginResponse = $soapClient->Login($Login);

//echo "<pre>";  print_r($LoginResponse); "</pre>"; exit; 

$LoginResponse = $soapClient->Login($Login);


// -- till here my code runs fine and also gives the failed output but adding the code //below gives me error cant find out whats wrong 


$AuthenticationToken = $LoginResponse->LoginResult->AuthenticationToken;



$AddDomesticConsignmentWebRequest = new stdClass();
$AddDomesticConsignmentWebRequest->Username = 'xxxxxx';
// setting the Authentication Token from the previous step
$AddDomesticConsignmentWebRequest->AuthenticationToken = $AuthenticationToken ;
// other properties are set here...

$AddDomesticConsignment = new stdClass();
$AddDomesticConsignment->request = $AddDomesticConsignmentWebRequest;


$soapClient = new SoapClient('https://svc?wsdl');
$AddDomesticConsignmentResponse = $soapClient->AddDomesticConsignment($AddDomesticConsignment);


?>

我已經解決了所有問題並得到了我的寄售,不需要追蹤我的api

我的xml是這樣的,或者你可以查看pdf

     Example XML Request:
    <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"        xmlns:thir="http://webapp-cl.internet-delivery.com/ThirdPartyIntegrationService">
    <soap:Header/>
    <soap:Body>
    <thir:ConsignmentTrackingSearchV1>
    <thir:UserName>mail.com</thir:UserName>
    <thir:Password>123</thir:Password>
    <thir:Token></thir:Token>
    <thir:ConsignmentNumber>01161</thir:ConsignmentNumber>
    <thir:IsPartialConsignmentNumber>false</thir:IsPartialConsignmentNumber>
    <thir:CustomerReference></thir:CustomerReference>
    <thir:IsPartialCustomerReference>false</thir:IsPartialCustomerReference>
    <thir:DeliveryPostCode></thir:DeliveryPostCode>
    <thir:MailingID></thir:MailingID>
    <thir:MaxResults>100</thir:MaxResults>
    </thir:ConsignmentTrackingSearchV1>
    </soap:Body>
    </soap:Envelope>

示例xml響應

      <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
       <soap:Body>
       <ConsignmentTrackingSearchV1Response xmlns="http://webapp-cl.internet- delivery.com/ThirdPartyIntegrationService">
        <ConsignmentTrackingSearchV1Result>
        <ResultState>Successful</ResultState>
         <ConsignmentResults>
       <ConsignmentSearchResult>
    <ConsignmentNumber>001161</ConsignmentNumber>
    <CustomerRef1/>
   <CustomerRef2/>
     <SubCustomerRef1/>
     <SubCustomerRef2/>
      <DeliveryType/>
      <ConsignmentStatus>Delivered</ConsignmentStatus>
      <DateTimeDelivered>2010-02-11T12:00:00+00:00</DateTimeDelivered>
      <ItemsDelivered>2</ItemsDelivered>
      <RecipientName>robin</RecipientName>
      <DeliveryComments/>
      <ExpectedDeliveryDate>2010-02-11T00:00:00</ExpectedDeliveryDate>
       <DeliveryService>Next Day</DeliveryService>
      <TotalItems>2</TotalItems>
      <Consignmentkey>22</Consignmentkey>
        </ConsignmentSearchResult>
         </ConsignmentResults>
        </ConsignmentTrackingSearchV1Result>
        </ConsignmentTrackingSearchV1Response>
        </soap:Body>
       </soap:Envelope>

介紹

顯然,這里缺少文檔。 不幸的是, $soapClient->__getTypes()$soapClient->__getTypes() 它僅顯示Web服務支持的可用復雜類型,但它不向我們顯示它們之間的關系。 即使您有$soapClient->__getFunctions()返回的輸入和輸出類型的所有可用操作的列表,也無法保證您可以在不知道復雜類型的確切性質或沒有任何文檔的情況下繼續操作。 但幸運的是,這是一個基於SOAP的Web服務,它提供了一個WSDL文檔。 WSDL文檔描述了所有支持的操作和復雜類型,以及它們之間的關系。 因此,我們可以通過僅檢查WSDL文檔來弄清楚如何使用該服務。

有兩種方法可以檢查WSDL文檔:

  1. 從WSDL文檔生成工件(客戶端類)並檢查工件
  2. 查看WSDL文檔和XML Schema文檔。

1.文物

工件可以由Java或C#等強類型語言提供的工具生成。 https://qa-api.ukmail.com/Services/UKMAuthenticationServices/頁面建議使用svcutil.exe工具為C#編程語言生成工件,或者您也可以使用wsimport工具為其生成工件。 Java編程語言。 我懷疑可以有任何好的工具來生成PHP編程語言的工件。

2. WSDL文檔和XML模式

如果您不熟悉C#或Java,您可以通過查看它和XML Schema來檢查WSDL文檔。 XML Schema可以包含在WSDL文檔中,也可以從外部文件導入。 雖然WSDL文檔描述了可以在Web服務上執行的操作,但XML Schema描述了復雜類型及其關系。

行動

我寫了介紹部分,以便您知道如何自己完成。 下面我想展示一個例子。 為了檢查WSDL文檔,我使用了兩種方法。 首先我使用wsimport工具生成工件,然后我讀了很多XML。

此服務的WSDL文檔使用import語句分為幾個文件。 因此,為了找到所有操作和復雜類型,您必須遵循import語句。

認證

如果我們查看Authentication Service的WSDL文檔( 位置 ),我們可以看到它導入了另一個WSDL文檔:

<wsdl:import namespace="http://tempuri.org/" location="https://qa-api.ukmail.com/Services/UKMAuthenticationServices/UKMAuthenticationService.svc?wsdl=wsdl1"/>

后者( 位置 )又進口另一個:

<wsdl:import namespace="http://www.UKMail.com/Services/Contracts/ServiceContracts" location="https://qa-api.ukmail.com/Services/UKMAuthenticationServices/UKMAuthenticationService.svc?wsdl=wsdl0"/>

最后一個( 位置 ),導入所有相關的XML Schema:

<wsdl:types>
  <xsd:schema targetNamespace="http://www.UKMail.com/Services/Contracts/ServiceContracts/Imports">
    <xsd:import schemaLocation="https://qa-api.ukmail.com/Services/UKMAuthenticationServices/UKMAuthenticationService.svc?xsd=xsd0" namespace="http://www.UKMail.com/Services/Contracts/ServiceContracts"/>
    <xsd:import schemaLocation="https://qa-api.ukmail.com/Services/UKMAuthenticationServices/UKMAuthenticationService.svc?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/"/>
    <xsd:import schemaLocation="https://qa-api.ukmail.com/Services/UKMAuthenticationServices/UKMAuthenticationService.svc?xsd=xsd2" namespace="http://www.UKMail.com/Services/Contracts/DataContracts"/>
    <xsd:import schemaLocation="https://qa-api.ukmail.com/Services/UKMAuthenticationServices/UKMAuthenticationService.svc?xsd=xsd3" namespace="http://schemas.datacontract.org/2004/07/UKMWebAPICommon.WebResponses"/>
  </xsd:schema>
</wsdl:types>

它還描述了通過調用$soapClient->__getFunctions()也可以查看的操作:

Array
(
    [0] => LoginResponse Login(Login $parameters)
    [1] => LogoutResponse Logout(Logout $parameters)
)

在這里,我們看到Login()操作接受類型為Login $parameters作為其參數,並返回類型為LoginResponse的響應。 這是它在WSDL文檔中的樣子:

<wsdl:operation name="Login">
  <wsdl:input wsaw:Action="http://www.UKMail.com/Services/IUKMAuthenticationService/Login" message="tns:IUKMAuthenticationService_Login_InputMessage"/>
  <wsdl:output wsaw:Action="http://www.UKMail.com/Services/Contracts/ServiceContracts/IUKMAuthenticationService/LoginResponse" message="tns:IUKMAuthenticationService_Login_OutputMessage"/>
</wsdl:operation>

Login是一個復雜的類型,這可以在導入的XML Schema文檔之一( schemaLocation )中看到:

<xs:element name="Login">
  <xs:complexType>
    <xs:sequence>
      <xs:element xmlns:q1="http://www.UKMail.com/Services/Contracts/DataContracts" minOccurs="0" name="loginWebRequest" nillable="true" type="q1:LoginWebRequest"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

它有一個名為loginWebRequest的元素,它也是一個名為LoginWebRequest的復雜類型,在另一個導入的XML Schema中描述:

<xs:complexType name="LoginWebRequest">
  <xs:sequence>
    <xs:element name="Password" nillable="true" type="xs:string"/>
    <xs:element name="Username" nillable="true" type="xs:string"/>
  </xs:sequence>
</xs:complexType>

LoginWebRequest更簡單。 它有兩種簡單類型, UsernamePassword類型為String

在PHP中,復雜類型由stdClass的對象表示。 所以,為了調用Login()操作,我們必須創建兩個對象LoginLoginWebRequest

$LoginWebRequest = new stdClass();
$LoginWebRequest->Username = 'Username';
$LoginWebRequest->Password = 'p@$$w0rd';

$Login = new stdClass();
$Login->loginWebRequest = $LoginWebRequest;

$soapClient = new SoapClient('https://qa-api.ukmail.com/Services/UKMAuthenticationServices/UKMAuthenticationService.svc?wsdl');
$LoginResponse = $soapClient->Login($Login);

這給我們一個LoginResponse類型的結果:

<xs:element name="LoginResponse">
  <xs:complexType>
    <xs:sequence>
      <xs:element xmlns:q2="http://www.UKMail.com/Services/Contracts/DataContracts" minOccurs="0" name="LoginResult" nillable="true" type="q2:UKMLoginResponse"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

,其中包含名為LoginResult的元素,其類型為UKMLoginResponse

<xs:complexType name="UKMLoginResponse">
  <xs:complexContent mixed="false">
    <xs:extension base="tns:UKMWebResponse">
      <xs:sequence>
        <xs:element minOccurs="0" name="Accounts" nillable="true" type="tns:ArrayOfAccountWebModel"/>
        <xs:element name="AuthenticationToken" nillable="true" type="xs:string"/>
      </xs:sequence>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>

UKMLoginResponse都有自己的兩個元素Accounts類型ArrayOfAccountWebModelAuthenticationToken類型的String和三個元素從inhereted UKMWebResponse (注意extension語句) Errors類型的ArrayOfUKMWebErrorWarnings類型的ArrayOfUKMWebWarningResult類型的UKMResultState

<xs:complexType name="UKMWebResponse">
  <xs:sequence>
    <xs:element minOccurs="0" name="Errors" nillable="true" type="tns:ArrayOfUKMWebError"/>
    <xs:element xmlns:q1="http://schemas.datacontract.org/2004/07/UKMWebAPICommon.WebResponses" name="Result" type="q1:UKMResultState"/>
    <xs:element minOccurs="0" name="Warnings" nillable="true" type="tns:ArrayOfUKMWebWarning"/>
  </xs:sequence>
</xs:complexType>

wsimport工具生成的工件中,它看起來像這樣:

public class UKMLoginResponse extends UKMWebResponse { ... }

因此,為了從LoginResponse獲取身份驗證令牌,我們執行以下操作:

$LoginResponse = $soapClient->Login($Login);
$AuthenticationToken = $LoginResponse->LoginResult->AuthenticationToken;

調用方法

我在這里不會非常具體,因為它與我們上面所做的非常相似。

例如,讓我們調用AddDomesticConsignment()方法。 根據寄存服務的WSDL文件,並通過返回的結果$soapClient->__getFunctions()AddDomesticConsignment()方法使用一個$parameters類型的參數AddDomesticConsignment並返回類型的結果AddDomesticConsignmentResponse 通過分析AddDomesticConsignment復雜類型,我們看到它有一個名為request的類型為AddDomesticConsignmentWebRequest的元素,它擴展了AddConsignmentWebRequest ,它本身擴展了WebRequest 以下是AddDomesticConsignmentWebRequest類型的所有元素的列表:

// AddDomesticConsignmentWebRequest's own elements
boolean BookIn
decimal CODAmount
string ConfirmationEmail
string ConfirmationTelephone
boolean ExchangeOnDelivery
int ExtendedCover
boolean LongLength
PreDeliveryNotificationType PreDeliveryNotification
string SecureLocation1
string SecureLocation2
boolean SignatureOptional

// elements inhereted from AddConsignmentWebRequest
string AccountNumber
AddressWebModel Address
string AlternativeRef
string BusinessName
string CollectionJobNumber
boolean ConfirmationOfDelivery
string ContactName
string CustomersRef
string Email
int Items
int ServiceKey
string SpecialInstructions1
string SpecialInstructions2
string Telephone
decimal Weight

// elements inhereted from WebRequest
string Username
string AuthenticationToken

請注意,並非所有元素都是必需的 那些是可選的,在XML Schema中將minOccurs屬性設置為0

<xs:element minOccurs="0" name="PreDeliveryNotification" type="tns:PreDeliveryNotificationType"/>

所以,最終我們稱之為方法:

$AddDomesticConsignmentWebRequest = new stdClass();
$AddDomesticConsignmentWebRequest->Username = 'Username';
// setting the Authentication Token from the previous step
$AddDomesticConsignmentWebRequest->AuthenticationToken = $AuthenticationToken;
// other properties are set here...

$AddDomesticConsignment = new stdClass();
$AddDomesticConsignment->request = $AddDomesticConsignmentWebRequest;

$soapClient = new SoapClient('https://qa-api.ukmail.com/Services/UKMConsignmentServices/UKMConsignmentService.svc?wsdl');
$AddDomesticConsignmentResponse = $soapClient->AddDomesticConsignment($AddDomesticConsignment);

AddDomesticConsignmentResponse被解析為我們根據XML Schema文檔中的定義解析了LoginResponse

好吧,我想這就是它的全部。 我自己沒有嘗試過,但理論上它應該可行。 希望這可以幫助。

UPDATE

根據文件跟蹤,寄售應該像執行以下操作一樣簡單:

// create the SOAP client
$soapClient = new SoapClient('http://web-service/?wsdl');
// call the `ConsignmentTrackingSearchV1` method and pass the search parameters
$ConsignmentTrackingSearchV1Response = $soapClient->ConsignmentTrackingSearchV1(
    'mail.com', // Username
    '123',      // Password
    '',         // Token
    '01161',    // ConsignmentNumber
    'false',    // IsPartialConsignmentNumber
    '',         // CustomerReference
    'false'     // IsPartialCustomerReference
    '',         // DeliveryPostCode
    '',         // MailingID
    100         // MaxResults
);
// parse the response
$ConsignmentTrackingSearchV1Result = $ConsignmentTrackingSearchV1Response->ConsignmentTrackingSearchV1Result;
$ResultState = $ConsignmentTrackingSearchV1Result->ResultState; // Successful
$ConsignmentResults = $ConsignmentTrackingSearchV1Result->ConsignmentResults;
// loop through the `ConsignmentResults`
foreach ($ConsignmentResults as $ConsignmentSearchResult) {
    $ConsignmentNumber = $ConsignmentSearchResult->ConsignmentNumber;
    $ConsignmentStatus = $ConsignmentSearchResult->ConsignmentStatus;
    // other properties
}

而已!

zafarkhaja先生幫助了我很多,但對於像我這樣的人,我想解釋一下對我有用的東西,以便將來也可能對他人有所幫助。

用於處理復雜類型 例如在我的英國郵件Api中登錄我做了這個

$LoginWebRequest = new stdClass();
$LoginWebRequest->Username = 'username';
$LoginWebRequest->Password = 'password';



$Login = new stdClass();
$Login->loginWebRequest = $LoginWebRequest;



$soapClient = new SoapClient('somewsdl?wsdl', array('cache_wsdl' => WSDL_CACHE_NONE) );


$LoginResponse = $soapClient->Login($Login);

並且我可以登錄 它給了我一個我需要回復的回應

 $AuthenticationToken = $LoginResponse->LoginResult->AuthenticationToken; 

我得到了身份驗證令牌,確保您可以根據您的網絡服務更改參數

好吧,當你沒有處理復雜類型時,使用soap客戶端

   $soapClient = new SoapClient('http://somewsdl?wsdl');

    //create an array whose valuse will be passed has parameters
          $input_array = array (   
               UserName => 'username',
               Password => 'password'

     ) ;

    // passes our array
    $ConsignmentResponse = $soapClient1->TrackingSearch($input_array);

  //retrieve the response like this 

    $ResultState =  $ConsignmentResponse->ResultState; // Successful

好吧,我搜索過的地方,但沒有人發布任何工作的例子,所有的代碼適用於我的PHP肥皂1.1復雜的類型和沒有。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM