简体   繁体   English

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

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

I'm working on a commerce site on which orders are placed. 我正在一个订购商品的商业网站上工作。 To track that delivery I need to give a link to users with all the parameters from a form filled by user to create a delivery and track its status using UK Mail with the link provided in a mail. 要跟踪该传递,我需要向用户提供一个链接,其中包含用户填写的表单中的所有参数,以创建传递并使用带有邮件中提供的链接的英国邮件跟踪其状态。

I have to use UK Mail Web Service API. 我必须使用英国邮件Web服务API。 Can anyone show me how to do so? 谁能告诉我怎么做? I am new to SOAP WSDL. 我是SOAP WSDL的新手。

From my understanding i did this now how to go further? 从我的理解,我现在做了这个如何进一步? My code below its just basic client i need to: 我的代码低于它的基本客户端我需要:

  1. authenticate login and use the authenticate token 验证登录并使用验证令牌

  2. I need to send the parameters to create a domestic assignments 我需要发送参数来创建国内作业

  3. I need to track the delivery status too 我也需要跟踪交付状态

here is my updated code : 这是我更新的代码:

<?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);


?>

i have solved all and got my consignment no too just need to track my api 我已经解决了所有问题并得到了我的寄售,不需要追踪我的api

my xml is like this or u can check the pdf 我的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>

example xml response 示例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>

Introduction 介绍

Obviously, documentation is missing here. 显然,这里缺少文档。 Unfortunately, $soapClient->__getTypes() doesn't tell much. 不幸的是, $soapClient->__getTypes()$soapClient->__getTypes() It only shows available complex types supported by a web service, but it doesn't show us the relationship among them. 它仅显示Web服务支持的可用复杂类型,但它不向我们显示它们之间的关系。 Even if You have a list of all available operations with their input and output types returned by $soapClient->__getFunctions() , there is no guarantee You can proceed without knowing the exact nature of the complex types or without having any kind of documentation. 即使您有$soapClient->__getFunctions()返回的输入和输出类型的所有可用操作的列表,也无法保证您可以在不知道复杂类型的确切性质或没有任何文档的情况下继续操作。 But fortunately, this is a SOAP-based web service that provides a WSDL document. 但幸运的是,这是一个基于SOAP的Web服务,它提供了一个WSDL文档。 The WSDL document describes all the supported operations and complex types, as well as their relationships. WSDL文档描述了所有支持的操作和复杂类型,以及它们之间的关系。 So, we can figure out how to use the service by only examining the WSDL document. 因此,我们可以通过仅检查WSDL文档来弄清楚如何使用该服务。

There are two ways of examining the WSDL document: 有两种方法可以检查WSDL文档:

  1. generate artifacts (client classes) from the WSDL document and examine the artifacts 从WSDL文档生成工件(客户端类)并检查工件
  2. look through the WSDL document and the XML Schema documents. 查看WSDL文档和XML Schema文档。

1. Artifacts 1.文物

The artifacts can be generated by tools provided by the strong typed languages like Java or C#. 工件可以由Java或C#等强类型语言提供的工具生成。 The https://qa-api.ukmail.com/Services/UKMAuthenticationServices/ page suggests to use the svcutil.exe tool to generate the artifacts for the C# programming language, or You can also use the wsimport tool to generate the artifacts for the Java programming language. https://qa-api.ukmail.com/Services/UKMAuthenticationServices/页面建议使用svcutil.exe工具为C#编程语言生成工件,或者您也可以使用wsimport工具为其生成工件。 Java编程语言。 I doubt that there can be any good tool for generating the artifacts for the PHP programming language. 我怀疑可以有任何好的工具来生成PHP编程语言的工件。

2. WSDL document and XML Schemas 2. WSDL文档和XML模式

If You're not familiar with C# or Java, You can always examine the WSDL document by looking through it and the XML Schemas. 如果您不熟悉C#或Java,您可以通过查看它和XML Schema来检查WSDL文档。 The XML Schemas can be included in the WSDL document or imported from an external file. XML Schema可以包含在WSDL文档中,也可以从外部文件导入。 While the WSDL document describes the operations that can be performed on the web service, the XML Schemas describe the complex types and their relationships. 虽然WSDL文档描述了可以在Web服务上执行的操作,但XML Schema描述了复杂类型及其关系。

Action 行动

I wrote the Introduction part so that You know how to do it on Your own. 我写了介绍部分,以便您知道如何自己完成。 Below I want to show an example of it. 下面我想展示一个例子。 For the purpose of examining the WSDL document I used both ways. 为了检查WSDL文档,我使用了两种方法。 First I generated the artifacts using the wsimport tool, then I read a lot of XML. 首先我使用wsimport工具生成工件,然后我读了很多XML。

The WSDL document for this service is divided into several files using import statements. 此服务的WSDL文档使用import语句分为几个文件。 So, in order to find all the operations and complex types You have to follow the import statements. 因此,为了找到所有操作和复杂类型,您必须遵循import语句。

Authentication 认证

If We look at the Authentication Service's WSDL document ( location ), We can see that it imports another WSDL document: 如果我们查看Authentication Service的WSDL文档( 位置 ),我们可以看到它导入了另一个WSDL文档:

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

