servermgrv2/app/Libraries/Adapter/API/LocalAdapter.php
2023-07-19 16:23:30 +09:00

54 lines
1.9 KiB
PHP

<?php
namespace App\Libraries\Adapter\API;
class LocalAdapter extends Adapter
{
public function __construct($entity, $debug = false)
{
parent::__construct($entity, $debug);
}
protected function getClient() //Codeigniter4 Service의 curlrequest이용시
{
if (is_null($this->_client)) {
// 참조:https://www.codeigniter.com/user_guide/libraries/curlrequest.html?highlight=curl#
// ex:)$options = [ 'baseURI' => 'http://www.foo.com/1.0/', 'timeout' => 0, 'allow_redirects' => false, 'proxy' => '192.168.16.1:10' ]
$options = [
'baseURI' => $this->getServerInfo(),
'auth' => $this->getAccountInfo(),
'verify' => $this->isSSLVerifiy(),
'cookie' => $this->getCookieFile(),
];
if ($this->_debug) {
$options['debug'] = PATHS['API'] . getenv('api.adapter.debug') ? getenv('api.adapter.debug') : "api-debug_" . date("Ymd");
}
$this->_client = \Config\Services::curlrequest($options);
}
return $this->_client;
}
protected function requestURL(string $url, string $method, array $datas = []): object
{
$options = array();
switch ($method) {
case 'POST':
$response = $this->getClient()->setBody($datas)->request($method, $url, $options);
break;
case 'HEAD':
break;
default:
$response = $this->getClient()->request($method, $url, $options);
break;
}
dd($response);
if ($response->getStatusCode() != 200) {
throw new \Exception(sprintf(
"오류가 발생하였습니다.\n%s\n%s",
$response->getHeaderLine('content-type'),
$response->getBody()
));
}
return json_decode($response->getBody());
}
}