1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Freyo\LaravelQueueCMQ\Queue\Driver; |
4
|
|
|
|
5
|
|
|
class CMQHttp |
6
|
|
|
{ |
7
|
|
|
private $connection_timeout; |
8
|
|
|
private $keep_alive; |
9
|
|
|
private $host; |
10
|
|
|
|
11
|
|
|
public function __construct($host, $connection_timeout = 10, $keep_alive = true) |
12
|
|
|
{ |
13
|
|
|
$this->connection_timeout = $connection_timeout; |
14
|
|
|
$this->keep_alive = $keep_alive; |
15
|
|
|
$this->host = $host.'/v2/index.php'; |
16
|
|
|
$this->curl = null; |
|
|
|
|
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function set_method($method = 'POST') |
20
|
|
|
{ |
21
|
|
|
$this->method = $method; |
|
|
|
|
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function set_connection_timeout($connection_timeout) |
25
|
|
|
{ |
26
|
|
|
$this->connection_timeout = $connection_timeout; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function is_keep_alive() |
30
|
|
|
{ |
31
|
|
|
return $this->keep_alive; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function set_keep_alive($keep_alive) |
35
|
|
|
{ |
36
|
|
|
$this->keep_alive = $keep_alive; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function send_request($req_inter, $userTimeout) |
40
|
|
|
{ |
41
|
|
|
if (!$this->keep_alive) { |
42
|
|
|
$this->curl = curl_init(); |
|
|
|
|
43
|
|
|
} else { |
44
|
|
|
if ($this->curl == null) { |
45
|
|
|
$this->curl = curl_init(); |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if ($this->curl == null) { |
50
|
|
|
throw new CMQClientException('Curl init failed'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$url = $this->host; |
54
|
|
|
if ($req_inter->method == 'POST') { |
55
|
|
|
curl_setopt($this->curl, CURLOPT_POST, 1); |
56
|
|
|
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $req_inter->data); |
57
|
|
|
} else { |
58
|
|
|
$url .= $req_inter->uri.'?'.$req_inter->data; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (isset($req_inter->header)) { |
62
|
|
|
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $req_inter->header); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
curl_setopt($this->curl, CURLOPT_URL, $url); |
66
|
|
|
curl_setopt($this->curl, CURLOPT_TIMEOUT, $this->connection_timeout + $userTimeout); |
67
|
|
|
|
68
|
|
|
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true); |
69
|
|
|
|
70
|
|
|
if (false !== strpos($url, 'https')) { |
71
|
|
|
// 证书 |
72
|
|
|
// curl_setopt($ch,CURLOPT_CAINFO,"ca.crt"); |
73
|
|
|
curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false); |
74
|
|
|
curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, false); |
75
|
|
|
} |
76
|
|
|
$resultStr = curl_exec($this->curl); |
77
|
|
|
if (curl_errno($this->curl)) { |
78
|
|
|
throw new CMQClientNetworkException(curl_error($this->curl)); |
79
|
|
|
} |
80
|
|
|
$info = curl_getinfo($this->curl); |
81
|
|
|
$resp_inter = new ResponseInternal($info['http_code'], null, $resultStr); |
82
|
|
|
|
83
|
|
|
return $resp_inter; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|