forked from alipay/global-open-sdk-php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DefaultAlipayClient.php
79 lines (63 loc) · 2.65 KB
/
DefaultAlipayClient.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
<?php
require_once 'BaseAlipayClient.php';
require_once 'model/HttpRpcResult.php';
class DefaultAlipayClient extends BaseAlipayClient{
function __construct($gatewayUrl, $merchantPrivateKey, $alipayPublicKey) {
parent::__construct($gatewayUrl, $merchantPrivateKey, $alipayPublicKey);
}
protected function buildCustomHeader(){
return null;
}
protected function sendRequest($requestUrl, $httpMethod, $headers, $reqBody){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $requestUrl);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $reqBody);
$rspContent = curl_exec($curl);
if (curl_getinfo($curl, CURLINFO_HTTP_CODE) != '200') {
return null;
}
$headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
$headerContent = substr($rspContent, 0, $headerSize);
$rspBody = substr($rspContent, $headerSize);
$httpRpcResult = new HttpRpcResult();
$httpRpcResult->setRspBody($rspBody);
$headArr = explode("\r\n", $headerContent);
foreach ($headArr as $headerItem) {
if(strstr($headerItem, "response-time") || strstr($headerItem, "signature")){
$responseTime = $this->getResponseTime($headerItem);
if(isset($responseTime) && $responseTime != null){
$httpRpcResult->setRspTime(trim($responseTime));
} else {
$signatureValue = $this->getResponseSignature($headerItem);
if(isset($signatureValue) && $signatureValue != null){
$httpRpcResult->setRspSign($signatureValue);
}
}
}
}
curl_close($curl);
return $httpRpcResult;
}
private function getResponseTime($headerItem){
if(strstr($headerItem, "response-time")){
$startIndex = strpos($headerItem, ":") + 1;
$responseTime = substr($headerItem, $startIndex);
return $responseTime;
}
return null;
}
private function getResponseSignature($headerItem){
if(strstr($headerItem, "signature")){
$startIndex = strrpos($headerItem, "=") + 1;
$signatureValue = substr($headerItem, $startIndex);
return $signatureValue;
}
return null;
}
}