api读取dhl的pdf订单

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
186
187
188
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
ini_set('soap.wsdl_cache_enabled', "0");
define('PDF_DIR', $_SERVER['DOCUMENT_ROOT'] . '/tmp/');
exec('rm /tmp/* -rf');
//关闭wsdl缓存
class BaseRetoure
{
private $username = "webchunyanliu";//"ws_online_retoure";//webchunyanliu
private $password = "Chunyan!14";//"Anfang1!";//'Chunyan!14'
private $portalId = "chunyanliu";
private $deliveryName = "RetourenWeb02";
private $end_point = "https://amsel.dpwn.net/abholportal/gw/lp/SoapConnector";
private $wsdl = "https://amsel.dpwn.net/abholportal/gw/lp/schema/1.0/var3ws.wsdl";
public $_attributes = array();
public $_orderCode = '';
public function __construct($orderCode=''){
$this->_orderCode = $orderCode;
}
public function getRetourePdf($params)
{
$xmlRequest = $this->getRequestXml($params);
$response = $this->curlSoapRequest($xmlRequest);
if($response){
$pdf = $this->getPdfFromResponse($response);
}
return $pdf;
}
public function displayPdf($pdf){
if (!is_dir(PDF_DIR)) {
exec("mkdir " . PDF_DIR);
exec("chmod 777 " . PDF_DIR);
}
$file = PDF_DIR . $this->_orderCode . '.pdf';
exec("chmod 777 " . $file);
$fp = fopen($file, 'wb');
fwrite($fp,$pdf);
fclose($fp);
header("Expires: 0");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Content-type: application/pdf");
header('Content-length: '.filesize($file));
header('Content-disposition: attachment; filename='.basename($file));
readfile($file);
}

private function getRequestXml($params)
{
$request =
"<?xml version='1.0' encoding='UTF-8' ?>
<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:var='https://amsel.dpwn.net/abholportal/gw/lp/schema/1.0/var3bl'>
<soapenv:Header>
<wsse:Security soapenv:mustUnderstand='1' xmlns:wsse='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'>
<wsse:UsernameToken>
<wsse:Username>".$this->username."</wsse:Username>
<wsse:Password Type='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText'>
".$this->password."
</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
<soapenv:Body>
<var:BookLabelRequest
portalId='".$this->portalId."'
deliveryName='".$this->deliveryName."'
shipmentReference='".$params['shipmentReference']."'
customerReference='".$params['customerReference']."'
labelFormat='PDF'
senderName1='".$params['senderName1']."'
senderName2='".$params['senderName2']."'
senderCareOfName='".$params['senderCareOfName']."'
senderContactPhone='".$params['senderContactPhone']."'
senderStreet='".$params['senderStreet']."'
senderStreetNumber='".$params['senderStreetNumber']."'
senderBoxNumber='".$params['senderBoxNumber']."'
senderPostalCode='".$params['senderPostalCode']."'
senderCity='".$params['senderCity']."' />
</soapenv:Body>
</soapenv:Envelope>";
return $request;
}

/**
* @param $soap_request
* @return mixed
*/
private function curlSoapRequest($xmlRequest)
{
$header = array(
"Content-type: text/xml;charset=\"utf-8\"",
"Accept: text/xml",
"Cache-Control: no-cache",
"Pragma: no-cache",
"Content-length: " . strlen($xmlRequest),
);

$soap_do = curl_init();
curl_setopt($soap_do, CURLOPT_URL, $this->end_point);
curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($soap_do, CURLOPT_TIMEOUT, 10);
curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true);
curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($soap_do, CURLOPT_POST, true);
curl_setopt($soap_do, CURLOPT_POSTFIELDS, $xmlRequest);
curl_setopt($soap_do, CURLOPT_HTTPHEADER, $header);
$response = curl_exec($soap_do);
// print_r($response);die;
if (!$response) {
$err = 'Curl error: ' . curl_error($soap_do);
}
else {
/* var_dump(htmlentities($response));*/
}

curl_close($soap_do);
return $response;
}

/**
* @param $response
* @return string
*/
private function getPdfFromResponse($response)
{

$xml = simplexml_load_string($response);

$ns = $xml->getNamespaces(true);

$soap = $xml->children($ns['env']);
$attrbites = array();
foreach($soap->Body->children($ns['var3bl'])->attributes() as $name => $value) {
$attrbites[$name] = $value[0]->__toString();
}
$this->_attributes = $attrbites;

$pdf = $soap->Body->children($ns['var3bl'])->BookLabelResponse->label;
$pdf = base64_decode($pdf);
return $pdf;
}
}

if(isset($_GET['flag']) && $_GET['flag'] == 'download'){
//订单编号(唯一值)
$orderCode = '0123456789';
$filename = PDF_DIR . $orderCode . '.pdf';
if(file_exists($filename)){
//该订单的pdf文件存在的话就直接读缓存
header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-disposition: attachment; filename='.basename($filename));
header("Content-Type: application/pdf"); //pdf格式的
header("Content-Transfer-Encoding: binary");
header('Content-Length: '. filesize($filename));
readfile($filename);
exit();
}

$params['shipmentReference'] = 'PP324567,Huang Li';
$params['customerReference'] = 'GL345';
$params['senderName1'] = 'Zhou Meichen';
$params['senderName2'] = 'via Webservice';
$params['senderCareOfName'] = 'careOfName';
$params['senderContactPhone'] = '0800 123456';
$params['senderStreet'] = 'Webservice Street';
$params['senderStreetNumber'] = '8080';
$params['senderBoxNumber'] = '12';
$params['senderPostalCode'] = '80807';
$params['senderCity'] = 'Muenchen';
$baseRetoure = new BaseRetoure($orderCode);
$pdf = $baseRetoure->getRetourePdf($params);
// print_r($baseRetoure->_attributes);die;
if($pdf){
$baseRetoure->displayPdf($pdf);
}
}

?>
<html>
<body>
[Download Link](?flag=download)
</body>
</html>

 

坚持原创技术分享,您的支持将鼓励我继续创作!