2014-01-24 5 views
-1

그래서 나는 cURL을 사용하여 트위터에 로그인하는이 스크립트를 가지고 있습니다. 그러나 문제는 스크립트가 작동하는 동안 임시 세션 만 생성되고 사용자는 로그인 상태가 유지되지 않습니다. 사용자가 실제로 로그인 상태인지 확인하기 위해 수정하는 방법은 무엇입니까?cURL을 사용하여 트위터로 로그인

# First call gets hidden form field authenticity_token 
# and session cookie 

$ch = curl_init(); 
$sTarget = "https://twitter.com/"; 
curl_setopt($ch, CURLOPT_URL, $sTarget); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); 
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/cookie.txt"); 
curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookie.txt"); 
curl_setopt($ch, CURLOPT_REFERER, "https://twitter.com/"); 
$html = curl_exec($ch); 

# parse authenticity_token out of html response 
preg_match('/<input type="hidden" value="([a-zA-Z0-9]*)" name="authenticity_token"\/>/', $html, $match); 
$authenticity_token = $match[1]; 

$username = "username"; 
$password = "password"; 

# set post data 
$sPost = "session[username_or_email]=$username&session[password]=$password&return_to_ssl=true&scribe_log=&redirect_after_login=%2F&authenticity_token=$authenticity_token"; 

# second call is a post and performs login 
$sTarget = "https://twitter.com/sessions"; 
curl_setopt($ch, CURLOPT_URL, $sTarget); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $sPost); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: application/x-www-form-urlencoded")); 

# display server response 
curl_exec($ch); 
curl_close($ch); 

답변

0

내가 추가 할 것입니다 :

curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE); 
curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookie.txt"); 
0

는 또한 쿠키 항아리 옵션을 사용합니다.

curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookie.txt"); 

는 기본적으로 CURLOPT_COOKIEFILE 옵션 컬 요청을 전송 중에 파일에서 쿠키를로드하는 데 사용합니다. 그리고 CURLOPT_COOKIEJAR 옵션을 사용하면 curl이 쿠키를 정의 된 파일에 저장 (응답)합니다.

+0

흠, 여전히 기쁨이 하나도 :/ – Zy0n

+0

나는 지금 사용하고있는 것을 반영하기 위해 OP를 편집했습니다. – Zy0n

+0

'$ password'에는 특수 문자가있을 수 있습니다. 패스워드 위에'urlencode()'를 써서 그것들을 벗어나십시오. 또한 curl의 VERBOSE 모드를 사용하여 트위터로부터받은 응답을 검사하십시오. 'curl_setopt ($ ch, CURLOPT_VERBOSE, true);' –

관련 문제