且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用的PHP api.dpd.co.uk API集成

更新时间:2023-02-07 15:24:04

尽管有人可能会发现这很有用 - 夫妇的音符 - - 你需要把你的工作的api.dpd.co.uk不同的方法这家AA负荷在账号 - 和你的用户名和密码 - 也创造出货量确保collectionDate是在未来或它只是抛出一个通用应用程序错误。

Though someone might find this useful - this a a load of different methods working for the api.dpd.co.uk - couple of notes - you need to put your account number in - and your username and password - also for creating shipments make sure collectionDate is in the future or it just throws a General Application Error..

<?php
$BASE="https://api.dpd.co.uk";

$method = '/user/?action=login';

$url = $BASE.$method;

$options = array(
    'http' => array(
        'method'  => 'POST',
        'Host'  => 'api.dpd.co.uk',
        'header'=>  "Content-Type: application/json\r\n" .
                    "Accept: application/json\r\n".
                    "Authorization: Basic ". base64_encode("user:pass") ."\r\n".
                    "GEOClient: account/123456\r\n".
                    "Content-Length: 0"
      )
);


$context     = stream_context_create($options);

$result      = file_get_contents($url, false, $context);
$response    = json_decode($result);
//echo var_dump($response);

$data=(json_decode($result,true));
$session=$data['data']['geoSession'];
echo $session;

//$session="MTAuMjYuMy4yMDd8LTczODMzNzE4MA==";


// get some services...
/*
/shipping/network/?collectionDetails.address.locality=Birmingham&collectionDetails.
address.county=West%20Midlands&collectionDetails.address.postcode=B661BY&collection
Details.address.countyCode=GB&deliveryDetails.address.locality=Birmingham&deliveryD
etails.address.county=West%20Midlands&deliveryDetails.address.postcode=B11AA&delive
ryDetails.address.countyCode=GB&deliveryDirection=1&numberOfParcels=1&totalWeight=5
&shipmentType=0 HTTP/1.1
Host: api.dpd.co.uk
Accept: application/json
GEOClient: account/123456
GEOSession: 1234567890ABCDEFGHIJK

*/

$method = '/shipping/network/?collectionDetails.address.locality=Birmingham&collectionDetails.address.county=West%20Midlands&collectionDetails.address.postcode=B661BY&collectionDetails.address.countyCode=GB&deliveryDetails.address.locality=Birmingham&deliveryDetails.address.county=West%20Midlands&deliveryDetails.address.postcode=B11AA&deliveryDetails.address.countyCode=GB&deliveryDirection=1&numberOfParcels=1&totalWeight=5&shipmentType=0&collectionDetails.address.countryCode=GB&deliveryDetails.address.countryCode=GB';

$url = $BASE.$method;

$options = array(
    'http' => array(
        'method'  => 'GET',
        'Host'  => 'api.dpd.co.uk',
        'header'=>  "Content-Type: application/json\r\n" .
                    "Accept: application/json\r\n".
                    "GEOClient: account/123456\r\n".
                    "GEOSession: ".$session."\r\n".
                    "Content-Length: 0"
      )
);

$context     = stream_context_create($options);

$result      = file_get_contents($url, false, $context);
$response    = json_decode($result);
//echo var_dump($response);

$data=(json_decode($result,true));
var_dump($data);

//

$method = '/shipping/country/GB';

$url = $BASE.$method;

$options = array(
    'http' => array(
        'method'  => 'GET',
        'Host'  => 'api.dpd.co.uk',
        'header'=>  "Content-Type: application/json\r\n" .
                    "Accept: application/json\r\n".
                    "GEOClient: account/123456\r\n".
                    "GEOSession: ".$session."\r\n".
                    "Content-Length: 0"
      )
);

$context     = stream_context_create($options);

$result      = file_get_contents($url, false, $context);
$response    = json_decode($result);
//echo var_dump($response);

$data=(json_decode($result,true));
var_dump($data);

$method = '/shipping/country/GB';

$url = $BASE.$method;

$options = array(
    'http' => array(
        'method'  => 'GET',
        'Host'  => 'api.dpd.co.uk',
        'header'=>  "Content-Type: application/json\r\n" .
                    "Accept: application/json\r\n".
                    "GEOClient: account/123456\r\n".
                    "GEOSession: ".$session."\r\n".
                    "Content-Length: 0"
      )
);

$context     = stream_context_create($options);

$result      = file_get_contents($url, false, $context);
$response    = json_decode($result);
//echo var_dump($response);

$data=(json_decode($result,true));
var_dump($data);

$method = '/shipping/country';

