如何使用api.dpd.co.uk api与php集成

时间:2014-05-13 16:29:20

标签: php api

您好我多年从事网络开发人员工作,并且在今天之前没有使用任何API。现在我必须将一家航运公司的api与客户网站集成,并且我试图了解它的api文档,但没有任何有用的结果。客户向我提供了pdf文件,对我来说很难理解。任何人都可以尽快帮助我。我应该非常感激任何人都可以帮助我

我尝试的最后一个代码:

$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);

可以帮助解决这个问题。

谢谢。

6 个答案:

答案 0 :(得分:6)

虽然有人可能会觉得这很有用 - 这是为api.dpd.co.uk提供的各种方法 - 几个笔记 - 你需要输入你的帐号 - 以及你的用户名和密码 - 也用于创建货件确保collectionDate在将来或它只是抛出一般应用程序错误..

<?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;


?>

答案 1 :(得分:2)

如果有人有兴趣写了一个认证课程?

class Authentication {
    private $url;
    private $timeout;
    private $ch;
    private $headers;
    private $username;
    private $password;
    private $accountNo;
    public function __construct($url, $username, $password, $accountNo, $timeout='5', $headers=array()) {
        $this->headers = array( 'Content-Type: application/json',
            'Accept: application/json',
            'Authorization: Basic ' . base64_encode($username . ':' . $password),
            'GEOClient: ' . $username . '/' . $accountNo,
            'Content-Length: 0'
        );
        $this->url = $url . '/user/?action=login';
        $this->timeout = $timeout;
        $this->headers = array_merge($this->headers, $headers);
        $this->ch = curl_init();
    }
    public function __destruct() {
        curl_close($this->ch);
    }
    public function doAuthentication() {
        curl_setopt_array($this->ch, array(
            CURLOPT_URL => $this->url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_CONNECTTIMEOUT => $this->timeout,
            CURLOPT_USERAGENT => 'Spry Web Design dev',
            CURLOPT_HTTPHEADER => $this->headers,
            CURLOPT_POST => true
        ));
        $authPost = curl_exec($this->ch);
        $data = json_decode($authPost, true);
        return $data['data']['geoSession'];
    }
};

// example usage below
$client = new Authentication("URL", 'USERNAME', 'PASSWORD', 'ACCOUNT_NO');
echo $result = $client->doAuthentication();

答案 2 :(得分:0)

您必须在dpd.co.uk上拥有帐户。虚拟值不会像(帐号/ 123456)或虚拟密码(DSMITH:MYPASSWD)那样工作,你必须在那里给你api帐号和用户名和密码http://dpd.co.uk将为你提供api而不是这个工作。在http://www.dpd.co.uk/apps/api/

上使用以下链接获取api帐户

答案 3 :(得分:0)

这对我来说是使用Zend库授权登录的最佳方式。

        $BASE="https://api.dpd.co.uk";
        $method = '/user/?action=login';
        $url = $BASE.$method;

        $client = new Zend_Http_Client($url);
        $client->setMethod(Zend_Http_Client::POST);
        $client->setHeaders('Host', 'api.dpd.co.uk');
        $client->setHeaders(Zend_Http_Client::CONTENT_TYPE, 'application/json');
        $client->setHeaders('Accept', 'application/json');
        $client->setHeaders('Authorization', 'Basic '. base64_encode('username:password'));
        $client->setHeaders('GEOClient', 'account name/account number');
        $client->setHeaders('Content-Length', '0');
        $response = $client->request();
        $body = Zend_Json::decode($response->getBody());
        $geoSession =   $body['data']['geoSession'];

答案 4 :(得分:0)

最近我在我的网站上连接了 DPD API。为了测试,如果需要,我创建了 repo,请查看。因为我已经在这个存储库上实现了所有 API 端点。

链接:https://github.com/najathi/dpd-api-shipping-uk

谢谢。

答案 5 :(得分:-1)

试试这个希望这会有所帮助。您必须拥有(dpd.co.uk)帐户的用户名,密码和帐号。

$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("username:password") ."\r\n".
                    "GEOClient: account/your account number\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);