简体   繁体   English

Drupal卷曲登录

[英]Drupal log in with a curl

I'm trying to log in a drupal via curl. 我正在尝试通过curl登录drupal。 I got message: 'logged' with a command: echo "logged"; 我收到一条消息:已记录“ logged”:echo“ logged”; which is in a if statement that tells me how everything was fine. if语句中的内容告诉我一切都很好。

After running a script I open my homepage and unfortunately I wasn't logged in. 运行脚本后,我打开主页,但是很遗憾,我没有登录。

I think that I have a problems with a cookies. 我认为我的Cookie有问题。

 <?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
    ?>

Every kind of help is welcome. 欢迎各种帮助。 Thanks in advance. 提前致谢。

Firstly, code 200 mustn't mean that you were really logged in. Response code 200 means that the webserver is telling you that your request succeeded but webserver has no idea whether you logged in to drupal or not. 首先,代码200不一定表示您已经真正登录。响应代码200表示Web服务器正在告诉您您的请求成功,但是Web服务器不知道您是否登录了Drupal。

Second thing is I open my homepage and unfortunately I wasn't logged in . 第二件事是I open my homepage and unfortunately I wasn't logged in You mean that you open your browser? 您的意思是打开浏览器? Does your browser share cookies that you specified in cURL params? 浏览器是否共享您在cURL参数中指定的cookie?

Make sure you choose correct Response formatters and Request Parsing for your resource. 确保为资源选择正确的响应格式器和请求解析。 You will check it at http://example.com/admin/structure/services/list/Your_resource/server 您将在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();
?>

Here is the example using Bash shell which reads address IP from /admin/reports/status/php page retrieved from Drupal 7: 以下是使用Bash shell的示例,该示例从从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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM