1
|
|
|
<?php |
2
|
|
|
namespace OpenOauth\Core\Http; |
3
|
|
|
|
4
|
|
|
class Http |
5
|
|
|
{ |
6
|
|
|
public static $error; |
7
|
|
|
|
8
|
|
|
public static function _post($apiUrl, $data) |
9
|
|
|
{ |
10
|
|
|
$apiUrl = urldecode($apiUrl); |
11
|
|
|
$ch = curl_init(); |
12
|
|
|
|
13
|
|
|
if (is_array($data)) { |
14
|
|
|
if (!defined('JSON_UNESCAPED_UNICODE')) { |
15
|
|
|
// 解决php 5.3版本 json转码时 中文编码问题. |
16
|
|
|
$data = json_encode($data); |
17
|
|
|
$data = preg_replace("#\\\u([0-9a-f]{4})#ie", "iconv('UCS-2BE', 'UTF-8', pack('H4', '\\1'))", $data); |
18
|
|
|
} else { |
19
|
|
|
$data = json_encode($data, JSON_UNESCAPED_UNICODE); |
20
|
|
|
} |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
curl_setopt($ch, CURLOPT_URL, $apiUrl); |
24
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 60); |
25
|
|
|
curl_setopt($ch, CURLOPT_HEADER, true); |
26
|
|
|
curl_setopt($ch, CURLOPT_NOBODY, false); |
27
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
28
|
|
|
curl_setopt($ch, CURLOPT_POST, 1); |
29
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); |
30
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); |
31
|
|
|
|
32
|
|
|
$res = trim(curl_exec($ch)); |
33
|
|
|
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
34
|
|
|
curl_close($ch); |
35
|
|
|
|
36
|
|
|
$header = ''; |
37
|
|
|
$body = $res; |
38
|
|
View Code Duplication |
if ($http_code == 200) { |
|
|
|
|
39
|
|
|
list($header, $body) = explode("\r\n\r\n", $res, 2); |
40
|
|
|
$header = http_parse_headers($header); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$result = []; |
44
|
|
|
$result['info'] = $body; |
45
|
|
|
$result['header'] = $header; |
46
|
|
|
$result['status'] = $http_code; |
47
|
|
|
|
48
|
|
|
return self::packData($result); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public static function _get($apiUrl) |
52
|
|
|
{ |
53
|
|
|
$apiUrl = urldecode($apiUrl); |
54
|
|
|
$ch = curl_init($apiUrl); |
55
|
|
|
curl_setopt($ch, CURLOPT_URL, $apiUrl); |
56
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 60); |
57
|
|
|
curl_setopt($ch, CURLOPT_HEADER, true); |
58
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
59
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); |
60
|
|
|
|
61
|
|
|
$res = curl_exec($ch); |
62
|
|
|
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
63
|
|
|
curl_close($ch); |
64
|
|
|
|
65
|
|
|
$header = ''; |
66
|
|
|
$body = $res; |
67
|
|
View Code Duplication |
if ($http_code == 200) { |
|
|
|
|
68
|
|
|
list($header, $body) = explode("\r\n\r\n", $res, 2); |
69
|
|
|
$header = http_parse_headers($header); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$result = []; |
73
|
|
|
$result['info'] = $body; |
74
|
|
|
$result['header'] = $header; |
75
|
|
|
$result['status'] = $http_code; |
76
|
|
|
|
77
|
|
|
return self::packData($result); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* 对接口返回的数据进行验证和组装. |
82
|
|
|
* |
83
|
|
|
* @author Tian |
84
|
|
|
* |
85
|
|
|
* @date 2015-12-08 |
86
|
|
|
* |
87
|
|
|
* @param array $apiReturnData 由_post|| _get方法返回的数据. |
88
|
|
|
* |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
private static function packData($apiReturnData) |
92
|
|
|
{ |
93
|
|
|
$status = $apiReturnData['status']; |
94
|
|
|
$returnData = $apiReturnData['info']; |
95
|
|
|
|
96
|
|
|
if ($status != 200 && empty($returnData)) { |
97
|
|
|
self::$error = '接口服务器连接失败.'; |
98
|
|
|
|
99
|
|
|
return false; |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
$apiReturnData = json_decode($returnData, true); |
103
|
|
|
|
104
|
|
|
if ($status != 200 && !$apiReturnData) { |
105
|
|
|
self::$error = $returnData; |
106
|
|
|
|
107
|
|
|
return false; |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if (isset($apiReturnData['errcode']) && $apiReturnData['errcode'] != 0) { |
111
|
|
|
$error = '错误码:' . $apiReturnData['errcode'] . ', 错误信息:' . $apiReturnData['errmsg']; |
112
|
|
|
|
113
|
|
|
self::$error = $error; |
114
|
|
|
|
115
|
|
|
return false; |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $apiReturnData; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
//post 发送xml |
122
|
|
|
public static function _post_xml($url, $xml = null) |
123
|
|
|
{ |
124
|
|
|
$ch = curl_init($url); |
125
|
|
|
curl_setopt($ch, CURLOPT_URL, $url); |
126
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
127
|
|
|
curl_setopt($ch, CURLOPT_POST, 1); |
128
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); |
129
|
|
|
$response = curl_exec($ch); |
130
|
|
|
if (curl_errno($ch)) { |
131
|
|
|
print curl_error($ch); |
132
|
|
|
} |
133
|
|
|
curl_close($ch); |
134
|
|
|
|
135
|
|
|
return $response; |
136
|
|
|
} |
137
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.