81 lines
3.1 KiB
PHP
81 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Libraries\MySocket;
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
use GuzzleHttp\Cookie\CookieJar;
|
|
use GuzzleHttp\Client;
|
|
|
|
abstract class MySocket extends Client
|
|
{
|
|
private $_cookieJar = null;
|
|
protected function __construct(array $config = [])
|
|
{
|
|
// SSL 인증서 검증을 비활성화
|
|
$config['verify'] = getenv("socket.web.ssl.verify") == "true" ? true : false;
|
|
parent::__construct($config);
|
|
}
|
|
final protected function getCookieJar(): CookieJar
|
|
{
|
|
if ($this->_cookieJar === null) {
|
|
$this->_cookieJar = new CookieJar();
|
|
}
|
|
return $this->_cookieJar;
|
|
}
|
|
protected function getUserAgent(): string
|
|
{
|
|
// User-Agent 목록 배열
|
|
$userAgents = [
|
|
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36',
|
|
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.2 Safari/605.1.15',
|
|
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0',
|
|
'Mozilla/5.0 (iPhone; CPU iPhone OS 14_7_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Mobile/15E148 Safari/604.1',
|
|
'Mozilla/5.0 (Linux; Android 10; SM-G973F) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Mobile Safari/537.36'
|
|
];
|
|
return $userAgents[array_rand($userAgents)];
|
|
}
|
|
protected function getRequestOptions(string $method, array $options = []): array
|
|
{
|
|
//cookies->쿠키값 , timeout->5초 안에 응답이 없으면 타임아웃
|
|
$requestOptions = [
|
|
'cookies' => $this->getCookieJar(),
|
|
'timeout' => getenv("socket.web.timeout"),
|
|
'headers' => [
|
|
'User-Agent' => $this->getUserAgent(),
|
|
],
|
|
];
|
|
return $requestOptions;
|
|
}
|
|
|
|
public function get($uri, array $options = []): ResponseInterface
|
|
{
|
|
return $this->request('GET', $uri, $options);
|
|
}
|
|
public function post($uri, array $options = []): ResponseInterface
|
|
{
|
|
return $this->request('POST', $uri, $options);
|
|
}
|
|
public function put($uri, array $options = []): ResponseInterface
|
|
{
|
|
return $this->request('PUT', $uri, $options);
|
|
}
|
|
public function patch($uri, array $options = []): ResponseInterface
|
|
{
|
|
return $this->request('PATCH', $uri, $options);
|
|
}
|
|
public function delete($uri, array $options = []): ResponseInterface
|
|
{
|
|
return $this->request('DELETE', $uri, $options);
|
|
}
|
|
public function request(string $method, $uri = '', array $options = []): ResponseInterface
|
|
{
|
|
$requestOptions = $this->getRequestOptions($method, $options);
|
|
$requestOptions[in_array($method, ['get', 'getAsync']) ? 'query' : 'json'] = $options;
|
|
log_message("debug", __FUNCTION__ .
|
|
"=> 호출 Socket URL:{$uri}\n--------------\n" .
|
|
var_export($options, true) .
|
|
"\n--------------\n");
|
|
return parent::request($method, $uri, $requestOptions);
|
|
}
|
|
}
|