1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* HttpClient |
4
|
|
|
* |
5
|
|
|
* @license MIT |
6
|
|
|
* @author zhangv |
7
|
|
|
*/ |
8
|
|
|
namespace zhangv\wechat\pay\util; |
9
|
|
|
|
10
|
|
|
class HttpClient{ |
11
|
|
|
|
12
|
|
|
const GET = 'get',POST = 'post', DELETE = 'delete',PUT = 'put'; |
13
|
|
|
private $instance = null; |
14
|
|
|
private $errNo = null; |
15
|
|
|
private $info = null; |
16
|
|
|
private $timeout = 1; |
17
|
|
|
private $retry = 1; |
18
|
66 |
|
private $tried = 0; |
19
|
66 |
|
|
20
|
66 |
|
public function __construct($timeout = 1,$retry = 1) { |
21
|
|
|
$this->timeout = $timeout; |
22
|
66 |
|
$this->retry = $retry; |
23
|
66 |
|
$this->initInstance(); |
24
|
66 |
|
} |
25
|
66 |
|
|
26
|
66 |
|
public function initInstance(){ |
27
|
66 |
|
if(!$this->instance) { |
28
|
66 |
|
$this->instance = curl_init(); |
29
|
|
|
if($this->timeout < 1) { |
30
|
66 |
|
curl_setopt($this->instance, CURLOPT_NOSIGNAL,1); |
|
|
|
|
31
|
|
|
curl_setopt($this->instance, CURLOPT_TIMEOUT_MS, $this->timeout * 1000); |
32
|
2 |
|
}else |
33
|
2 |
|
curl_setopt($this->instance, CURLOPT_TIMEOUT, intval($this->timeout)); |
34
|
2 |
|
curl_setopt($this->instance, CURLOPT_RETURNTRANSFER, true); |
35
|
2 |
|
curl_setopt($this->instance, CURLOPT_FOLLOWLOCATION, true); |
36
|
2 |
|
curl_setopt($this->instance, CURLOPT_SSL_VERIFYPEER, false); |
37
|
2 |
|
} |
38
|
2 |
|
} |
39
|
2 |
|
|
40
|
2 |
|
public function get($url,$params = array(),$headers = array(),$opts = array()) { |
41
|
2 |
|
if (!$this->instance) $this->initInstance($this->timeout); |
|
|
|
|
42
|
2 |
|
if($params && count($params) > 0) $url .= '?' . http_build_query($params); |
43
|
|
|
curl_setopt($this->instance, CURLOPT_URL, $url); |
44
|
|
|
curl_setopt($this->instance, CURLOPT_HTTPGET, true); |
45
|
1 |
|
curl_setopt($this->instance, CURLOPT_HTTPHEADER, $headers); |
46
|
1 |
|
curl_setopt_array($this->instance,$opts); |
47
|
1 |
|
$result = $this->execute(); |
48
|
1 |
|
curl_close($this->instance); |
49
|
1 |
|
$this->instance = null; |
50
|
1 |
|
return $result; |
51
|
1 |
|
} |
52
|
1 |
|
|
53
|
1 |
|
public function post($url, $params = array(),$headers = array(),$opts = array()) { |
54
|
1 |
|
if (!$this->instance) $this->initInstance($this->timeout); |
|
|
|
|
55
|
1 |
|
curl_setopt($this->instance, CURLOPT_URL, $url); |
56
|
|
|
curl_setopt($this->instance, CURLOPT_POST, true); |
57
|
|
|
curl_setopt($this->instance, CURLOPT_POSTFIELDS, $params); |
58
|
3 |
|
curl_setopt($this->instance, CURLOPT_HTTPHEADER, $headers); |
59
|
3 |
|
curl_setopt_array($this->instance,$opts); |
60
|
3 |
|
$result = $this->execute(); |
61
|
3 |
|
curl_close($this->instance); |
62
|
3 |
|
$this->instance = null; |
63
|
|
|
return $result; |
64
|
|
|
} |
65
|
3 |
|
|
66
|
3 |
|
private function execute() { |
67
|
|
|
$this->tried = 0; |
68
|
|
|
do{ |
69
|
|
|
$result = curl_exec($this->instance); |
70
|
|
|
$this->errNo = curl_errno($this->instance); |
71
|
|
|
}while(!$result && $this->errNo === CURLE_OPERATION_TIMEDOUT && $this->tried++ < $this->retry); |
72
|
|
|
$this->info = curl_getinfo($this->instance); |
73
|
|
|
return $result; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function getInfo(){ |
77
|
|
|
return $this->info; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getErrorNo(){ |
81
|
|
|
return $this->errNo; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function getTried(){ |
85
|
|
|
return $this->tried; |
86
|
|
|
} |
87
|
|
|
} |