2013-05-08 121 views
6

我試圖登錄到使用PHP +捲曲的Twitter,但我覺得有些不對勁符合我的要求,因爲我得到這個作爲迴應:登錄到Twitter的,捲曲

東西是技術上的錯誤。

感謝您的注意 - 我們將會修復它,讓事情很快回到 。

我使用的PHP代碼是:

<?php 
      $ch = curl_init($sTarget); 
      curl_setopt($ch, CURLOPT_HEADER, false); 
      curl_setopt($ch, CURLOPT_POST, true); 
      curl_setopt($ch, CURLOPT_POSTFIELDS, $sPost); 
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 
      curl_setopt($ch, CURLOPT_COOKIESESSION, true); 
      curl_setopt($ch, CURLOPT_COOKIEJAR, $_CKS); 
      curl_setopt($ch, CURLOPT_COOKIEFILE, $_CKS); 
      curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); 
      curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data")); 
      curl_setopt($ch, CURLOPT_REFERER, "https://twitter.com"); 

?> 

$ sPost看起來是這樣的:

會議[username_or_email] =用戶&會話[密碼] =密碼& remember_me = 1 & scribe_log = & redirect_after_login = & authenticity_token = 6e706165609354bd7a92a99cf94e09140ea86c6f

該代碼首先獲取登錄表單字段,以便post變量具有適當的值(auth token)。我只是嘗試了一切,是的,我知道這有一個API,但我寧願找出如何做到手動學習的緣故。

在此先感謝。

回答

9

CURLOPT_COOKIESESSION用於指示新的會話。這不是你想要的,因爲你需要在第二篇文章中發送會話cookie。

我得到了Twitter的登錄,使用此代碼工作:

<?php 

# 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_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 = "[email protected]"; 
$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); 

?> 

PS:對不起,不讀您的文章正確的第一次。

+0

我將如何去保持用戶使用此登錄?該腳本用於登錄用戶,但會話不會保留。 – Zy0n 2014-01-24 12:20:39

+0

您將使用CURLOPT_COOKIEJAR獲取存儲在文件中的cookie,以便在隨後的請求中重新使用它們。 – 2015-12-23 12:21:26

2

我注意到兩兩件事:

1)儘量URL編碼的POST數據

如: 會話%5Busername_or_email%5D =用戶&會話%5Bpassword%5D =密碼...

代替: 會話[username_or_email] =用戶&會話[口令] =密碼...

2)Twitter已經命名authenticity_token隱藏字段在登錄表單中。它必然會議。因此,您不能使用靜態authenticity_token,您必須先閱讀登錄表單並從那裏使用authenticity_token字段。

希望有所幫助。

+0

我已經有了像我說過的那樣需要動態授權令牌的代碼。也試圖對變量進行url編碼,但仍然沒有效果,謝謝你的幫助。 – JustaN00b 2013-05-08 14:56:53