皇家邮政API V2

时间:2016-03-31 15:01:24

标签: php

我尝试使用皇家邮政API V2,我会修改部分@flowers for martians代码,我看到了这个错误" E0004失败的架构验证"

任何人使用皇家邮政API V2?

   stdClass Object
(
    [exceptionDetails] => stdClass Object
        (
            [exceptionCode] => E0004
            [exceptionText] => Failed Schema Validation
        )

)





 <?php
//ini_set('soap.wsdl_cache_enabled', '1');
ini_set('default_socket_timeout', 120); 
ini_set('soap.wsdl_cache_enabled',0);
ini_set('soap.wsdl_cache_ttl',0);

class royalmaillabelRequest
{
    private $apiapplicationid           = "xxxx1";
    private $api_password               = "xxxxx!"; 
    private $api_username               = "xxxx"; //"mailAPI"
    private $api_certificate_passphrase = "xxx";
    private $clientid                   = 'xxxx';
    private $api_service_enhancements   = "";//14 "E-Mail Notification"
    private $DigestPass                 = '';
    private $locationforrequest         = 'https://api.royalmail.net/shipping/v2';

    private function preparerequest(){



            //PASSWORD DIGEST
            $date               = gmdate('Y-m-d\TH:i:s\Z');
            $nonce              = mt_rand();
            $nonce_date_pwd     = pack("A*",$nonce) . pack("A*",$date) . pack("H*", sha1($DigestPass));
            $passwordDigest     = base64_encode(pack('H*',sha1($nonce_date_pwd)));
            $ENCODEDNONCE       = base64_encode($nonce);



            //SET CONNECTION DETAILS

            $soapclient_options = array();
            $soapclient_options['cache_wsdl'] = 'WSDL_CACHE_NONE';
            $soapclient_options['stream_context']= stream_context_create(
                            array(
                                'http' =>
                                    array(
                                        'header'           => implode(
                                            "\r\n",
                                            array(
                                                'Accept: application/soap+xml',
                                                'X-IBM-Client-Id: ' . $this->clientid,
                                                'X-IBM-Client-Secret: ' . $this->api_certificate_passphrase,
                                            )
                                        ),
                                    ),
                            )
                 );                                         
                #
                #
                $soapclient_options['trace']        = true;
                $soapclient_options['ssl_method']   = 'SOAP_SSL_METHOD_SSLv3';
                $soapclient_options['location']     = $this->locationforrequest;
                $soapclient_options['soap_version'] = 'SOAP_1_1';// SOAP_1_2
                #
                #
                //launch soap client
                $client = new SoapClient(dirname(__FILE__) . "/ShippingAPI_V2_0_9/ShippingAPI_V2_0_9.wsdl", $soapclient_options);
                $client->__setLocation($soapclient_options['location']);
                #
                #
                //headers needed for royal mail//D8D094Fd2716E3Es142588808s317
                $HeaderObjectXML  = '<wsse:Security xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
                                          xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
                                 <wsse:UsernameToken wsu:Id="UsernameToken-SFSDFSDFY123200401">
                                        <wsse:Username>'.$this->api_username.'</wsse:Username>
                                        <wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest">'.$passwordDigest.'</wsse:Password>
                                        <wsse:Nonce EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">'.$ENCODEDNONCE.'</wsse:Nonce>
                                        <wsu:Created>'.$created.'</wsu:Created>
                                 </wsse:UsernameToken>
                           </wsse:Security>'; 


        //push the header into soap
        $HeaderObject = new SoapVar( $HeaderObjectXML, XSD_ANYXML);                           
        //push soap header
        $header = new SoapHeader( 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd', 'Security', $HeaderObject );
        $client->__setSoapHeaders($header);
        return $client;

    }

    public function CreateShippiment($data){

        $request = $this->buildCreateshippiment($data);
        $type    = 'createShipment';
        return $this->makerequest($type, $request);

    }

    public function PrintLabel($shipmentNumber,$order_tracking_id){

        $time = gmdate('Y-m-d\TH:i:s');
        $request = array(
                'integrationHeader' => array(
                    'dateTime' => $time,
                    'version' => '2',
                    'identification' => array(
                        'applicationId' => $this->apiapplicationid,
                        'transactionId' => $order_tracking_id
                    )
                ),
                'shipmentNumber' => $shipmentNumber,
                'outputFormat' => 'PDF',
        );
        $type = 'printLabel';
        $response = $this->makerequest($type, $request);
        return $response->label;

    }

    private function makerequest($type, $request){

        $client = $this->preparerequest();
        $response = false;
        $times = 1;


        while(true){
            try {
                $response = $client->__soapCall($type, array($request), array('soapaction' => $this->locationforrequest) );
                //echo "REQUEST:\n" . htmlentities($client->__getLastResponse()) . "\n";
                break;
            }catch (Exception $e) {


                print_r($client->__getLastRequest());
                if($e->detail->exceptionDetails->exceptionCode == "E0010" && $times <= 25){
                        sleep(1.5);                     
                        $times++;
                        continue;                       
                            }else{
                                echo $e->getMessage();
                                echo "<pre>";
                                print_r($e->detail);
                                echo $client->__getLastResponse();
                                echo "REQUEST:\n" . htmlentities($client->__getLastResponse()) . "\n";
                                break;
                            }           
                        }
                    break;
            }
            return $response;
    }

    private function buildCreateshippiment($data2) {

            $time = gmdate('Y-m-d\TH:i:s');

            $data = new ArrayObject();
            foreach ($data2 as $key => $value)
            {
                $data->$key = $value;
            }
            $request = array(
                'integrationHeader' => array(
                    'dateTime' => $time,
                    'version' => '2',
                    'identification' => array(
                        'applicationId' => $this->apiapplicationid,
                        'transactionId' => $data->order_tracking_id
                    )
                ),
                'requestedShipment' => array(
                'shipmentType' => array('code' => 'Delivery'),
                'serviceOccurrence' => 1,
                'serviceType' => array('code' => $data->api_service_type),

                'serviceOffering' => array('serviceOfferingCode' => array('code' => $data->api_service_code)),

                'serviceFormat' => array('serviceFormatCode' => array('code' => $data->api_service_format)),
                'shippingDate' => date('Y-m-d'),

                'recipientContact' => array('name' => $data->shipping_name, 'complementaryName' => $data->shipping_company, 'telephoneNumber' => array('countryCode' => '0044', 'telephoneNumber' => $data->telephoneNumber), 'electronicAddress' =>  array('electronicAddress'=>$data->electronicAddress)),

                'recipientAddress' => array('addressLine1' => $data->shipping_address1, 'postTown' => $data->shipping_town, 'postcode' => $data->shipping_postcode, 'country'=>array('countryCode'=>array('code'=>$data->countryCode))),

                'items' => array('item' => array(
                           'numberOfItems' => $data->order_tracking_boxes,
                           'weight' => array( 'unitOfMeasure' => array('unitOfMeasureCode' => array('code' => 'g')),
                           'value' => $data->order_tracking_weight,
                           ),
                        //'offlineShipments'=>array('itemID'=>$data->itemID, 'status'=>array('status' => array('statusCode'=>$data->statusCode), 'validFrom'=>$time)),
                        )
                    ),
                 //'signature' => 0,

                'customerReference' => $data->customerReference, 
                'senderReference' => $data->senderReference
                )
            );



        //if($data->api_service_enhancements == 6 && $data->api_service_type == 1){
          if($api_service_enhancements != "" && strlen($data->electronicAddress)>5) {
                    $request['requestedShipment']['serviceEnhancements'] = array('enhancementType' => array('serviceEnhancementCode' => array('code' => $data->api_service_enhancements)));
            }

        return $request;

    }
}

$array = array(
                'order_tracking_id'     =>'', 
                'api_service_type'      =>'T', /// serviceOccurrence
                'api_service_code'      =>'TPN', // TPN => 24 Tracked,  TPS => 48 Tracked
                'api_service_format'    =>'N',  // serviceFormat
                #
                #
                'countryCode'           =>'GB',
                'order_tracking_boxes'  =>'1',
                'order_tracking_weight' =>'990',
                #
                #
                'shipping_name'         =>'MR John Doe',
                'shipping_company'      =>'Doe',
                'shipping_address1'     =>'12 gascony avenue',
                'shipping_town'         =>'London', 
                'shipping_postcode'     =>'nw6 4na',
                #
                #
                'telephoneNumber'       => '07234345500',
                'electronicAddress'     => 'johndoe@gmail.com',

                'customerReference'     => 'BY00001',
                'senderReference'       => 'BY00002'
);

$request    = new royalmaillabelRequest(); 
$response   = $request->CreateShippiment($array);
?>

1 个答案:

答案 0 :(得分:0)

您无法将订单跟踪ID设置为空并放置并检查!