80 lines
3.1 KiB
PHP
80 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Libraries\MySocket;
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
use GuzzleHttp\Cookie\CookieJar;
|
|
use GuzzleHttp\Client;
|
|
|
|
class MySocket extends Client
|
|
{
|
|
private $_cookieJar = null;
|
|
public function __construct(array $config = [])
|
|
{
|
|
// SSL 인증서 검증을 비활성화
|
|
$config['verify'] = env("socket.web.ssl.verify") ?? false;
|
|
$config['User-Agent'] = $config['User-Agent'] ?? $this->getUserAgent();
|
|
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초 안에 응답이 없으면 타임아웃
|
|
//method가 get이면 $request['query'] = $options , 다른것이면 $request['json] = $options
|
|
$options = [
|
|
// 'cookies' => $this->getCookieJar(),
|
|
'timeout' => env("socket.web.timeout") ?? 5,
|
|
in_array($method, ['get']) ? 'query' : 'json' => $options
|
|
];
|
|
return $options;
|
|
}
|
|
|
|
final public function get($uri, array $options = []): ResponseInterface
|
|
{
|
|
return $this->request('GET', $uri, $options);
|
|
}
|
|
final public function post($uri, array $options = []): ResponseInterface
|
|
{
|
|
return $this->request('POST', $uri, $options);
|
|
}
|
|
final public function put($uri, array $options = []): ResponseInterface
|
|
{
|
|
return $this->request('PUT', $uri, $options);
|
|
}
|
|
final public function patch($uri, array $options = []): ResponseInterface
|
|
{
|
|
return $this->request('PATCH', $uri, $options);
|
|
}
|
|
final public function delete($uri, array $options = []): ResponseInterface
|
|
{
|
|
return $this->request('DELETE', $uri, $options);
|
|
}
|
|
public function request(string $method, $uri = '', array $options = []): ResponseInterface
|
|
{
|
|
$options = $this->getRequestOptions($method, $options);
|
|
log_message("debug", __FUNCTION__ .
|
|
"=> 호출 Socket URL:{$uri}\n--------------\n" .
|
|
var_export($options, true) .
|
|
"\n--------------\n");
|
|
return parent::request($method, $uri, $options);
|
|
}
|
|
}
|