1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Reuse\Controllers\Api; |
4
|
|
|
|
5
|
|
|
use App\Http\Request; |
6
|
|
|
use App\Interfaces\Controllers\IApi; |
7
|
|
|
|
8
|
|
|
trait TRelay |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
protected $apiRelayResponse; |
12
|
|
|
protected $apiRelayHttpCode; |
13
|
|
|
protected $apiRelayHeaders; |
14
|
|
|
protected $apiRelayOptionHeader = false; |
15
|
|
|
protected $apiRelayOptionVerbose = false; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* make http request to url with method and headers |
19
|
|
|
* then set apiRelayResponse with reponse content |
20
|
|
|
* and apiRelayHttpCode with status code |
21
|
|
|
* |
22
|
|
|
* @param string $method |
23
|
|
|
* @param string $url |
24
|
|
|
* @param array $headers |
25
|
|
|
* @param array $datas |
26
|
|
|
* @return self |
27
|
|
|
*/ |
28
|
2 |
|
protected function apiRelayRequest( |
29
|
|
|
string $method, |
30
|
|
|
string $url, |
31
|
|
|
array $headers = [], |
32
|
|
|
$datas = [] |
33
|
|
|
): self { |
34
|
2 |
|
$cha = curl_init(); |
35
|
2 |
|
if (false !== $cha) { |
36
|
2 |
|
curl_setopt($cha, CURLOPT_VERBOSE, false); |
37
|
2 |
|
curl_setopt($cha, CURLOPT_URL, $url); |
38
|
2 |
|
curl_setopt($cha, CURLOPT_POST, ($method == Request::METHOD_POST)); |
39
|
2 |
|
curl_setopt($cha, CURLOPT_TIMEOUT, 300); |
40
|
2 |
|
curl_setopt($cha, CURLOPT_USERAGENT, IApi::USER_AGENT); |
41
|
2 |
|
curl_setopt($cha, CURLOPT_BUFFERSIZE, IApi::BUFFER_SIZE); |
42
|
2 |
|
curl_setopt($cha, CURLOPT_HTTPHEADER, $headers); |
43
|
2 |
|
if ($this->apiRelayOptionHeader) { |
44
|
|
|
curl_setopt($cha, CURLOPT_VERBOSE, 1); |
45
|
|
|
curl_setopt($cha, CURLOPT_HEADER, 1); |
46
|
|
|
} |
47
|
2 |
|
$this->apiRelayRequestSetPostData($cha, $method, $datas); |
48
|
2 |
|
curl_setopt($cha, CURLOPT_RETURNTRANSFER, 1); |
49
|
2 |
|
$result = curl_exec($cha); |
50
|
2 |
|
$error = (false === $result); |
51
|
2 |
|
$this->apiRelayResponse = ($error) ? 'Api relay error' : $result; |
52
|
2 |
|
$curlInfoCode = ($error) ? 500 : curl_getinfo($cha, CURLINFO_HTTP_CODE); |
53
|
2 |
|
$this->apiRelayHttpCode = (false === $curlInfoCode) ? 500 : $curlInfoCode; |
54
|
2 |
|
if ($this->apiRelayOptionHeader && false === $error) { |
55
|
|
|
$this->apiRelayHeaders = []; |
56
|
|
|
$headerSize = curl_getinfo($cha, CURLINFO_HEADER_SIZE); |
57
|
|
|
$rawHeaders = substr($this->apiRelayResponse, 0, $headerSize); |
|
|
|
|
58
|
|
|
$this->apiRelayHeaders = explode("\r\n\r\n", $rawHeaders, 2); |
59
|
|
|
$this->apiRelayResponse = substr( |
60
|
|
|
$this->apiRelayResponse, |
61
|
|
|
$headerSize |
62
|
|
|
); |
63
|
|
|
} |
64
|
2 |
|
if (false === $error) { |
65
|
2 |
|
curl_close($cha); |
66
|
|
|
} |
67
|
|
|
} |
68
|
2 |
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* patch curl instance to provide posted datas if required |
73
|
|
|
* |
74
|
|
|
* @param mixed $cha |
75
|
|
|
* @param string $method |
76
|
|
|
* @param array $datas |
77
|
|
|
* @return void |
78
|
|
|
*/ |
79
|
|
|
protected function apiRelayRequestSetPostData(&$cha, string $method, array $datas) |
80
|
|
|
{ |
81
|
|
|
if (is_resource($cha)) { |
82
|
|
|
if (Request::METHOD_POST == $method && !empty($datas)) { |
83
|
|
|
curl_setopt( |
84
|
|
|
$cha, |
85
|
|
|
CURLOPT_POSTFIELDS, |
86
|
|
|
http_build_query($datas) |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|