servermgrv2/app/Libraries/Adapter/API/CurlAdapter.php
최준흠git config git config --helpgit config --global user.name 최준흠 b3659ee820 servermgrv2 init...
2023-07-22 23:19:44 +09:00

212 lines
6.6 KiB
PHP

<?php
namespace App\Libraries\Adapter\API;
// 참고:
// https://techhub.hpe.com/eginfolib/servers/docs/HPRestfultool/iLo4/data_model_reference.html
// https://github.com/SyntaxPhoenix/iloclient
class CurlAdapter extends Adapter
{
public function __construct($debug = false)
{
parent::__construct($debug);
}
private function debugging($response, array $datas = array())
{
if (!$this->_debug) {
return false;
}
if ($response === false) {
log_message('error', curl_error($this->getClient()));
}
$info = curl_getinfo($this->getClient());
log_message('debug', var_export($info, true));
log_message('debug', var_export($datas, true));
log_message('debug', "{$info['total_time']}초, url:{$info['url']}, return:{$info['http_code']}");
switch ($info['http_code']) {
case 100:
log_message('debug', "{$info['http_code']} Continue");
break;
case 101:
log_message('debug', "{$info['http_code']} Switching Protocols");
break;
case 200:
log_message('debug', "{$info['http_code']} OK");
break;
case 201:
log_message('debug', "{$info['http_code']} Created");
break;
case 202:
log_message('debug', "{$info['http_code']} Accepted");
break;
case 203:
log_message('debug', "{$info['http_code']} Non-Authoritative Information");
break;
case 204:
log_message('debug', "{$info['http_code']} No Content");
break;
case 205:
log_message('debug', "{$info['http_code']} Reset Content");
break;
case 206:
log_message('debug', "{$info['http_code']} Partial Content");
break;
case 300:
log_message('debug', "{$info['http_code']} Multiple Choices");
break;
case 301:
log_message('debug', "{$info['http_code']} Moved Permanently");
break;
case 302:
log_message('debug', "{$info['http_code']} Found");
break;
case 303:
log_message('debug', "{$info['http_code']} See Other");
break;
case 304:
log_message('debug', "{$info['http_code']} Not Modified");
break;
case 305:
log_message('debug', "{$info['http_code']} Use Proxy");
break;
case 306:
log_message('debug', "{$info['http_code']} (Unused)");
break;
case 307:
log_message('debug', "{$info['http_code']} Temporary Redirect");
break;
case 400:
log_message('debug', "{$info['http_code']} Bad Request");
break;
case 401:
log_message('debug', "{$info['http_code']} Unauthorized");
break;
case 402:
log_message('debug', "{$info['http_code']} Payment Required");
break;
case 403:
log_message('debug', "{$info['http_code']} Forbidden");
break;
case 404:
log_message('debug', "{$info['http_code']} Not Found");
break;
case 405:
log_message('debug', "{$info['http_code']} Method Not Allowed");
break;
case 406:
log_message('debug', "{$info['http_code']} Not Acceptable");
break;
case 407:
log_message('debug', "{$info['http_code']} Proxy Authentication Required");
break;
case 408:
log_message('debug', "{$info['http_code']} Request Timeout");
break;
case 409:
log_message('debug', "{$info['http_code']} Conflict");
break;
case 410:
log_message('debug', "{$info['http_code']} Gone");
break;
case 411:
log_message('debug', "{$info['http_code']} Length Required");
break;
case 412:
log_message('debug', "{$info['http_code']} Precondition Failed");
break;
case 413:
log_message('debug', "{$info['http_code']} Request Entity Too Large");
break;
case 414:
log_message('debug', "{$info['http_code']} Request-URI Too Long");
break;
case 415:
log_message('debug', "{$info['http_code']} Unsupported Media Type");
break;
case 416:
log_message('debug', "{$info['http_code']} Requested Range Not Satisfiable");
break;
case 417:
log_message('debug', "{$info['http_code']} Expectation Failed");
break;
case 500:
log_message('debug', "{$info['http_code']} Internal Server Error");
break;
case 501:
log_message('debug', "{$info['http_code']} Not Implemented");
break;
case 502:
log_message('debug', "{$info['http_code']} Bad Gateway");
break;
case 503:
log_message('debug', "{$info['http_code']} Service Unavailable");
break;
case 504:
log_message('debug', "{$info['http_code']} Gateway Timeout");
break;
case 505:
log_message('debug', "{$info['http_code']} HTTP Version Not Supported");
break;
default:
log_message('debug', "Return Code : {$info['http_code']}");
break;
}
}
protected function getClient()
{
if (is_null($this->_client)) {
$this->_client = curl_init();
}
return $this->_client;
}
protected function requestURL(string $url, string $method, array $datas = []): object
{
curl_setopt($this->getClient(), CURLOPT_URL, $this->getServerInfo() . $url);
switch ($method) {
case 'POST':
curl_setopt($this->getClient(), CURLOPT_POST, TRUE);
curl_setopt($this->getClient(), CURLOPT_CUSTOMREQUEST, 'PATCH');
curl_setopt($this->getClient(), CURLOPT_POSTFIELDS, json_encode($datas));
curl_setopt($this->getClient(), CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
//cookie값 파일저장용
curl_setopt($this->getClient(), CURLOPT_COOKIEJAR, API['COOKIE_FILE']);
curl_setopt($this->getClient(), CURLOPT_COOKIEFILE, API['COOKIE_FILE']);
break;
default:
break;
}
//cookie값 전달용
foreach (curl_getinfo($this->getClient(), CURLINFO_COOKIELIST) as $cookie_line) {
curl_setopt($this->getClient(), CURLOPT_COOKIELIST, $cookie_line);
}
//SSL 확인여부용
curl_setopt($this->getClient(), CURLOPT_SSL_VERIFYPEER, API['SSL_VERIFY']);
curl_setopt($this->getClient(), CURLOPT_SSL_VERIFYHOST, API['SSL_VERIFY']);
curl_setopt($this->getClient(), CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->getClient(), CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($this->getClient(), CURLOPT_USERPWD, implode(":", $this->getAccountInfo()));
//header값 전달용
$headers = [];
curl_setopt(
$this->getClient(),
CURLOPT_HEADERFUNCTION,
function ($curl, $header) use (&$headers) {
$length = strlen($header);
$header = explode(':', $header, 2);
if (count($header) < 2) { // ignore invalid headers
return $length;
}
$headers[strtolower(trim($header[0]))][] = trim($header[1]);
return $length;
}
);
$response = curl_exec($this->getClient());
$this->debugging($response);
curl_close($this->getClient());
if (is_null($response)) {
throw new \Exception("해당서버[{$this->getServerInfo()}]의 ILO접속 오류가 발생하였습니다.");
}
return json_decode($response);
}
}