之前每次用到第三方api接口,都是用一次写一次,闲来闲暇,结合之前http调用第三方api接口的实例,封装一下比较基础的http请求类

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
<?php


class CurlHttp
{
private array $options = array(
/**
* 请求类型
*/
'method' => 'GET',
/**
* 请求头
*/
'headers' => array(),
/**
* 请求参数、这里会放在body里进行发送,一般用于POST、PATCH等请求
*/
'data' => array(),
/**
* 请求参数、这里会放在url后面拼接,GET请求需要放在此处
*/
'params' => array(),
/**
* 响应数据类型。text|json
*/
'responseType' => 'text',
/**
* 请求超时时间(单位为毫秒)
*/
'timeout' => 3000,
/**
* 请求成功回调函数
*/
'success' => null,
/**
* 请求失败回调函数
*/
'error' => null,
/**
* 请求完成(无论成功与失败)回调函数
*/
'finally' => null
);

/**
* GET请求
* @param $url
* @param array $params
* @param \Closure|null $closure
* @date : 2021/7/16 19:19
* @author : 孤鸿渺影
* @return bool|string
* @throws \Exception
*/
public function get($url,array $params = [],\Closure $closure = null)
{
$options = [
'url' => $url,
'method' => 'GET',
'success' => $closure,
'params' => $params
];
return $this->http($options);
}

/**
* POST请求
* @param $url
* @param array $data
* @param \Closure|null $closure
* @date : 2021/7/16 19:26
* @author : 孤鸿渺影
* @return bool|string
* @throws \Exception
*/
public function post($url,array $data = [],\Closure $closure = null)
{
$options = [
'url' => $url,
'method' => 'POST',
'success' => $closure,
'data' => $data
];
return $this->http($options);
}

public function http(array $options = [])
{
if(empty($options['url'])) throw new \Exception('url不能为空');
$url = $options['url'];
$method = $options['method'] ?? $this->options['method'];
$headers = (isset($options['headers']) && is_array($options['headers'])) ? $options['headers'] : $this->options['headers'];
$data = (isset($options['data']) && is_array($options['data'])) ? $options['data'] : $this->options['data'];
$params = (isset($options['params']) && is_array($options['params'])) ? $options['params'] : $this->options['params'];
$success = (isset($options['success']) && $this->is_function($options['success'])) ? $options['success'] : $this->options['success'];
$error = (isset($options['error']) && $this->is_function($options['error'])) ? $options['error'] : $this->options['error'];
$finally = (isset($options['finally']) && $this->is_function($options['finally'])) ? $options['finally'] : $this->options['finally'];
$responseType = (isset($options['responseType']) && in_array($options['responseType'],['text','json'])) ? $options['responseType'] : $this->options['responseType'];

$curl = curl_init();
//请求类型
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);

curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_FAILONERROR, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT_MS, (isset($options['timeout']) && is_numeric($options['timeout'])) ? $options['timeout'] : $this->options['timeout']);
if($method === 'POST'){
curl_setopt($curl,CURLOPT_POST,true);
curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
}
// 解析url参数
$paramStr = '';
foreach ($params as $key => $param){
$paramStr !== '' && ($paramStr .="&");
$paramStr .= $key . "=" . urlencode($param);
}
if($paramStr){
$url .= '?' . $paramStr;
}
// 请求地址
curl_setopt($curl, CURLOPT_URL, $url);
if (1 == strpos("$" . $url, "https://")) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
$out_put = curl_exec($curl);
if (curl_error($curl)) {
if($error){
$error($curl);
}
} else {
if($responseType == 'json'){
$out_put = json_decode($out_put,true);
}
if($success){
$success($out_put);
}
}
if($finally){
$finally($out_put);
}

curl_close($curl);
return $out_put;
}


/**
* 判断是否是方法
* @param $f
* @date : 2021/7/16 19:50
* @author : 孤鸿渺影
* @return bool
*/
private function is_function($f) {
return (is_string($f) && function_exists($f)) || (is_object($f) && ($f instanceof \Closure));
}
}

使用示范

GET请求

1
2
3
4
5
6
7
$curlHttp = new CurlHttp();
$curlHttp->get('https://xx.xx.com/api/article/list',[
'page' => 1,
'limit' => 5
],function ($response){
var_dump($response);
});

POST请求

1
2
3
4
5
6
7
$curlHttp = new CurlHttp();
$curlHttp->post('https://xx.xx.com/api/article/list',[
'page' => 1,
'limit' => 5
],function ($response){
var_dump($response);
});

其它比较复杂的http请求

这个http的选项option可以看上边的源码注释

1
2
3
4
5
6
7
8
9
10
11
12
$curlHttp = new CurlHttp();
$curlHttp->http([
'url' => 'https://xx.xx.com/api/article/list',
'params' => [
'page' => 1,
'limit' => 5
],
'responseType' => 'json',
'success' => function ($response){
var_dump($response);
}
]);