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