1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Reuse\Controllers\Api; |
4
|
|
|
|
5
|
|
|
use App\Http\Request; |
6
|
|
|
|
7
|
|
|
trait TRelay |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
protected $apiRelayResponse; |
11
|
|
|
protected $apiRelayHttpCode; |
12
|
|
|
protected $apiRelayHeaders; |
13
|
|
|
protected $apiRelayOptionHeader = false; |
14
|
|
|
protected $apiRelayOptionVerbose = false; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* make http request to url with method and headers |
18
|
|
|
* then set apiRelayResponse with reponse content |
19
|
|
|
* and apiRelayHttpCode with status code |
20
|
|
|
* |
21
|
|
|
* @param string $method |
22
|
|
|
* @param string $url |
23
|
|
|
* @param array $headers |
24
|
|
|
* @param array $datas |
25
|
|
|
* @return void |
26
|
|
|
*/ |
27
|
|
|
protected function apiRelayRequest(string $method, string $url, array $headers = [], $datas = []) |
28
|
|
|
{ |
29
|
|
|
$cha = curl_init(); |
30
|
|
|
curl_setopt($cha, CURLOPT_VERBOSE, false); |
|
|
|
|
31
|
|
|
curl_setopt($cha, CURLOPT_URL, $url); |
32
|
|
|
curl_setopt($cha, CURLOPT_POST, ($method == Request::METHOD_POST)); |
33
|
|
|
curl_setopt($cha, CURLOPT_TIMEOUT, 300); |
34
|
|
|
curl_setopt($cha, CURLOPT_USERAGENT, self::USER_AGENT); |
|
|
|
|
35
|
|
|
curl_setopt($cha, CURLOPT_BUFFERSIZE, self::BUFFER_SIZE); |
|
|
|
|
36
|
|
|
curl_setopt($cha, CURLOPT_HTTPHEADER, $headers); |
37
|
|
|
if ($this->apiRelayOptionHeader) { |
38
|
|
|
curl_setopt($cha, CURLOPT_VERBOSE, 1); |
39
|
|
|
curl_setopt($cha, CURLOPT_HEADER, 1); |
40
|
|
|
} |
41
|
|
|
if ($method == Request::METHOD_POST && $datas) { |
|
|
|
|
42
|
|
|
curl_setopt( |
43
|
|
|
$cha, |
44
|
|
|
CURLOPT_POSTFIELDS, |
45
|
|
|
http_build_query($datas) |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
curl_setopt($cha, CURLOPT_RETURNTRANSFER, 1); |
49
|
|
|
$this->apiRelayResponse = curl_exec($cha); |
|
|
|
|
50
|
|
|
$this->apiRelayHttpCode = curl_getinfo($cha, CURLINFO_HTTP_CODE); |
|
|
|
|
51
|
|
|
if ($this->apiRelayOptionHeader) { |
52
|
|
|
$this->apiRelayHeaders = []; |
53
|
|
|
$headerSize = curl_getinfo($cha, CURLINFO_HEADER_SIZE); |
54
|
|
|
$rawHeaders = substr($this->apiRelayResponse, 0, $headerSize); |
55
|
|
|
$this->apiRelayHeaders = explode("\r\n\r\n", $rawHeaders, 2); |
56
|
|
|
$this->apiRelayResponse = substr( |
57
|
|
|
$this->apiRelayResponse, |
58
|
|
|
$headerSize |
59
|
|
|
); |
60
|
|
|
} |
61
|
|
|
curl_close($cha); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|