php soap客户端为英国邮件webservice api?

Rin*_*ler 9 php api soap wsdl web-services

我正在一个订购商品的商业网站上工作.要跟踪该传递,我需要向用户提供一个链接,其中包含用户填写的表单中的所有参数,以创建传递并使用带有邮件中提供的链接的英国邮件跟踪其状态.

我必须使用英国邮件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);


?>
Run Code Online (Sandbox Code Playgroud)

我已经解决了所有问题并得到了我的货物,不需要追踪我的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>
Run Code Online (Sandbox Code Playgroud)

示例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>
Run Code Online (Sandbox Code Playgroud)

zaf*_*aja 20

介绍

显然,这里缺少文档.不幸的是,$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"/>
Run Code Online (Sandbox Code Playgroud)

后者(位置)又进口另一个:

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

最后一个(位置),导入所有相关的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>
Run Code Online (Sandbox Code Playgroud)

它还描述了通过调用也可以查看的操作$soapClient->__getFunctions():

Array
(
    [0] => LoginResponse Login(Login $parameters)
    [1] => LogoutResponse Logout(Logout $parameters)
)
Run Code Online (Sandbox Code Playgroud)

在这里,我们看到Login()操作接受$parameters类型Login作为其参数并返回类型的响应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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

它有一个名为的元素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>
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)

这给我们一个类型的结果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>
Run Code Online (Sandbox Code Playgroud)

,其中包含一个名为的元素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>
Run Code Online (Sandbox Code Playgroud)

UKMLoginResponse有两个Accounts类型ArrayOfAccountWebModelAuthenticationToken类型的String元素,还有三个类型,类型和类型UKMWebResponse(注释extension语句)Errors的元素:ArrayOfUKMWebErrorWarningsArrayOfUKMWebWarningResultUKMResultState

<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>
Run Code Online (Sandbox Code Playgroud)

wsimport工具生成的工件中,它看起来像这样:

public class UKMLoginResponse extends UKMWebResponse { ... }
Run Code Online (Sandbox Code Playgroud)

因此,为了从中获取身份验证令牌LoginResponse,我们执行以下操作:

$LoginResponse = $soapClient->Login($Login);
$AuthenticationToken = $LoginResponse->LoginResult->AuthenticationToken;
Run Code Online (Sandbox Code Playgroud)

调用方法

我在这里不会非常具体,因为它与我们上面所做的非常相似.

举个例子,让我们调用一个AddDomesticConsignment()方法.根据寄存服务的WSDL文件,并通过返回的结果$soapClient->__getFunctions()AddDomesticConsignment()方法使用一个$parameters类型的参数AddDomesticConsignment和返回类型的结果AddDomesticConsignmentResponse.通过分析AddDomesticConsignment复杂类型,我们看到它有一个名为requesttype 的元素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
Run Code Online (Sandbox Code Playgroud)

请注意,并非所有元素都是必需的.那些可选的minOccurs属性0在XML Schema中设置为.

<xs:element minOccurs="0" name="PreDeliveryNotification" type="tns:PreDeliveryNotificationType"/>
Run Code Online (Sandbox Code Playgroud)

所以,最终我们称之为方法:

$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);
Run Code Online (Sandbox Code Playgroud)

AddDomesticConsignmentResponse为我们解析解析LoginResponse根据其在XML Schema文档中定义.

好吧,我想这就是它的全部.我自己没有尝试过,但从理论上说它应该可行.希望这可以帮助.

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
}
Run Code Online (Sandbox Code Playgroud)

而已!