在php中发送dpd.com的soap请求

Posted

技术标签:

【中文标题】在php中发送dpd.com的soap请求【英文标题】:send soap request for dpd.com in php 【发布时间】:2016-09-01 02:46:24 【问题描述】:

我正在尝试从 dpd.com 获取运输标签。为此,我需要使用肥皂来完成任务。我已经完成了登录认证并获得了 AuthToken。 这是它的代码。

<?php
$c = new SoapClient('https://public-ws-stage.dpd.com/services/LoginService/V2_0/?WSDL');
$res = $c->getAuth(array(
    'delisId' => 'username',
    'password' => 'password',
    'messageLanguage' => 'en-us',
));
$authToken = $res->return->authToken;

现在,问题是我想通过发送请求并使用此 AuthToken 来获取运输标签。 肥皂请求的格式是这样的。

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                  xmlns="http://dpd.com/common/service/types/Authentication/2.0"
                  xmlns1="https://public-ws-stage.dpd.com/services/ShipmentService/V3_2/?wsdl">
    <soapenv:Header>
        <ns:authentication>
            <delisId>username</delisId>
            <authToken>AuthToken From Above</   authToken>
            <messageLanguage>en-us</messageLanguage>
        </ns:authentication>
    </soapenv:Header>
    <soapenv:Body>
        <ns1:storeOrders>
            <paperFormat>A4</paperFormat>
            <order>
                <generalShipmentData>
                    <sendingDepot>'.$depot_num.'</sendingDepot>
                    <product>CL</product>
                    <mpsCompleteDeliver>false</mpsCompleteDeliver>
                    <sender>
                        <name1>Fritz</name1>
                        <street>Mustergasse</street>
                        <houseNo>1</houseNo>
                        <state>BY</state>
                        <country>DE</country>
                        <zipCode>53950</zipCode>
                        <city>Maibach</city>
                    </sender>
                    <recipient>
                        <name1></name1>
                        <street></street>
                        <houseNo></houseNo>
                        <state></state>
                        <country></country>
                        <zipCode></zipCode>
                        <city></city>
                        </recipient>
                </generalShipmentData>
                <parcels>
                    <parcelLabelNumber></parcelLabelNumber>
                </parcels>
                <productAndServiceData>
                    <orderTyp></orderType>
                </productAndServiceData>
            </order>
        </ns1:storeOrdes>
    </soapenv:Body> 
</soapenv:Envelope>

但我不知道如何发送此请求并在 pdfData 标记中获取响应。

【问题讨论】:

【参考方案1】:

我看到这个问题很老,但其他人也可以搜索。 来自http://labor.99grad.de/2014/10/05/deutscher-paket-dienst-dpd-soap-schnittstelle-mit-php-nutzen-um-versandetikett-als-pdf-zu-generieren/的回答

<?php

   // Einloggen

   $c = new SoapClient('https://public-ws-stage.dpd.com/services/LoginService/V2_0?wsdl');

   $res = $c->getAuth(array(
      'delisId'          => 'your-Id',
      'password'         => 'your-Password',
      'messageLanguage'   => 'de_DE'
   ));

   // ...und das Token merken
   $auth = $res->return;


   // Jetzt das Label generieren:

   $c = new SoapClient('https://public-ws-stage.dpd.com/services/ShipmentService/V3_1?wsdl');

   $token = array(
      'delisId'         => $auth->delisId,
      'authToken'         => $auth->authToken,
      'messageLanguage'   => 'de_DE'
   );

   // Set the header with the authentication token
   $header = new SOAPHeader('http://dpd.com/common/service/types/Authentication/2.0', 'authentication', $token);
   $c->__setSoapHeaders($header);

   try 
      $res = $c->storeOrders( array
         (
            "printOptions" => array(
               "paperFormat" => "A4",
               "printerLanguage" => "PDF"
            ),
            "order" => array(
               "generalShipmentData" => array(
                  "sendingDepot" => $auth->depot,
                  "product" => "CL",
                  "mpsCompleteDelivery" => false,
                  "sender" => array(
                     "name1" => "Sender Name",
                     "street" => "Sender Street 2",
                     "country" => "DE",
                     "zipCode" => "65189",
                     "city" => "Wiesbaden",
                     "customerNumber" => "123456789"
                  ),
                  "recipient" => array(
                     "name1" => "John Malone",
                     "street" => "Johns Street 34",
                     "country" => "DE",
                     "zipCode" => "65201",
                     "city" => "Wiesbaden"
                  )
               ),
               "parcels" => array(
                  "parcelLabelNumber" => "09123829120"
               ),
               "productAndServiceData" => array(
                  "orderType" => "consignment"
               )
            )
         )
      );
    catch (SoapFault $exception) 
      echo $exception->getMessage();
      die();
   

   // Et voilà!

   header('Content-type: application/pdf');
   echo $res->orderResult->parcellabelsPDF;
?>

【讨论】:

以上是关于在php中发送dpd.com的soap请求的主要内容,如果未能解决你的问题,请参考以下文章

在 Qt 中发送 SOAP 请求时的 ProtocolUnknownError

来自 Siebel 的 SOAP 请求在每个元素中都有命名空间

如何使用 Flex 在 ISO-8859-1 中发送 SOAP 请求?

使用node-soap发送正确的soap请求

在 SOAP UI 请求中将 cookie 作为请求标头发送以获取 REST Web 服务

使用SSL的Node中的SOAP请求