-
Notifications
You must be signed in to change notification settings - Fork 6
/
createLabel.php
93 lines (80 loc) · 2.6 KB
/
createLabel.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
require __DIR__ . '/../vendor/autoload.php';
$config = require __DIR__ . '/exampleConfig.php';
$client = new \Dhl\ApiClient($config);
// Generate a version 4 (random) UUID object
$labelId = \Ramsey\Uuid\Uuid::uuid4();
// createLabel parameters, which is custom per call
$parameters = [
'body' => [
'labelId' => $labelId,
'labelFormat' => 'pdf',
'orderReference' => 'MyOrder: 000000007',
'parcelTypeKey' => 'SMALL',
'receiver' => [
'name' => [
'firstName' => 'CustomerFirst',
'lastName' => 'CustomerLast',
'companyName' => null,
],
'address' => [
'countryCode' => 'NL',
'postalCode' => '3542 AB',
'city' => 'Utrecht',
'street' => 'Atoomweg',
'number' => '30',
'isBusiness' => false,
'addition' => null
],
'email' => 'yourcustomer@example.com',
'phoneNumber' => null,
],
'shipper' => [
'name' => [
'firstName' => 'Plugins',
'lastName' => 'Dhl',
'companyName' => 'Webshop DHL',
],
'address' => [
'countryCode' => 'NL',
'postalCode' => '3542 AD',
'city' => 'Utrecht',
'street' => 'Reactorweg',
'number' => '25',
'isBusiness' => true,
'addition' => null
],
'email' => 'plugins@dhl.com',
'phoneNumber' => '0612345678'
],
'accountId' => '08500001',
'options' => [
[
'key' => 'DOOR',
],
[
'key' => 'REFERENCE',
'input' => '000000007',
],
[
'key' => 'REFERENCE2',
'input' => 'OrderID',
],
],
'pieceNumber' => 1,
'quantity' => 1,
'automaticPrintDialog' => false,
'application' => 'ApiWrapperv0.1.0',
],
];
/** @var \GuzzleHttp\Command\Result $labelResult */
$labelResult = $client->createLabel($parameters);
printf('%s%s', print_r($labelResult, true), PHP_EOL);
$trackerCode = $labelResult->offsetGet('trackerCode');
$filename = sprintf('/tmp/DHL-%s.pdf', $trackerCode);
$rawPdf = $labelResult->offsetGet('pdf');
if (!file_put_contents($filename, base64_decode($rawPdf))) {
printf('could not write label: %s%s', $filename, PHP_EOL);
} else {
printf('Shipping label writen: %s%s', $filename, PHP_EOL);
}