cURL이란?
client URL
cURL은 다양한 통신 프로토콜을 이용하여 데이터를 전송하기 위한 라이브러리와 명령을 제공하는 컴퓨터 소프트웨어 프로젝트이다.
cURL을 통해 웹 페이지의 소스를 가져오거나 로그인, 쿠키값을 가져오는 등의 다양한 기능을 사용할 수 있다.
커맨드에서도 cURL을 사용할 수 있는데, php에서 cURL을 사용해서 자동 로그인을 해보았다.
cURL로 로그인하기
로그인 아이디와 패스워드, 로그인할 페이지 URL, user-agent, 로그인 후 접속할 페이지 URL을 각각 입력한다.
$submit['ID'] = "아이디";
$submit['PW' ] = "패스워드";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,"로그인 페이지 URL");
curl_setopt($curl, CURLOPT_COOKIELIST, "");
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $submit);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_USERAGENT, "User-Agent 입력");
$result = curl_exec($curl);
curl_setopt($curl, CURLOPT_URL,"로그인 후 접속할 URL");
$result = curl_exec($curl);
print_r($result);
curl_close($curl);
exit;
$result에 url 웹 페이지 소스가 담겨서 출력된다.
User-Agent
user-agent 는 F12(개발자도구)에서 본인 user-agent 를 사용해도 되고, 웹이나 아래 URL을 통해서 입력해도 된다.
user-agent를 입력하는 이유는 특정 user-agent로 접속될 경우 차단되어 접속이 안되는 경우도 있기 때문에 입력한다.
https://www.useragentstring.com/pages/useragentstring.php
UserAgentString.com - List of User Agent Strings
www.useragentstring.com
User-Agent 관련 게시글
https://dev-wisdom.tistory.com/52
User Agent에 대해 알아보자
Usear Agent란 User Agent는 웹을 요청하는 사용자의 정보를 담고 있는 Request Header 종류 중 하나이다. 개발자도구(F12)를 통해 Headers 에서 내 user-agent를 확인할 수 있다. 나는 크롬을 통해 접속해서, user-a
dev-wisdom.tistory.com
'Development > PHP' 카테고리의 다른 글
[PHP] 로그파일에 소스코드가 출력되는 경우 (2) | 2023.01.12 |
---|---|
PHP 로그에 소스코드가 출력될 때 (0) | 2023.01.11 |
php timestamp 13자리 변환하기 (0) | 2022.12.16 |
[fsockopen()오류] php_network_getaddresses: getaddrinfo failed (0) | 2022.12.16 |
[PHP] 쉘 함수 exec()와 파라미터 전달 (0) | 2022.11.17 |