2012-01-24 37 views
17

Tôi có mã này để đăng nhập vào Google bằng Trình phân tích cú pháp DOM đơn giản với curl. Tôi đã thử thêm vào tệp cookiejar nhưng không có kết quả. Tôi tiếp tục nhận được thông báo:Đăng nhập vào Google bằng PHP và Curl, Cookie đã bị tắt?

Chức năng cookie của trình duyệt của bạn bị tắt. Hãy bật nó lên.

Bất kỳ ý tưởng nào về cách giải quyết vấn đề này?

Dưới đây là mã của tôi để tham khảo:

$html = file_get_html('https://accounts.google.com/ServiceLogin?hl=en&service=alerts&continue=http://www.google.com/alerts/manage'); 

//... some code for getting post data here 

$curl_connection = curl_init('https://accounts.google.com/ServiceLoginAuth'); 
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30); 
curl_setopt($curl_connection, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"); 
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($curl_connection, CURLOPT_COOKIEJAR, COOKIEJAR); 
curl_setopt($curl_connection, CURLOPT_COOKIEFILE, COOKIEJAR); 
curl_setopt($curl_connection, CURLOPT_HEADER, true); 
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER,1); 
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 120); 
curl_setopt($curl_connection, CURLOPT_TIMEOUT, 120); 
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string); 

$result = curl_exec($curl_connection); 
curl_close($curl_connection); 

echo $result; 
+0

Có lẽ bạn nên tìm nạp URL trong hàm 'file_get_html' bằng cách sử dụng cURL thay vì nó có thể đặt một số cookie dịch vụ xác thực có thể đang tìm kiếm biểu mẫu. Ngoài ra, bạn có thể xác nhận tệp được chỉ định bởi 'COOKIEJAR' đang được tạo và chứa cookie không? – drew010

+0

Tôi đã kiểm tra tệp COOKIEJAR, yep nó chứa một số văn bản bên trong. Tôi cũng đặt url curl_init thành url giống như file_get_html, vẫn giống nhau, không có cookie cho tôi. :( – kazuo

+0

Tôi nhận được một số tiêu đề ở đây, có vấn đề gì không? Ở đây chúng là: HTTP/1.1 200 OK-Cookie: GoogleAccountsLocale_session = vi; Bộ mã hóa an toàn: GAPS = 1: ZuuFm50cJM2_fiqQc38hkyuCjZXRRg: bMuhAssScKIBtI1L; Path = /; Expires = Thu, 23-Jan-2014 18:32:24 GMT; Bảo mật; HttpOnly Loại Nội dung: văn bản/html; charset = UTF-8 Nghiêm ngặt-Giao thông-Bảo mật: tối đa tuổi = 2592000; includeSubDomains Ngày: Thứ Ba, ngày 24 tháng 1 năm 2012 18:32:24 GMT Hết hạn: Thứ Ba, ngày 24 tháng 1 năm 2012 18:32:24 GMT Cache-Control: riêng tư, max-age = 0 X-Content-Type-Options: nosniff X-XSS-Protection: 1; mode = block Độ dài nội dung: 1848 Máy chủ: GSE – kazuo

Trả lời

26

Dưới đây là một số mã sửa đổi mà làm việc.

Trước tiên, trang yêu cầu trang đăng nhập để lấy cookie ban đầu và trích xuất các giá trị bắt buộc cho biểu mẫu đăng nhập. Tiếp theo nó thực hiện một bài đăng đến dịch vụ đăng nhập. Sau đó, nó kiểm tra xem liệu nó có đang cố sử dụng javascript và thẻ meta để chuyển hướng đến URL đích hay không.

Dường như bạn đã có mã để lấy các trường biểu mẫu, vì vậy tôi đã không đăng bài của tôi, nhưng nếu bạn cần nó cho tôi biết. Chỉ cần đảm bảo $formFields là một mảng kết hợp với các khóa là tên trường và giá trị là giá trị trường.

<?php 

/** 
* Log in to Google account and go to account page 
* 
*/ 

$USERNAME = '[email protected]'; 
$PASSWORD = 'password'; 
$COOKIEFILE = 'cookies.txt'; 

// initialize curl handle used for all requests 
$ch = curl_init(); 

// set some options on the handle 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); 
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:49.0) Gecko/20100101 Firefox/49.0"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
curl_setopt($ch, CURLOPT_COOKIEJAR, $COOKIEFILE); 
curl_setopt($ch, CURLOPT_COOKIEFILE, $COOKIEFILE); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120); 
curl_setopt($ch, CURLOPT_TIMEOUT, 120); 

