-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.php
185 lines (147 loc) · 5.46 KB
/
example.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
require_once __DIR__.'/autoload.php';
require_once __DIR__.'/tests/fixtures.php';
use Cpapdotcom\Asendia\WebApiClient\Adapter\Soap\AsendiaWsdlClientImpl;
use Cpapdotcom\Asendia\WebApiClient\Adapter\Soap\SoapAsendiaWebApiClient;
use Cpapdotcom\Asendia\WebApiClient\AsendiaWebApiClient;
use Cpapdotcom\Asendia\WebApiClient\Error;
if (3 > count($argv)) {
printf("Example application for exercising the Asendia Web API Client.\n");
printf("\n");
printf("Will connect to the testing WSDL:\n");
printf(" * %s\n", SoapAsendiaWebApiClient::TESTING_WSDL);
printf("\n");
printf("Usage: %s <login> <password> [<labelType>]\n", $argv[0]);
printf("\n");
die;
}
if (3 === count($argv)) {
list ($noop, $login, $password) = $argv;
$labelType = AsendiaWebApiClient::LABEL_TYPE_PDF;
} else {
list ($noop, $login, $password, $labelType) = $argv;
switch (strtolower($labelType)) {
case 'none':
case '0':
$labelType = AsendiaWebApiClient::LABEL_TYPE_NONE;
break;
case 'pdf':
case '1':
$labelType = AsendiaWebApiClient::LABEL_TYPE_PDF;
break;
case 'png':
case '2':
$labelType = AsendiaWebApiClient::LABEL_TYPE_PNG;
break;
case 'jpeg':
case 'jpg':
case '3':
$labelType = AsendiaWebApiClient::LABEL_TYPE_JPEG;
break;
default:
printf("Example application for exercising the Asendia Web API Client.\n");
printf("\n");
printf("Unknown label type specified: %s\n", $labelType);
printf("Must be one of: none, pdf, png, jpeg (default: pdf)\n");
printf("\n");
printf("Usage: %s <login> <password> [<labelType>]\n", $argv[0]);
printf("\n");
die;
}
}
// This is the complex way to get a testing Asendia client.
// This method allows us to capture the original SoapClient
// instance for debugging. (we will check the last request
// made by SoapClient any time we see an exception!)
$soapClient = new SoapClient(SoapAsendiaWebApiClient::TESTING_WSDL, ['trace' => 1]);
$wsdlClient = new AsendiaWsdlClientImpl($soapClient);
$asendia = new SoapAsendiaWebApiClient($wsdlClient, $login, $password);
try {
printf("Creating a shipment.\n");
$createdShipment = $asendia->createShipment();
printf(" * shipment number: %s\n", $createdShipment->getShipment());
printf(" * status: %s\n", $createdShipment->getStatus());
printf("\n");
} catch (Error $e) {
printf(" * ERROR: %s\n\n", $e->getMessage());
if ($soapClient) {
print_r($soapClient->__getLastRequestHeaders());
print_r($soapClient->__getLastRequest());
}
throw $e;
}
try {
printf("Adding packages to a shipment.\n");
$manifest = get_manifest_from_facade($randomize = true);
$addedShipmentPackages = $asendia->addPackagesToShipment(
$createdShipment->getShipment(),
$manifest,
$labelType
);
printf(" * shipment number: %s\n", $addedShipmentPackages->getShipment());
foreach ($addedShipmentPackages->getPackages() as $package) {
printf(" * Added package\n");
printf(" * PckId: %s\n", $package->getPckId());
printf(" * LabelFile: %s\n", $package->getLabelFile());
printf("\n");
}
} catch (Error $e) {
printf(" * ERROR: %s\n\n", $e->getMessage());
if ($soapClient) {
print_r($soapClient->__getLastRequestHeaders());
print_r($soapClient->__getLastRequest());
}
throw $e;
}
if (AsendiaWebApiClient::LABEL_TYPE_NONE !== $labelType) {
printf("Retrieving labels.\n");
foreach ($addedShipmentPackages->getPackages() as $package) {
try {
printf(" * Retrieving PDF label for %s (%s)\n", $package->getPckId(), $package->getLabelFile());
$label = $asendia->retrieveLabel($labelType, $package->getLabelFile());
printf(" * Retrieved label file: %s\n", $label->getLabelFile());
printf(" * Retrieved label size: %d bytes\n", strlen($label->getContent()));
$temporary = get_temp_file_for_label($label);
$label->writeContentToFile($temporary);
printf(" * Retrieved label file: %s\n", $temporary);
printf("\n");
} catch (Error $e) {
printf(" * ERROR: %s\n\n", $e->getMessage());
if ($soapClient) {
print_r($soapClient->__getLastRequestHeaders());
print_r($soapClient->__getLastRequest());
}
throw $e;
}
}
}
try {
printf("Closing a shipment.\n");
$closedShipment = $asendia->closeShipment($createdShipment->getShipment());
printf(" * shipment number: %s\n", $closedShipment->getShipment());
printf(" * status: %s\n", $closedShipment->getStatus());
printf("\n");
} catch (Error $e) {
printf(" * ERROR: %s\n\n", $e->getMessage());
if ($soapClient) {
print_r($soapClient->__getLastRequestHeaders());
print_r($soapClient->__getLastRequest());
}
throw $e;
}
function get_temp_dir_for_labels()
{
return __DIR__.'/temp';
}
function ensure_temp_dir_for_labels_exists()
{
$temp_dir = get_temp_dir_for_labels();
if (! file_exists($temp_dir)) {
mkdir($temp_dir);
}
}
function get_temp_file_for_label(\Cpapdotcom\Asendia\WebApiClient\Label $label)
{
ensure_temp_dir_for_labels_exists();
return get_temp_dir_for_labels().'/'.$label->getLabelFile();
}