如何使用 api.dpd.co.uk api 与 php 集成 [英] How to use api.dpd.co.uk api integration with php

查看:22
本文介绍了如何使用 api.dpd.co.uk api 与 php 集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从事 Web 开发已有几年了,在今天之前没有使用任何 api.现在我必须将航运公司的 api 与客户网站集成,我正在尝试了解其 api 文档,但没有任何有用的结果.客户为我提供了一个我难以理解的 pdf 文件.任何人都可以尽快帮助我.我应该非常感谢任何人可以帮助我

Hi I'm working as web developer from several years and did not use any api before today. Now I have to integrate a shipping company's api with clients website and I'm trying to understand its api documentation but did not have any useful result. client provided me a pdf file that's difficult to understand for me. can anybody help me as soon as possible. I should be very grateful I any one can help me

我尝试的最后一个代码:

last code that i try:

$url="api.dpd.co.uk/user/?action=login HTTP/1.1";

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


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

帮助解决这个问题将不胜感激.

assistance to solve this issue would be appreciated.

谢谢.

推荐答案

虽然有人可能会发现这很有用 - 这是一个适用于 api.dpd.co.uk 的不同方法的负载 - 几个注意事项 - 你需要把你的帐号 - 以及您的用户名和密码 - 也用于创建货件,请确保 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;


?>

这篇关于如何使用 api.dpd.co.uk api 与 php 集成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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