Request.php
4.54 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
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
<?php
require_once QCLOUDAPI_ROOT_PATH . '/Common/Sign.php';
/**
* QcloudApi_Common_Request
*/
class QcloudApi_Common_Request
{
/**
* $_requestUrl
* 请求url
* @var string
*/
protected static $_requestUrl = '';
/**
* $_rawResponse
* 原始的返回信息
* @var string
*/
protected static $_rawResponse = '';
/**
* $_version
* @var string
*/
protected static $_version = 'SDK_PHP_1.1';
/**
* $_timeOut
* 设置连接主机的超时时间
* @var int 数量级:秒
* */
protected static $_timeOut = 10;
/**
* getRequestUrl
* 获取请求url
*/
public static function getRequestUrl()
{
return self::$_requestUrl;
}
/**
* getRawResponse
* 获取原始的返回信息
*/
public static function getRawResponse()
{
return self::$_rawResponse;
}
/**
* generateUrl
* 生成请求的URL
*
* @param array $paramArray
* @param string $secretId
* @param string $secretKey
* @param string $requestHost
* @param string $requestPath
* @param string $requestMethod
* @return
*/
public static function generateUrl($paramArray, $secretId, $secretKey, $requestMethod, $requestHost, $requestPath) {
if(!isset($paramArray['SecretId']))
$paramArray['SecretId'] = $secretId;
if (!isset($paramArray['Nonce']))
$paramArray['Nonce'] = rand(1, 65535);
if (!isset($paramArray['Timestamp']))
$paramArray['Timestamp'] = time();
$paramArray['RequestClient'] = self::$_version;
$plainText = QcloudApi_Common_Sign::makeSignPlainText($paramArray,
$requestMethod, $requestHost, $requestPath);
$paramArray['Signature'] = QcloudApi_Common_Sign::sign($plainText, $secretKey);
$url = 'https://' . $requestHost . $requestPath;
if ($requestMethod == 'GET') {
$url .= '?' . http_build_query($paramArray);
}
return $url;
}
/**
* send
* 发起请求
* @param array $paramArray 请求参数
* @param string $secretId secretId
* @param string $secretKey secretKey
* @param string $requestMethod 请求方式,GET/POST
* @param string $requestHost 接口域名
* @param string $requestPath url路径
* @return
*/
public static function send($paramArray, $secretId, $secretKey, $requestMethod, $requestHost, $requestPath)
{
if(!isset($paramArray['SecretId']))
$paramArray['SecretId'] = $secretId;
if (!isset($paramArray['Nonce']))
$paramArray['Nonce'] = rand(1, 65535);
if (!isset($paramArray['Timestamp']))
$paramArray['Timestamp'] = time();
$paramArray['RequestClient'] = self::$_version;
$plainText = QcloudApi_Common_Sign::makeSignPlainText($paramArray,
$requestMethod, $requestHost, $requestPath);
$paramArray['Signature'] = QcloudApi_Common_Sign::sign($plainText, $secretKey);
$url = 'https://' . $requestHost . $requestPath;
$ret = self::_sendRequest($url, $paramArray, $requestMethod);
return $ret;
}
/**
* _sendRequest
* @param string $url 请求url
* @param array $paramArray 请求参数
* @param string $method 请求方法
* @return
*/
protected static function _sendRequest($url, $paramArray, $method = 'POST')
{
$ch = curl_init();
if ($method == 'POST')
{
$paramArray = is_array( $paramArray ) ? http_build_query( $paramArray ) : $paramArray;
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $paramArray);
}
else
{
$url .= '?' . http_build_query($paramArray);
}
self::$_requestUrl = $url;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT,self::$_timeOut);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if (false !== strpos($url, "https")) {
// 证书
// curl_setopt($ch,CURLOPT_CAINFO,"ca.crt");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}
$resultStr = curl_exec($ch);
self::$_rawResponse = $resultStr;
$result = json_decode($resultStr, true);
if (!$result)
{
return $resultStr;
}
return $result;
}
}