/
cartOrderingAPI.php
120 lines (92 loc) · 3.24 KB
/
cartOrderingAPI.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
<?php
class IMatOrderApiClient
{
private $_apiCode;
private $_apiUrl;
public function __construct($apiUrl, $apiCode)
{
$this->_apiUrl = $apiUrl;
$this->_apiCode = $apiCode;
}
public function Post($request, $deliveryNoteFilePath, $invoiceFilePath, $requestContentType)
{
$bodyPartsDelimiter = $this->CreateDalimiter();
$body = $this->CreatePostBody($request, $deliveryNoteFilePath, $invoiceFilePath, $requestContentType, $bodyPartsDelimiter);
$headers = $this->CreateHeaders($body, $requestContentType, $bodyPartsDelimiter);
$result = $this->DoPost($this->_apiUrl, $headers, $body);
return $result;
}
private function CreateDalimiter()
{
$delimiter = "----------------".uniqid();
return $delimiter;
}
private function CreateHeaders($body, $acceptContentType, $delimiter)
{
$headers = array (
"Content-Type: multipart/form-data; boundary=".$delimiter,
"Accept: ".$acceptContentType,
"ApiCode: ".$this->_apiCode,
"Content-Length: ".strlen($body)
);
return $headers;
}
private function DoPost($apiUrl, $headers, $body)
{
$curl = curl_init($apiUrl);
curl_setopt($curl,CURLOPT_POSTFIELDS , $body);
curl_setopt($curl,CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl,CURLOPT_POST, true);
curl_setopt($curl,CURLOPT_RETURNTRANSFER , true);
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
private function CreatePostBody($request, $deliveryNoteFilePath, $invoiceFilePath, $requestContentType, $delimiter)
{
$requestPart = $this->CreateRequestPart($request, $requestContentType);
$deliveryNoteFilePart = $this->CreateFilePart("MyDeliveryNoteFile", $deliveryNoteFilePath);
$invoiceFilePart = $this->CreateFilePart("MyInvoiceFile", $invoiceFilePath);
$body = $this->CombineParts(array($requestPart, $deliveryNoteFilePart, $invoiceFilePart), $delimiter);
return $body;
}
private function CreateRequestPart($request, $contentType)
{
$data= "Content-Disposition: form-data; name=\"request\"\r\n";
$data.= "Content-Type: $contentType\r\n\r\n";
$data.= $request;
return $data;
}
private function CombineParts($parts, $delimiter)
{
$result = "--$delimiter\r\n".implode("\r\n--$delimiter\r\n", $parts)."\r\n--$delimiter--\r\n";
return $result;
}
private function CreateFilePart($name, $filePath)
{
$fileData = file_get_contents($filePath);
$fileInfo = pathinfo($filePath);
$fileName = $fileInfo["basename"];
$data= "Content-Disposition: form-data; name=\"".$name."\"; filename=\"".$fileName."\"\r\n";
$data.= "Content-Type: application/octet-stream\r\n\r\n";
$data.= $fileData;
return $data;
}
}
$requestData = '
{
"cartID":"[cart id here]",
"myOrderReference":"some reference",
"directMailingAllowed":"false",
"shipmentService":"",
"myInvoiceLink":"",
"myDeliveryNoteLink":"",
"remarks":""
}';
$deliveryNoteFilePath = "[File Path here]";//file path or empty string
$invoiceFilePath = "[File Path here]";//file path or empty string
$client = new IMatOrderApiClient("https://imatsandbox.materialise.net/web-api/order/post","[API Code here]");
$result = $client->Post($requestData, $deliveryNoteFilePath, $invoiceFilePath, "application/json");
echo(htmlspecialchars($result));
?>