Automation/app/Libraries/LoginLibrary.php
2024-08-30 19:32:21 +09:00

48 lines
1.1 KiB
PHP

<?php
namespace App\Libraries;
class LoginLibrary
{
private $url;
private $credentials;
private $cookieFile;
// $credentials = [
// 'username' => 'your_username',
// 'password' => 'your_password'
// ];
public function __construct(string $url, array $credentials)
{
$this->url = $url;
$this->credentials = $credentials;
$this->cookieFile = tempnam(sys_get_temp_dir(), 'cookie'); // 임시 쿠키 파일 생성
}
public function execute()
{
$ch = curl_init($this->url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($this->credentials));
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookieFile); // 쿠키를 저장할 파일 지정
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
public function getCookie()
{
return $this->cookieFile;
}
public function clearCookie()
{
// 쿠키 파일 삭제
unlink($this->cookieFile);
}
}