2011-03-25 47 views
0

我遇到了用我寫的PHP代碼登錄到Yahoo的問題。該代碼工作較早,但由於某種原因,它停止工作。它總是顯示雅虎登錄頁面,頂部有我的用戶名,如Hi Sudhir在標題處,就好像我已經登錄了,還有我的雅虎用戶名,但又顯示密碼字段和登錄按鈕,並再次顯示Please Verify your password在php中使用cURL登錄到Yahoo的問題

雖然我試圖再次採取新的表單動作https://login.yahoo.com/config/login_verify2?重新發送請求,但它不斷顯示相同的頁面。以下是我完整的雅虎登錄代碼。

請建議我做錯了什麼,或者我錯過了什麼?

<?php 
$yahooCalUrl = "http://calendar.yahoo.com"; 
$ch = curl_init($yahooCalUrl); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies_new.txt"); 
//curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies_new.txt"); 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
$resCalUrl = curl_exec($ch); 
//echo $resCalUrl; 
$loginUrl = "https://login.yahoo.com/config/login?"; 

//get the hidden values 
$searchStr = "/name=\".u\" value=\"(.*?)\"/"; 
preg_match($searchStr, $resCalUrl, $matches); 
$u = $matches[1]; 

$searchStr1 = "/name=\".challenge\" value=\"(.*?)\"/"; 
preg_match($searchStr1, $resCalUrl, $matches1); 
$challenge = $matches1[1]; 

$searchStr2 = "/name=\".pd\" value=\"(.*?)\"/"; 
preg_match($searchStr2, $resCalUrl, $matches2); 
$pd = $matches2[1]; 

$username = "my_yahoo_username"; 
$password = "my_yahoo_password"; 
//$mdPassword = md5($password); 
//$finalPass = $mdPassword . $challenge; 
//$hash = md5($finalPass); 
//$finalHash = urlencode($hash); 
$done = urlencode("http://calendar.yahoo.com"); 
$postFields = ".tries=1&.src=fpctx&.md5=&.hash=&.js=&.last=&promo=&.intl=us&.bypass=&.partner=&.u=".$u."&.v=0&.challenge=".$challenge."&.yplus=&.emailCode=&pkg=&stepid=&.ev=&hasMsgr=0&.chkP=Y&.done=http%3A%2F%2Fwww.yahoo.com%2F&.pd=fpctx_ver%3D0%26c%3D%26ivt%3D%26sg%3D&pad=3&aad=3&login=".$username."&passwd=".$password."&.save=&passwd_raw="; 
//$postFields = ".tries=1&.src=&.md5=&.hash=&.js=&.last=&promo=&.intl=us&.bypass=&.partner=&.u=".$u."&.v=0&.challenge=".$challenge."&.yplus=&.emailCode=&pkg=&stepid=&.ev=&hasMsgr=1&.chkP=Y&.pd=".$pd."&pad=6&aad=6&.persistent=&.save=1&login=".$username."&passwd=".$password."&.done=".$done; 
curl_setopt($ch, curlOPT_USERAGENT, $agent); 
curl_setopt($ch, CURLOPT_URL, $loginUrl); 
curl_setopt($ch, CURLOPT_REFERER, $loginUrl); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_POST, true); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
//curl_setopt($ch, CURLOPT_COOKIEJAR, "my_cookies_new.txt"); 
curl_setopt($ch, CURLOPT_COOKIEFILE, "my_cookies_new.txt"); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 
$finalLoggedIn = curl_exec($ch); 
echo $finalLoggedIn; 

?> 

雖然我嘗試使用密碼哈希值,即雅虎的動態信道值與我的密碼組合,但它也不能工作了這種方式。

+0

也許一個愚蠢的問題,但你還能登錄「正常」的方式?另外,請檢查登錄時正在發送的標題。也許他們的形式已經發生了變化! – ChrisH 2011-03-25 09:57:00

+0

上面的代碼在我的本地服務器上工作正常,但是當我將它上載到活服務器時,它根本不起作用,始終保持顯示登錄頁面。我現場有一臺Linux服務器。可能是什麼問題呢..?請建議 – 2011-03-29 07:40:28

+0

請檢查您是否具有相同的PHP.ini設置,適用於實況和本地。請確保你也顯示了所有的錯誤'ini_set('display_errors','on'); error_reporting(E_ALL);' – ChrisH 2011-03-29 09:39:22

回答

1

看起來沒有人對此作出迴應。我知道它是一個令人討厭的問題。

我看你在哪裏設置cookie名稱/ jar ...但不能打開文件來讀/寫。解決的辦法是多likley使用該文件第一fopen() ...

$cookie_jar_file = "my_cookies_new.txt"; 
$fp = fopen($cookie_jar_file, "w"); 

curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar_file); 

另外:

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 

不要忘記關閉打開的文件,並刪除它在腳本的結尾。

fclose($fp); 

unlink($cookie_jar_file); 

希望這可以幫助別人。 下面是我將如何做到這一點:

1:從雅虎獲取您需要的信息。

