2013-11-03 151 views
2

我正在嘗試通過捲曲登錄drupal。我得到的消息:'記錄'一個命令: 回聲「記錄」;這是在if語句中,告訴我一切都很好。Drupal以捲曲登錄

運行腳本我打開我的主頁,不幸的是,我沒有。

登錄後,我認爲我有一個餅乾的問題。

<?php 
    ob_start(); // Initiate the output buffer 
    function mymodule_get_csrf_header() { 
     $curl_get = curl_init(); 
     curl_setopt_array($curl_get, array(
     CURLOPT_RETURNTRANSFER => 1, 
     CURLOPT_URL => 'http://will.sx/services/session/token', 
    )); 
     $csrf_token = curl_exec($curl_get); 
     curl_close($curl_get); 
     return 'X-CSRF-Token: ' . $csrf_token; 
    } 
    $username = 'test'; 
    $password = 'TEST'; 
    $request_url = 'http://will.sx/rests/user/login'; 
    $user_data = array(
     'username' => $username, 
     'password' => $password, 
    ); 
    $user_data = http_build_query($user_data); 

    $curl = curl_init($request_url); 
    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json')); // Accept JSON response 
    curl_setopt($curl, CURLOPT_POST, 1); // Do a regular HTTP POST 
    curl_setopt($curl, CURLOPT_POSTFIELDS, $user_data); // Set POST data 
    curl_setopt($curl, CURLOPT_HEADER, FALSE); // Ask to not return Header 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); 
    curl_setopt($curl, CURLOPT_FAILONERROR, TRUE); 
    curl_setopt($curl, CURLOPT_COOKIESESSION, true); 
    curl_setopt($curl, CURLOPT_COOKIEFILE, "cookie.txt"); 
    curl_setopt($curl, CURLOPT_COOKIEJAR, "cookie.txt"); 

    $response = curl_exec($curl); 
    $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); 
    if ($http_code == 200) { 
     $logged_user = json_decode($response); 
     echo 'logged'; 
    } 
    else { 
     $http_message = curl_error($curl); 
     die('Unable to connect to Basic CMS Engine! 
              Username or password incorrect! 
              Please enter valid username and password!'); 
    } 
    //setcookie(name,value,expire,path,domain,secure) 
    setcookie($logged_user->session_name,$logged_user->sessid,time() + 10000,'/'); 
    ob_end_flush(); // Flush the output from the buffer 
    ?> 

歡迎各種幫助。 在此先感謝。

+0

相關:[使得認證的請求到REST服務器](https://www.drupal.org/節點/ 910598)。 – kenorb

回答

1

檢查它首先,代碼200必須並不意味着你真的登陸,響應代碼200的意思是網絡服務器是告訴你,你的請求成功,但Web服務器不知道你是否登錄到Drupal或不。第二件事I open my homepage and unfortunately I wasn't logged in。你的意思是你打開你的瀏覽器?您的瀏覽器是否共享您在cURL params中指定的cookie?

0

確保您爲資源選擇正確的響應格式化程序和請求分析。您將在 http://example.com/admin/structure/services/list/Your_resource/server

<?php 
/** 
* Create a token for non-safe REST calls. 
**/ 
function mymodule_get_csrf_header() { 
    $curl_get = curl_init(); 
    curl_setopt_array($curl_get, array(
    CURLOPT_RETURNTRANSFER => 1, 
    CURLOPT_URL => 'http://example.com/services/session/token', 
)); 
    $csrf_token = curl_exec($curl_get); 
    curl_close($curl_get); 
    return 'X-CSRF-Token: ' . $csrf_token; 
} 


$service_url = 'http://example.com/rest/user/login'; 
$post_data = array(
    'username' => 'admin', 
    'password' => 'pass', 
); 
// We format post data as application/x-www-form-urlencoded so make 
// sure that you tick it under the rest server parser options. 
$post_data = http_build_query($post_data, '', '&'); 

// cURL 
$curl = curl_init($service_url); 
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json', mymodule_get_csrf_header())); 
// We want curl to return a string 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
// Choose method POST 
curl_setopt($curl, CURLOPT_POST, true); 
// Feed the data to POST to curl 
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data); 
// Make it verbose for debugging. 
curl_setopt($curl, CURLOPT_VERBOSE, true); 
// Go! 
$response = curl_exec($curl); 
$logged_user = json_decode($response); 
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); 
    if ($http_code == 200) { 
     $logged_user = json_decode($response); 
     echo 'logged'; 
    } 
    else { 
     $http_message = curl_error($curl); 
     die('Unable to connect to Basic CMS Engine! 
              Username or password incorrect! 
              Please enter valid username and password!'); 
    } 
    setcookie($logged_user->session_name,$logged_user->sessid,time() + 10000,'/'); 
    ob_end_flush(); 
?> 
+0

你可以使用var_dump($ logged_user)來檢查; – versha

0

這裏是使用bash外殼的例子,其從Drupal 7的檢索/admin/reports/status/php頁讀取地址IP:

#!/usr/bin/env bash 
url="https://www.example.com" 
uri_php="/admin/reports/status/php" 
user=admin 
pass=admin 
form_build_id=$(curl -s $url/user | grep -o 'form-[^" ]\{40,\}') 
cookie=$(curl -sX POST -d "name=$user&pass=$pass&form_id=user_login&op=Log+in&form_build_id=$form_build_id" $url -D- | grep -o "SESS[^;]\{60,\}") 
content=$(curl -s -H "Cookie: $cookie" ${url}${uri_php}) 
read key server_ip < <(grep -o "SERVER_ADDR[ <][^.]\+\.[^.]\+\.[^.]\+\.[^ <]\+" <<<$content | sed -e 's/<[^>]*>//g'); 
echo $server_ip