TIP게시판

제목 curl로 로그인 후에 데이터 가져오기
글쓴이 변종원(웅파) 작성시각 2013/07/04 17:17:30
댓글 : 4 추천 : 1 스크랩 : 0 조회수 : 25906   RSS
로그인 주소는 빼고 공개를 합니다. (주소는 알아서 찾으세요)

curl로 로그인 후에 그 정보를 파일로 저장하고
로그인 뒷단에 있는 마이페이지 같은 것을 curl로 가져올때 앞에서 생성한 파일을 같이 넘겨줘서
처리하는 방식입니다.

2개의 함수로 되어 있습니다.

소스는 간단하니 따로 설명은 안하겠습니다.

유용하게 쓰시길.....  (원 소스는 구글 검색해서 찾은 건데 어느 분 것인지 모르겠네요)

로그인정보 저장경로는 아무나 쓰기 가능하도록 퍼미션 주셔야 합니다.

<?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;
}
?>

 다음글 세션이 풀리는 문제 (4)
 이전글 로드밸런서가 적용된 아파치 Access Log 에서의 ... (4)

댓글

헛발이 / 2013/07/04 17:35:34 / 추천 0
감사합니다...


한대승(불의회상) / 2013/07/04 17:56:53 / 추천 0
오... 비싼거 인증 !!!!!

일억이네.. ^^
변종원(웅파) / 2013/07/04 18:27:42 / 추천 0
ㅎㅎㅎ 아쉽게도 견본이지만... 액면은 일억
테러보이 / 2015/10/20 18:18:11 / 추천 0
감사합니다.!!!