PHP的SOAP客户端为英国的邮件Web服务API? [英] php soap client for uk mail webservice api?

查看:167
本文介绍了PHP的SOAP客户端为英国的邮件Web服务API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作在其上的订单被放置在一个电子商务网站。要跟踪交付我需要给一个链接,用户与用户填写表单中所有的参数来创建一个交付和使用英国的邮件,在邮件中提供的链接跟踪其状态。

我必须使用英国邮件Web服务API。谁能告诉我该怎么办呢?我是新来的SOAP WSDL。

从我的理解我现在这样做如何走得更远?我下面的只是基本的客户端code,我需要:


  1. 认证登录和使用身份验证令牌


  2. 我需要发送的参数来创建一个国内转让


  3. 我需要跟踪交货状态太


这是我更新code:

 < PHP$ LoginWebRequest =新stdClass的();
$ LoginWebRequest->用户名='XXX着在这里展示XXX';
$ LoginWebRequest->密码='XXX着在这里展示XXX';//回声< pre>中;的print_r($ LoginWebRequest); < / pre>中;出口;$登录=新stdClass的();
$登录 - > loginWebRequest = $ LoginWebRequest;//回声< pre>中;的print_r($登录); < / pre>中;出口;$ SoapClient的=新SoapClient的('somewsdl WSDL?');
$ LoginResponse = $ soapClient->登录($登录);//回声< pre>中;的print_r($ LoginResponse); < / pre>中;出口;$ LoginResponse = $ soapClient->登录($登录);
// - 到这里我的code运行良好,也给出了失败的输出,但加上code //下面让我错误着找出什么是错
$ AuthenticationToken = $ LoginResponse-> LoginResult-> AuthenticationToken;$ AddDomesticConsignmentWebRequest =新stdClass的();
$ AddDomesticConsignmentWebRequest->用户名='XXXXXX';
//从previous步骤设置身份验证令牌
$ AddDomesticConsignmentWebRequest-> AuthenticationToken = $ AuthenticationToken;
//其他属性都在这里设置...$ AddDomesticConsignment =新stdClass的();
$ AddDomesticConsignment->请求= $ AddDomesticConsignmentWebRequest;
$ SoapClient的=新SoapClient的(的https:// SVC WSDL');
$ AddDomesticConsignmentResponse = $ soapClient-> AddDomesticConsignment($ AddDomesticConsignment);
?>

我已经解决了一切,得到了我的货物没有太只需跟踪我的API

我的XML是这样或U可以检查PDF

 示例XML请求:
    <肥皂:信封的xmlns:SOAP =htt​​p://www.w3.org/2003/05/soap-envelope的xmlns:锡尔=htt​​p://webapp-cl.internet-delivery.com/ThirdPartyIntegrationService&GT ;
    <肥皂:页眉/>
    <肥皂:身体与GT;
    <锡尔:ConsignmentTrackingSearchV1>
    <锡尔:用户名>&mail.com LT; /锡尔:用户名>
    <锡尔:密码> 123 LT; /锡尔:密码>
    <锡尔:令牌>< /锡尔:令牌>
    <锡尔:ConsignmentNumber> 01161< /锡尔:ConsignmentNumber>
    <锡尔:IsPartialConsignmentNumber>假LT; /锡尔:IsPartialConsignmentNumber>
    <锡尔:CustomerReference>< /锡尔:CustomerReference>
    <锡尔:IsPartialCustomerReference>假LT; /锡尔:IsPartialCustomerReference>
    <锡尔:DeliveryPost code>< /锡尔:DeliveryPost code>
    <锡尔:MailingID>< /锡尔:MailingID>
    &所述;锡尔:的maxResults> 100℃/锡尔:的maxResults>
    < /锡尔:ConsignmentTrackingSearchV1>
    < / SOAP:身体与GT;
    < / SOAP:信封>

例如XML响应

 <肥皂:信封的xmlns:SOAP =htt​​p://www.w3.org/2003/05/soap-envelope的xmlns:XSI =HTTP:// WWW .w3.org / 2001 / XML模式实例的xmlns:XSD =http://www.w3.org/2001/XMLSchema>
       <肥皂:身体与GT;
       < ConsignmentTrackingSearchV1Response的xmlns =HTTP://webapp-cl.internet- delivery.com/ThirdPartyIntegrationService\">
        < ConsignmentTrackingSearchV1Result>
        < ResultState>&成功LT; / ResultState>
         < ConsignmentResults>
       < ConsignmentSearchResult>
    < ConsignmentNumber> 001161< / ConsignmentNumber>
    < CustomerRef1 />
   < CustomerRef2 />
     < SubCustomerRef1 />
     < SubCustomerRef2 />
      < D​​eliveryType />
      < ConsignmentStatus>发出和LT; / ConsignmentStatus>
      < D​​ateTimeDelivered> 2010-02-11T12:00:00 + 00:00< / DateTimeDelivered>
      &所述; ItemsDelivered→2&下; / ItemsDelivered>
      <&RECIPIENTNAME GT;罗宾< / RECIPIENTNAME>
      < D​​eliveryComments />
      &所述; ExpectedDeliveryDate> 2010-02-11T00:00:00&下; / ExpectedDeliveryDate>
       <提交服务>接下来白天< /提交服务>
      &所述; TOTALITEMS→2&下; / TOTALITEMS>
      &所述; Consignmentkey> 22℃; / Consignmentkey>
        < / ConsignmentSearchResult>
         < / ConsignmentResults>
        < / ConsignmentTrackingSearchV1Result>
        < / ConsignmentTrackingSearchV1Response>
        < / SOAP:身体与GT;
       < / SOAP:信封>


解决方案

简介

显然,文档丢失在这里。不幸的是, $ soapClient-> __ getTypes()并没有太多说不清。它仅示出了由网络服务支持的可用的复杂类型,但它没有告诉我们它们之间的关系。即使你有与 $ soapClient-和返回自己的输入和输出类型的所有可用操作的列表GT; __ getFunctions(),也不能保证你可以在不知道进行复杂类型或无需任何文件的确切性质。不过幸运的是,这是提供了一个WSDL文档基于SOAP的Web服务。 WSDL文档描述了所有支持的操作和复杂类型,以及它们之间的关系。因此,我们可以找出如何,仅通过WSDL文档来使用该服务。

有检查WSDL文档的两种方式:


  1. 生成WSDL文档工件(客户端类),并检查工件

  2. 期待通过WSDL文档和XML Schema文档。

1。文物

的工件可以通过如Java或C#的强类型语言提供的工具来生成。在<一个href=\"https://qa-api.ukmail.com/Services/UKMAuthenticationServices/\">https://qa-api.ukmail.com/Services/UKMAuthenticationServices/页面显示使用 svcutil.exe的工具生成的C#编程语言的文物,或者您也可以使用的wsimport 工具来生成Java编程语言中的文物。我怀疑有可能产生的工件为PHP编程语言的好工具。

2。 WSDL文档和XML Schema

如果你不熟悉C#或Java,你总是可以通过它和XML架构看检查WSDL文档。 XML模式可以包含在WSDL文档中或从外部文件导入。虽然WSDL文档描述,可以在Web服务上执行的操作,XML架构描述复杂的类型和它们之间的关系。

动作

我写的绪论部分,让你知道该怎么做你自己的。下面我想表明它的一个例子。对于检查我用这两种方法的WSDL文档的目的。首先我生成使用的wsimport 工具,那么我读了很多XML的文物。

该服务的WSDL文档使用导入语句分为几档。因此,为了找到所有的操作和复杂类型你必须遵循的导入语句。

验证

如果我们看一下验证服务的WSDL文件(<一个href=\"https://qa-api.ukmail.com/Services/UKMAuthenticationServices/UKMAuthenticationService.svc?wsdl\">location),我们可以看到,它导入另一个WSDL文档:

 &LT; WSDL:进口命名空间=htt​​p://tempuri.org/ location=\"https://qa-api.ukmail.com/Services/UKMAuthenticationServices/UKMAuthenticationService.svc?wsdl=wsdl1\"/>

后者(<一href=\"https://qa-api.ukmail.com/Services/UKMAuthenticationServices/UKMAuthenticationService.svc?wsdl=wsdl1\">location),反过来,进口另一个问题:

 &LT; WSDL:进口命名空间=htt​​p://www.UKMail.com/Services/Contracts/ServiceContracts location=\"https://qa-api.ukmail.com/Services/UKMAuthenticationServices/UKMAuthenticationService.svc?wsdl=wsdl0\"/>

最后一节(<一个href=\"https://qa-api.ukmail.com/Services/UKMAuthenticationServices/UKMAuthenticationService.svc?wsdl=wsdl0\">location),进口所有相关的XML架构:

 &LT; WSDL:类型&GT;
  &LT; XSD:架构的targetNamespace =htt​​p://www.UKMail.com/Services/Contracts/ServiceContracts/Imports&GT;
    &LT; XSD:进口schemaLocation=\"https://qa-api.ukmail.com/Services/UKMAuthenticationServices/UKMAuthenticationService.svc?xsd=xsd0\"命名空间=htt​​p://www.UKMail.com/Services/Contracts/ServiceContracts/&GT;
    &LT; XSD:进口schemaLocation=\"https://qa-api.ukmail.com/Services/UKMAuthenticationServices/UKMAuthenticationService.svc?xsd=xsd1\"命名空间=htt​​p://schemas.microsoft.com/2003/10/Serialization//&GT;
    &LT; XSD:进口schemaLocation=\"https://qa-api.ukmail.com/Services/UKMAuthenticationServices/UKMAuthenticationService.svc?xsd=xsd2\"命名空间=htt​​p://www.UKMail.com/Services/Contracts/DataContracts/&GT;
    &LT; XSD:进口schemaLocation=\"https://qa-api.ukmail.com/Services/UKMAuthenticationServices/UKMAuthenticationService.svc?xsd=xsd3\"命名空间=htt​​p://schemas.datacontract.org/2004/07/UKMWebAPICommon.WebResponses/&GT;
  &LT; / XSD:架构&GT;
&LT; / WSDL:类型&GT;

这也说明它也可以看作操作通过调用 $ soapClient-&GT; __ getFunctions()

 阵列

    [0] =&GT; LoginResponse登录(登录$参数)
    [1] =&GT; LogoutResponse退出(注销$参数)

在这里,我们看到登录()操作接受 $参数类型登录作为它的参数和返回类型的响应 LoginResponse 。这是它的外观在WSDL文档中:

 &LT; WSDL:操作名称=登陆GT&;
  &LT; WSDL:输入WSAW:行动=htt​​p://www.UKMail.com/Services/IUKMAuthenticationService/Login消息=TNS:IUKMAuthenticationService_Login_InputMessage/&GT;
  &LT; WSDL:输出wsaw:Action=\"http://www.UKMail.com/Services/Contracts/ServiceContracts/IUKMAuthenticationService/LoginResponse\"消息=TNS:IUKMAuthenticationService_Login_OutputMessage/&GT;
&LT; / WSDL:运转&gt;

登录是一个复杂的类型,这可以在导入XML Schema文档中的一个可以看到(<一个href=\"https://qa-api.ukmail.com/Services/UKMAuthenticationServices/UKMAuthenticationService.svc?xsd=xsd0\">schemaLocation):

 &LT; XS:元素的名称=登陆GT&;
  &LT; XS:复杂类型&GT;
    &LT; XS:序列&GT;
      &LT; XS:元素的xmlns:Q1 =htt​​p://www.UKMail.com/Services/Contracts/DataContracts的minOccurs =0NA​​ME =loginWebRequest的nillable =真TYPE =Q1:LoginWebRequest/&GT ;
    &LT; / XS:序列&GT;
  &LT; / XS:复杂类型&GT;
&LT; / XS:组件&gt;

它有一个名为 loginWebRequest 元素也是 LoginWebRequest 称为复杂类型是在另一个导入的XML描述架构:

 &LT; XS:复杂类型名称=LoginWebRequest&GT;
  &LT; XS:序列&GT;
    &LT; XS:元素的名称=密码的nillable =真类型=XS:字符串/&GT;
    &LT; XS:元素的名称=用户名的nillable =真类型=XS:字符串/&GT;
  &LT; / XS:序列&GT;
&LT; / XS:复杂类型&GT;

LoginWebRequest 更简单。它有两个的简单的类型用户名密码类型字符串

在PHP复杂类型重新由 stdClass的的对象psented $ P $。因此,为了调用登录()运行,我们必须创建两个对象登录 LoginWebRequest

  $ LoginWebRequest =新stdClass的();
$ LoginWebRequest-&GT;用户名='用户名';
$ LoginWebRequest-&GT;密码='P @ $$ w0rd';$登录=新stdClass的();
$登录 - &GT; loginWebRequest = $ LoginWebRequest;$ SoapClient的=新SoapClient('https://qa-api.ukmail.com/Services/UKMAuthenticationServices/UKMAuthenticationService.svc?wsdl');
$ LoginResponse = $ soapClient-&GT;登录($登录);

这给了我们键入 LoginResponse 的结果是:

 &LT; XS:元素的名称=LoginResponse&GT;
  &LT; XS:复杂类型&GT;
    &LT; XS:序列&GT;
      &LT; XS:元素的xmlns:Q2 =htt​​p://www.UKMail.com/Services/Contracts/DataContracts的minOccurs =0NA​​ME =LoginResult的nillable =真TYPE =Q2:UKMLoginResponse/&GT ;
    &LT; / XS:序列&GT;
  &LT; / XS:复杂类型&GT;
&LT; / XS:组件&gt;

,其中包含 LoginResult 具有类型命名的元素 UKMLoginResponse

 &LT; XS:复杂类型名称=UKMLoginResponse&GT;
  &LT; XS:复杂内容混合=false的&GT;
    &LT; XS:扩展基板=TNS:UKMWebResponse&GT;
      &LT; XS:序列&GT;
        &LT; XS:元素的minOccurs =0NA​​ME =帐户的nillable =真TYPE =TNS:ArrayOfAccountWebModel/&GT;
        &LT; XS:元素的名称=AuthenticationToken的nillable =真类型=XS:字符串/&GT;
      &LT; / XS:序列&GT;
    &LT; / XS:扩展&GT;
  &LT; / XS:复杂内容&GT;
&LT; / XS:复杂类型&GT;

UKMLoginResponse 都有自己的帐户类型 ArrayOfAccountWebModel AuthenticationToken 字符串 UKMWebResponse (注意扩展语句)错误 ArrayOfUKMWebError 类型的警告 ArrayOfUKMWebWarning 结果的键入 UKMResultState

 &LT; XS:复杂类型名称=UKMWebResponse&GT;
  &LT; XS:序列&GT;
    &LT; XS:元素的minOccurs =0NA​​ME =错误的nillable =真TYPE =TNS:ArrayOfUKMWebError/&GT;
    &LT; XS:元素的xmlns:Q1 =htt​​p://schemas.datacontract.org/2004/07/UKMWebAPICommon.WebResponsesNAME =结果TYPE =Q1:UKMResultState/&GT;
    &LT; XS:元素的minOccurs =0NA​​ME =警告的nillable =真TYPE =TNS:ArrayOfUKMWebWarning/&GT;
  &LT; / XS:序列&GT;
&LT; / XS:复杂类型&GT;

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

 公共类UKMLoginResponse扩展UKMWebResponse {...}

所以,为了得到从身份验证令牌的 LoginResponse ,我们做到以下几点:

  $ LoginResponse = $ soapClient-&GT;登录($登录);
$ AuthenticationToken = $ LoginResponse-&GT; LoginResult-&GT; AuthenticationToken;

调用方式

我也不会很具体的这里,因为它非常类似于我们上面那样。

作为一个例子,让我们叫 AddDomesticConsignment()方法。根据寄售服务的WSDL文件和 $ soapClient-&GT返回的结果; __ getFunctions() AddDomesticConsignment()方法需要一个 $参数类型的参数 AddDomesticConsignment ,并返回类型的结果 AddDomesticConsignmentResponse 。通过分析 AddDomesticConsignment 复杂类型,我们可以看到,它有命名的元素要求类型 AddDomesticConsignmentWebRequest 延伸 AddConsignmentWebRequest 这本身延伸的WebRequest 。以下是所有的 AddDomesticConsignmentWebRequest 类型的元素列表:

  // AddDomesticConsignmentWebRequest自己的元素
布尔BookIn
十进制CODAmount
串ConfirmationEmail
串ConfirmationTelephone
布尔ExchangeOnDelivery
INT ExtendedCover
布尔LongLength
preDeliveryNotificationType preDeliveryNotification
串SecureLocation1
串SecureLocation2
布尔SignatureOptional//元素从AddConsignmentWebRequest inhereted
串账户号码
AddressWebModel地址
串AlternativeRef
串BUSINESSNAME
串CollectionJobNumber
布尔ConfirmationOfDelivery
串联系人姓名
串CustomersRef
字符串电子邮件
INT项目
INT的serviceKey
串SpecialInstructions1
串SpecialInstructions2
串电话
小数重量//从元素的WebRequest inhereted
字符串用户名
串AuthenticationToken

请注意,并非所有的元素都需要。那些可选在XML模式设置为 0 的minOccurs 属性。

 &LT; XS:元素的minOccurs =0NA​​ME =preDeliveryNotificationTYPE =TNS:preDeliveryNotificationType/&GT;

所以,最终这是我们如何调用该方法:

  $ AddDomesticConsignmentWebRequest =新stdClass的();
$ AddDomesticConsignmentWebRequest-&GT;用户名='用户名';
//从previous步骤设置身份验证令牌
$ AddDomesticConsignmentWebRequest-&GT; AuthenticationToken = $ AuthenticationToken;
//其他属性都在这里设置...$ AddDomesticConsignment =新stdClass的();
$ AddDomesticConsignment-&GT;请求= $ AddDomesticConsignmentWebRequest;$ SoapClient的=新SoapClient('https://qa-api.ukmail.com/Services/UKMConsignmentServices/UKMConsignmentService.svc?wsdl');
$ AddDomesticConsignmentResponse = $ soapClient-&GT; AddDomesticConsignment($ AddDomesticConsignment);

AddDomesticConsignmentResponse 被解析为我们的 LoginResponse 根据它在XML Schema文档中定义的解析。

好吧,我想这是所有它。我没有尝试它自己,但在理论上它应该工作。
希望这有助于。

更新

根据文档跟踪货物应该是因为这样做容易如下:

  //创建SOAP客户端
$ SoapClient的=新SoapClient的(的http:// Web服务/ WSDL');
//调用`ConsignmentTrackingSearchV1`方法,并通过搜索参数
$ ConsignmentTrackingSearchV1Response = $ soapClient-&GT; ConsignmentTrackingSearchV1(
    mail.com',//用户名
    '123',//密码
    '',//令牌
    '01161',// ConsignmentNumber
    '假',// IsPartialConsignmentNumber
    '',// CustomerReference
    '假'// IsPartialCustomerReference
    '',// DeliveryPost code
    '',// MailingID
    100 //的maxResults
);
//解析响应
$ ConsignmentTrackingSearchV1Result = $ ConsignmentTrackingSearchV1Response-&GT; ConsignmentTrackingSearchV1Result;
$ ResultState = $ ConsignmentTrackingSearchV1Result-&GT; ResultState; //成功
$ ConsignmentResults = $ ConsignmentTrackingSearchV1Result-&GT; ConsignmentResults;
//通过`ConsignmentResults`循环
的foreach($ ConsignmentResults为$ ConsignmentSearchResult){
    $ ConsignmentNumber = $ ConsignmentSearchResult-&GT; ConsignmentNumber;
    $ ConsignmentStatus = $ ConsignmentSearchResult-&GT; ConsignmentStatus;
    //其他属性
}

这就是它!

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. Can anyone show me how to do so? I am new to 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

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

      <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. It only shows available complex types supported by a web service, but it doesn't show us the relationship among them. 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. But fortunately, this is a SOAP-based web service that provides a WSDL document. The WSDL document describes all the supported operations and complex types, as well as their relationships. So, we can figure out how to use the service by only examining the WSDL document.

There are two ways of examining the WSDL document:

  1. generate artifacts (client classes) from the WSDL document and examine the artifacts
  2. look through the WSDL document and the XML Schema documents.

1. Artifacts

The artifacts can be generated by tools provided by the strong typed languages like Java or 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. I doubt that there can be any good tool for generating the artifacts for the PHP programming language.

2. WSDL document and XML Schemas

If You're not familiar with C# or Java, You can always examine the WSDL document by looking through it and the XML Schemas. The XML Schemas can be included in the WSDL document or imported from an external file. 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.

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. First I generated the artifacts using the wsimport tool, then I read a lot of XML.

The WSDL document for this service is divided into several files using import statements. So, in order to find all the operations and complex types You have to follow the import statements.

Authentication

If We look at the Authentication Service's WSDL document (location), We can see that it imports another WSDL document:

<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:

<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():

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. This is how it looks in the WSDL document:

<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):

<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:

<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. It has two simple types Username and Password of type String.

In PHP complex types are represented by objects of stdClass. So, in order to call the Login() operation, We have to create two objects Login and LoginWebRequest:

$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:

<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:

<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:

<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:

public class UKMLoginResponse extends UKMWebResponse { ... }

So, in order to get the authentication token from the LoginResponse, we do the following:

$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. 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. 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. Following is the list of all of the elements of the AddDomesticConsignmentWebRequest type:

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

<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.

Well, I guess this is all to it. I didn't try it myself, but in theory it should work. Hope this helps.

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!

这篇关于PHP的SOAP客户端为英国的邮件Web服务API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