$url = $BASE.$method;

$options = array(
    'http' => array(
        'method'  => 'GET',
        'Host'  => 'api.dpd.co.uk',
        'header'=>  "Content-Type: application/json\r\n" .
                    "Accept: application/json\r\n".
                    "GEOClient: account/123456\r\n".
                    "GEOSession: ".$session."\r\n".
                    "Content-Length: 0"
      )
);

$context     = stream_context_create($options);

$result      = file_get_contents($url, false, $context);
$response    = json_decode($result);
//echo var_dump($response);

$data=(json_decode($result,true));
var_dump($data);

// /shipping/network/812/

$method = '/shipping/network/812/';

$url = $BASE.$method;

$options = array(
    'http' => array(
        'method'  => 'GET',
        'Host'  => 'api.dpd.co.uk',
        'header'=>  "Content-Type: application/json\r\n" .
                    "Accept: application/json\r\n".
                    "GEOClient: account/123456\r\n".
                    "GEOSession: ".$session."\r\n".
                    "Content-Length: 0"
      )
);

$context     = stream_context_create($options);

$result      = file_get_contents($url, false, $context);
$response    = json_decode($result);
//echo var_dump($response);

$data=(json_decode($result,true));
var_dump($data);


// /shipping/shipment

$method = '/shipping/shipment';

$url = $BASE.$method;
$json='{
                        "job_id": null,
                        "collectionOnDelivery": false,
                        "invoice": null,
                        "collectionDate": "2014-11-13T016:00:00",
                        "consolidate": false,
                        "consignment": [{
                            "consignmentNumber": null,
                            "consignmentRef": null,
                            "parcels": [],
                            "collectionDetails": {
                            "contactDetails": {
                            "contactName": "My Contact",
                            "telephone": "0121 500 2500"
                            },
                            "address": {
                            "organisation": "GeoPostUK Ltd",
                            "countryCode": "GB",
                            "postcode": "B66 1BY",
                            "street": "Roebuck Lane",
                            "locality": "Smethwick",
                            "town": "Birmingham",
                            "county": "West Midlands"
                            }
                            },
                            "deliveryDetails": {"contactDetails": {
                            "contactName": "My Contact",
                            "telephone": "0121 500 2500"
                            },
                            "address": {
                            "organisation": "GeoPostUK Ltd",
                            "countryCode": "GB",
                            "postcode": "B66 1BY",
                            "street": "Roebuck Lane",
                            "locality": "Smethwick",
                            "town": "Birmingham",
                            "county": "West Midlands"
                            },
                            "notificationDetails": {
                            "email": "my.email@geopostuk.com",
                            "mobile": "07921000001"
                            }
                            },
                            "networkCode": "1^01",
                            "numberOfParcels": 1,
                            "totalWeight": 5,
                            "shippingRef1": "My Ref 1",
                            "shippingRef2": "My Ref 2",
                            "shippingRef3": "My Ref 3",
                            "customsValue": null,
                            "deliveryInstructions": "Please deliver with neighbour",
                            "parcelDescription": "",
                            "liabilityValue": null,
                            "liability": false
                            }]
                        }';
$json=(str_replace(" ", "", $json));
$json=(str_replace("\n", "", $json));
$json=(str_replace("    ", "", $json));
//exit();
$length=strlen($json);
echo $length;
$options = array(
    'http' => array(
        'method'  => 'POST',
        'Host'  => 'api.dpd.co.uk',
        'header'=>  "Content-Type: application/json\r\n" .
                    "Accept: application/json\r\n".
                    "GEOClient: account/123456\r\n".
                    "GEOSession: ".$session."\r\n".
                    "Content-Length: ".$length."\r\n",
        'content'=> $json
      )
);
var_dump($options);
$context     = stream_context_create($options);
echo var_dump($context);
$result      = file_get_contents($url, false, $context);


$response    = json_decode($result);

$data=(json_decode($result,true));
var_dump($data);

$shipmentId=$data['data']['shipmentId'];
///shipping/shipment/[shipmentId]/label/
echo $shipmentId;

$method = '/shipping/shipment/'.$shipmentId.'/label/';

$url = $BASE.$method;

$options = array(
    'http' => array(
        'method'  => 'GET',
        'Host'  => 'api.dpd.co.uk',
        'header'=>  "Accept: text/html\r\n".
                    "GEOClient: account/123456\r\n".
                    "GEOSession: ".$session."\r\n"
                    )
);

$context     = stream_context_create($options);

$result      = file_get_contents($url, false, $context);
$response    = json_decode($result);
echo $result;


?>