The latter ( location ), in its turn, imports another one: 后者( 位置 )又进口另一个:

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

The final one ( location ), imports all the related XML Schemas: 最后一个( 位置 ),导入所有相关的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>

It also describes the operations which can also be viewed by calling $soapClient->__getFunctions() : 它还描述了通过调用$soapClient->__getFunctions()也可以查看的操作:

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

Here, We see that the Login() operation accepts $parameters of type Login as its argument and returns a response of type LoginResponse . 在这里,我们看到Login()操作接受类型为Login $parameters作为其参数,并返回类型为LoginResponse的响应。 This is how it looks in the WSDL document: 这是它在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 is a complex type, this can be seen in one of the imported XML Schema documents ( schemaLocation ): 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>

It has an element named loginWebRequest which is also a complex type called LoginWebRequest which is described in another imported XML Schema: 它有一个名为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 is simpler. LoginWebRequest更简单。 It has two simple types Username and Password of type String . 它有两种简单类型, UsernamePassword类型为String

In PHP complex types are represented by objects of stdClass . 在PHP中,复杂类型由stdClass的对象表示。 So, in order to call the Login() operation, We have to create two objects Login and LoginWebRequest : 所以,为了调用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);

This gives us a result of type LoginResponse : 这给我们一个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>

, which contains an element named LoginResult which has a type of UKMLoginResponse : ,其中包含名为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 has two elements of its own Accounts of type ArrayOfAccountWebModel and AuthenticationToken of type String and three more elements inhereted from UKMWebResponse (note the extension statement) Errors of type ArrayOfUKMWebError , Warnings of type ArrayOfUKMWebWarning and Result of type UKMResultState : 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>

In the artifacts generated by the wsimport tool it looks like this: wsimport工具生成的工件中,它看起来像这样:

public class UKMLoginResponse extends UKMWebResponse { ... }

So, in order to get the authentication token from the LoginResponse , we do the following: 因此,为了从LoginResponse获取身份验证令牌,我们执行以下操作:

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

Calling a method 调用方法

I won't be very specific here, because it's very similar to what we did above. 我在这里不会非常具体,因为它与我们上面所做的非常相似。

As an example, let's call a AddDomesticConsignment() method. 例如,让我们调用AddDomesticConsignment()方法。 According to the WSDL document of the Consignment Service and the result returned by $soapClient->__getFunctions() the AddDomesticConsignment() method takes one $parameters argument of type AddDomesticConsignment and returns a result of type AddDomesticConsignmentResponse . 根据寄存服务的WSDL文件,并通过返回的结果$soapClient->__getFunctions()AddDomesticConsignment()方法使用一个$parameters类型的参数AddDomesticConsignment并返回类型的结果AddDomesticConsignmentResponse By analyzing the AddDomesticConsignment complex type, we see that it has an element named request of type AddDomesticConsignmentWebRequest which extends AddConsignmentWebRequest which itself extends WebRequest . 通过分析AddDomesticConsignment复杂类型,我们看到它有一个名为request的类型为AddDomesticConsignmentWebRequest的元素,它扩展了AddConsignmentWebRequest ,它本身扩展了WebRequest Following is the list of all of the elements of the AddDomesticConsignmentWebRequest type: 以下是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

Note that not all of the elements are required . 请注意,并非所有元素都是必需的 Those which are optional have the minOccurs attribute set to 0 in the XML Schema. 那些是可选的,在XML Schema中将minOccurs属性设置为0

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

So, eventually this is how We call the method: 所以,最终我们称之为方法:

$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);

The AddDomesticConsignmentResponse is parsed as We parsed the LoginResponse according to its definition in the XML Schema document. AddDomesticConsignmentResponse被解析为我们根据XML Schema文档中的定义解析了LoginResponse

Well, I guess this is all to it. 好吧,我想这就是它的全部。 I didn't try it myself, but in theory it should work. 我自己没有尝试过,但理论上它应该可行。 Hope this helps. 希望这可以帮助。

UPDATE UPDATE

According to the documentation tracking the consignment should be as easy as doing the following: 根据文件跟踪,寄售应该像执行以下操作一样简单:

// 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
}

That's it! 而已!

Well Mr zafarkhaja helped me a lot but for others like me i will like to explain what worked for me so that might be helpfull to others too in future .. zafarkhaja先生帮助了我很多,但对于像我这样的人,我想解释一下对我有用的东西,以便将来也可能对他人有所帮助。

For dealing with complex types For Example Like in my Uk Mail Api For login i did this 用于处理复杂类型 例如在我的英国邮件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);

and hurray i could login . 并且我可以登录 it gave me a response which i needed to retreive 它给了我一个我需要回复的回应

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

and i got the auth token well sure you can change parameters according to your webservice 我得到了身份验证令牌,确保您可以根据您的网络服务更改参数

Well using a soap Client When You are not dealing with complex type 好吧,当你没有处理复杂类型时,使用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

Well i searched everwhere but no one had posted any working example well all the code works for me in php soap 1.1 with both complex type and without . 好吧,我搜索过的地方,但没有人发布任何工作的例子,所有的代码适用于我的PHP肥皂1.1复杂的类型和没有。

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

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