Automation/app/Libraries/MyWeb/MyWebLibrary.php
2024-09-03 19:25:54 +09:00

79 lines
1.9 KiB
PHP

<?php
namespace App\Libraries\MyWeb;
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Client;
class MyWebLibrary
{
private $_host = "";
private $_client = null;
private $_cookieJar = null;
private $_debug = false;
public function __construct(string $host)
{
$this->_host = $host;
}
final public function getHost(): string
{
return $this->_host;
}
final public function getClient(): Client
{
if ($this->_client === null) {
$this->_client = new Client(['verify' => false]);
}
return $this->_client;
}
final public function getCookieJar()
{
if ($this->_cookieJar === null) {
$this->_cookieJar = new CookieJar();
}
return $this->_cookieJar;
}
final public function getDebug(): bool
{
return $this->_debug;
}
final public function setDebug(bool $debug): void
{
$this->_debug = $debug;
}
final public function getContent(string $url): string
{
return $this->getClient()->get($this->gethost() . $url)->getBody();
}
// 로그인 메서드
final public function login($url, $username, $password)
{
try {
$response = $this->getClient()->post($this->gethost() . $url, [
'form_params' => [
'username' => $username,
'password' => $password,
],
'cookies' => $this->getCookieJar(),
]);
if ($response->getStatusCode() == 200) {
log_message("info", "로그인 성공!");
return true;
} else {
log_message("info", "로그인 실패: " . $response->getStatusCode());
return false;
}
} catch (\Exception $e) {
log_message("error", "로그인 중 오류 발생: " . $e->getMessage());
return false;
}
}
}