[PHP] php curl 을 이용한 자동 로그인
<?php #크론 다이렉트로 안 걸림. wget, curl, GET 등으로 걸어야 됨. define('AGENT', 'Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko'); define('COOKIE', 'cookie.txt'); $today = date("Y-m-d H:i:s"); $url = 'http://www.site.com/login_check.php'; $post_data["mb_id"] = "아이디"; $post_data["mb_password"] = "패스워드"; #<a href="http://kr.php.net/manual/kr/function.curl-setopt.php" target="_blank">http://kr.php.net/manual/kr/function.curl-setopt.php</a> $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); //접속할 URL 주소 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);#FALSE 를 설정하면 cURL는 서버 인증서의 유효성을 검사하지 않습니다.#다른 인증를 CURLOPT_CAINFO 옵션 지정하거나 CURLOPT_CAPATH 옵션 증명서 디렉토리를 지정합니다. #curl_setopt ($ch, CURLOPT_SSLVERSION,1); #SSL 버젼 지정. 기본값은 2 curl_setopt($ch, CURLOPT_HEADER, 1);#TRUE 를 설정하면 헤더의 내용을 출력합니다. curl_setopt($ch, CURLOPT_POST, 1);#TRUE 를 설정하면 HTTP POST를 수행합니다. POST는 application / x - www - form - urlencoded 식으로 이루어집니다. 이것은 일반적인 HTML 양식과 같은 형식입니다. curl_setopt($ch, CURLOPT_USERAGENT, AGENT);#HTTP 요청에서 사용되는 "User - Agent :" 헤더의 내용. curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE);#curl_close 호출 될 때 쿠키를 파일 이름으로 저장. curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIE);#쿠키의 데이터를 http 헤더를 통해 보낸다. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);#POST 로 데이터 전송. curl_setopt($ch, CURLOPT_TIMEOUT, 30);#최대 실행 시간(초) curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);#TRUE 를 설정하면 curl_exec () 의 반환 값을 문자열로 반환합니다. 일반적으로 데이터를 직접 출력합니다. $result = curl_exec($ch); # login 후 $url = 'http://www.site.com/check.php'; $post_data["data1"] = "data"; $post_data["data3"] = "출석체크 합니다."; curl_setopt($ch, CURLOPT_URL, $url); //접속할 URL 주소 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSLVERSION, 1); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_REFERER, $referer); curl_setopt($ch, CURLOPT_USERAGENT, AGENT); curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIE); curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIE); curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); curl_setopt($ch, CURLOPT_TIMEOUT, 30); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($ch); $file = fopen("./curl.log" , "a+") ; // Check if any error occured if (curl_errno($ch)) { echo 'Curl error: ' . curl_error($ch); fwrite($file , "$today : Curl error: ' . curl_error($ch) \n"); } curl_close($ch); #echo $result; $mystring = $result; $findme = '출석완료'; $pos = strpos($mystring, $findme); if ($pos === false) { fwrite($file , "$today : FALSE \n"); fwrite($file , "$result \n"); } else { //echo $pos; //echo "login Succecs"; fwrite($file , "$today : success \n"); } fclose($file); ?>
<?php function scrap() { $geturl="로그인후 스크랩할 주소"; $loginurl = '로그인 주소'; $postfields = 'id=아이디&password=비밀번호'; $cookieFile = $this->auth_site_cookie_store($loginurl,$postfields); echo $result = $this->auth_site_get($geturl, "/저장경로/".$cookieFile, $postfields); } function auth_site_cookie_store($loginurl, $postfields) { $parseURL = parse_url($loginurl); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,"$loginurl"); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, "$postfields"); curl_setopt($ch, CURLOPT_COOKIEJAR, "/저장경로/".$parseURL['host'].".cookie"); ob_start(); curl_exec ($ch); ob_end_clean(); curl_close ($ch); return $parseURL['host'].".cookie"; } function auth_site_get($geturl, $cookiefile, $postfields) { $parseURL = parse_url($geturl); $ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER, 1 ); curl_setopt($ch, CURLOPT_POST,1); curl_setopt($ch, CURLOPT_POSTFIELDS,$postfields); curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); curl_setopt($ch, CURLOPT_TIMEOUT,100); curl_setopt($ch, CURLOPT_COOKIEJAR, "/저장경로/".$parseURL['host'].".cookie"); curl_setopt($ch, CURLOPT_COOKIEFILE, "$cookiefile"); curl_setopt($ch, CURLOPT_URL,"$geturl"); $result = curl_exec ($ch); curl_close ($ch); return $result; } ?>