// ********************************************************************************************* 
// GET CHALLENGE 
// ********************************************************************************************* 
    $cs = curl_init(); 
    curl_setopt($cs, CURLOPT_USERAGENT, $agent); 
    curl_setopt($cs, CURLOPT_REFERER, "http://m.google.com/"); 
    curl_setopt($cs, CURLOPT_URL, "https://login.yahoo.com/config/login_verify2?"); 
// curl_setopt($cs, CURLOPT_POSTFIELDS, "login=".$id."&passwd=".$pass."&.src=&.tries=5&.bypass=&.partner=&.md5=&.hash=&.intl=us&.tries=1&.challenge=ydKtXwwZarNeRMeAufKa56.oJqaO&.u=dmvmk8p231bpr&.yplus=&.emailCode=&pkg=&stepid=&.ev=&hasMsgr=0&.v=0&.chkP=N&.last=&.done=http://m.yahoo.com/w/ygo-mail/"); 
    curl_setopt($cs, CURLOPT_COOKIEJAR, $cookie_jar_file); 
    curl_setopt($cs, CURLOPT_COOKIEFILE, $cookie_jar_file); 
    curl_setopt($cs, CURLOPT_HEADER, 1); 
    //curl_setopt($cs, CURLOPT_FOLLOWLOCATION, 1); 
    curl_setopt($cs, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($cs, CURLOPT_TIMEOUT, 30); 
    curl_setopt($cs, CURLOPT_CONNECTTIMEOUT,5); 
// curl_setopt($cs, CURLOPT_HTTPPROXYTUNNEL, 1); 
// curl_setopt($cs, CURLOPT_PROXY, $proxy); 
    curl_setopt($cs, CURLOPT_SSL_VERIFYPEER, FALSE); 
    curl_setopt($cs, CURLOPT_SSL_VERIFYHOST, 2); 

    $y_login_page = curl_exec($cs); 
    //print $y_login_page; 
    curl_close($cs); 


$proceed = true; 


if(preg_match("/type=\"hidden\" name=\".challenge\" value=\"(.*?)\"/", $y_login_page, $challenge)){ 
    print "<PRE>"; 
    print_r($challenge); 
    print"</PRE>"; 
    print "FOUND CHALLENGE STRING<BR>"; 
    $chal = $challenge[1]; 
} 
else { 
    print "\nNO CHALLENGE STRING"; 
    $proceed = false; 
} 
if(preg_match("/type=\"hidden\" name=\".u\" value=\"(.*?)\"/", $y_login_page, $array_u)){ 
    print "<PRE>"; 
    print_r($array_u); 
    print"</PRE>"; 
    print "FOUND .U STRING<BR>"; 
    $u = $array_u[1]; 
} 
else { 
    print "\nNO U STRING"; 
    $proceed = false; 
} 
if(preg_match("/type=\"hidden\" name=\".pd\" value=\"(.*?)\"/", $y_login_page, $array_pd)){ 
    print "<PRE>"; 
    print_r($array_pd); 
    print"</PRE>"; 
    print "FOUND .PD STRING<BR>"; 
    $pd = $array_pd[1]; 
} 

if(preg_match("/ts=(.*?)&/", $y_login_page, $array_ts)){ 
    print "<PRE>"; 
    print_r($array_ts); 
    print"</PRE>"; 
    print "FOUND TIME STAMP STRING<BR>"; 
    $xts = $array_ts[1]; 
} 

然後使用您收集的用戶名和通過登錄的信息。

// ********************************************************************************************* 
// CURL SESSION #1: AUTHENTICATE THE USER ID ON YAHOO'S SERVER 
// ********************************************************************************************* 
$cs = curl_init(); 
curl_setopt($cs, CURLOPT_USERAGENT, $agent); 
curl_setopt($cs, CURLOPT_REFERER, "http://m.google.com/"); 
curl_setopt($cs, CURLOPT_URL, "http://login.yahoo.com/config/login?");//http://login.yahoo.com/config/login? 
curl_setopt($cs, CURLOPT_POSTFIELDS, ".tries=1&.src=&.md5=&.hash=&.js=&.last=&promo=&.intl=us&.lang=en-US&.bypass=&.partner=&.u=".$u."&.v=0&.challenge=".$chal."&.yplus=&.emailCode=&pkg=&stepid=&.ev=&hasMsgr=0&.chkP=Y&.done=http://calendar.yahoo.com&.pd=".$pd."&.ws=0&.cp=0&nr=0&pad=5&aad=5&login=".$id."&passwd=".$pass."&.persistent=y&.save="); 
curl_setopt($cs, CURLOPT_COOKIEJAR, $cookie_jar_file); 
curl_setopt($cs, CURLOPT_COOKIEFILE, $cookie_jar_file); 
curl_setopt($cs, CURLOPT_HEADER, 1); 
curl_setopt($cs, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($cs, CURLOPT_TIMEOUT, 30); 
curl_setopt($cs, CURLOPT_CONNECTTIMEOUT,5); 
//curl_setopt($cs, CURLOPT_HTTPPROXYTUNNEL, 1); 
//curl_setopt($cs, CURLOPT_PROXY, $proxy); 
$y_login_page = curl_exec($cs); 
print $y_login_page; 

這裏的PostFeilds有一個名爲「done」的字段。

設置,爲done=http://calendar.yahoo.com

或任何你想要在登錄後去。