// url of our first request fetches the account login page 
curl_setopt($ch, CURLOPT_URL, 
    'https://accounts.google.com/ServiceLogin?hl=en&service=alerts&continue=http://www.google.com/alerts/manage'); 
$data = curl_exec($ch); 

// extract form fields from account login page 
$formFields = getFormFields($data); 

// inject email and password into form 
$formFields['Email'] = $USERNAME; 
$formFields['Passwd'] = $PASSWORD; 
unset($formFields['PersistentCookie']); 

$post_string = http_build_query($formFields); // build urlencoded POST string for login 

// set url to login page as a POST request 
curl_setopt($ch, CURLOPT_URL, 'https://accounts.google.com/ServiceLoginAuth'); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string); 

// execute login request 
$result = curl_exec($ch); 

// check for "Redirecting" message in title to indicate success 
// based on your language - you may need to change this to match some other string 
if (strpos($result, '<title>Redirecting') === false) { 
    die("Login failed"); 
    var_dump($result); 
} 

// login likely succeeded - request account page; unset POST so we do a regular GET 
curl_setopt($ch, CURLOPT_URL, 'https://myaccount.google.com/?utm_source=OGB'); 
curl_setopt($ch, CURLOPT_POST, 0); 
curl_setopt($ch, CURLOPT_POSTFIELDS, null); 

// execute request for login page using our cookies 
$result = curl_exec($ch); 

echo $result; 


// helpef functions below 

// find google "#gaia_loginform" for logging in 
function getFormFields($data) 
{ 
    if (preg_match('/(<form.*?id=.?gaia_loginform.*?<\/form>)/is', $data, $matches)) { 
     $inputs = getInputs($matches[1]); 

     return $inputs; 
    } else { 
     die('didnt find login form'); 
    } 
} 

// extract all <input fields from a form 
function getInputs($form) 
{ 
    $inputs = array(); 

    $elements = preg_match_all('/(<input[^>]+>)/is', $form, $matches); 

    if ($elements > 0) { 
     for($i = 0; $i < $elements; $i++) { 
      $el = preg_replace('/\s{2,}/', ' ', $matches[1][$i]); 

      if (preg_match('/name=(?:["\'])?([^"\'\s]*)/i', $el, $name)) { 
       $name = $name[1]; 
       $value = ''; 

       if (preg_match('/value=(?:["\'])?([^"\'\s]*)/i', $el, $value)) { 
        $value = $value[1]; 
       } 

       $inputs[$name] = $value; 
      } 
     } 
    } 

    return $inputs; 
} 
+0

Whoa! Cảm ơn vì điều này! Tôi đã thử nó mặc dù và tôi nhận được một đăng nhập thất bại. mảng post_data của tôi vào mảng formFields của bạn, đây là chuỗi: continue = http% 3A% 2F% 2Fwww.google.com% 2Falerts% 2Fmanage & service = alert & dsh = -6553802846829809996 & hl = vi & GALX = Cg4X gqEmZ_w & timeStmp = & secTok = & Email = xxxxxxxx & Passwd = xxxxxxxxxxx & signIn = Đăng nhập + in & rmShown = 1 Sau khi thất bại, không có đầu ra nào khác. – kazuo

+0

Nevermind, tôi đã nhận nó, cảm ơn!: D Tôi sẽ cố gắng để xem những gì làm cho bạn làm việc và tôi không làm việc. – kazuo

+0

Dường như giả sử nó đã được chuyển đến đúng biến cho curl. Tôi vừa cập nhật mã với phiên bản đầy đủ lấy các trường ẩn. Nhập tên người dùng và mật khẩu của bạn vào đó và xem nó có phù hợp với bạn hay không. Tôi vừa xác nhận lại toàn bộ ví dụ hoạt động. Nếu đăng nhập thất bại, nó sẽ var_dump trang web kết quả. – drew010

Các vấn đề liên quan