-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path04_fetch_document.php
More file actions
76 lines (65 loc) · 2.77 KB
/
Copy path04_fetch_document.php
File metadata and controls
76 lines (65 loc) · 2.77 KB
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
<?php
/**
* Example 04 - Download shipment documents and per-package (colli) numbers.
* ---------------------------------------------------------------------------
* `fetchDocument()` calls SUUS `getDocument` and returns the raw PDF bytes for
* one of four document types:
*
* DocumentType::Label -> standard A4 shipping label
* DocumentType::LabelA6 -> A6 thermal-printer label (Zebra etc.)
* DocumentType::ShippingOrder -> shipping order (list przewozowy)
* DocumentType::LoadingList -> consolidated loading list
*
* `fetchLabel()` is a shortcut for fetchDocument(..., DocumentType::Label).
* `getColliNumbers()` returns the per-package tracking numbers you need to
* request individual colli labels.
*
* NOTE: in the sandbox, getDocument / getColliNo always fail (PRJ000001) - run
* this against PRODUCTION with a real shipment number.
*
* Run:
* SUUS_LOGIN=ws_xxx SUUS_PASSWORD=xxx php examples/04_fetch_document.php OPLKRI2600895
*/
declare(strict_types=1);
require __DIR__ . '/../vendor/autoload.php';
use VeryCodeCom\Suus\SuusClient;
use VeryCodeCom\Suus\Enum\DocumentType;
use VeryCodeCom\Suus\Exception\SuusException;
$client = SuusClient::production(
login: getenv('SUUS_LOGIN') ?: 'ws_yourlogin',
password: getenv('SUUS_PASSWORD') ?: 'your_password',
);
$shipmentNo = $argv[1] ?? 'OPLKRI2600895';
$outDir = sys_get_temp_dir();
/**
* Small helper: fetch a document type and write it to disk.
*/
$download = static function (SuusClient $client, string $shipmentNo, DocumentType $type, string $outDir): void {
$pdf = $client->fetchDocument($shipmentNo, $type);
$path = sprintf('%s/%s_%s.pdf', $outDir, $type->value, $shipmentNo);
file_put_contents($path, $pdf);
printf(" %-14s -> %s (%d bytes)\n", $type->value, $path, strlen($pdf));
};
try {
echo "Downloading documents for {$shipmentNo}:\n";
// The standard A4 label via the convenience shortcut ...
$label = $client->fetchLabel($shipmentNo);
$labelPath = "{$outDir}/label_{$shipmentNo}.pdf";
file_put_contents($labelPath, $label);
printf(" %-14s -> %s (%d bytes)\n", 'label', $labelPath, strlen($label));
// ... and the other document types explicitly.
$download($client, $shipmentNo, DocumentType::LabelA6, $outDir);
$download($client, $shipmentNo, DocumentType::ShippingOrder, $outDir);
$download($client, $shipmentNo, DocumentType::LoadingList, $outDir);
// Per-package tracking numbers (colli).
echo "\nColli (per-package) numbers:\n";
$colli = $client->getColliNumbers($shipmentNo);
if ($colli === []) {
echo " (none returned)\n";
}
foreach ($colli as $i => $number) {
printf(" #%d %s\n", $i + 1, $number);
}
} catch (SuusException $e) {
echo "SUUS error: {$e->getMessage()}\n";
}