2

我正在尝试通过 curl 登录 drupal。我收到消息:'logged' 使用命令:echo "logged"; 这是在一个 if 语句中,它告诉我一切都很好。

运行脚本后,我打开主页,不幸的是我没有登录。

我认为我的 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
    ?>

欢迎各种帮助。提前致谢。

4

3 回答 3

1

首先,代码 200 不一定意味着您真的登录了。响应代码 200 意味着网络服务器告诉您您的请求成功,但网络服务器不知道您是否登录到 drupal。

第二件事是I open my homepage and unfortunately I wasn't logged in。你的意思是你打开浏览器?您的浏览器是否共享您在 cURL 参数中指定的 cookie?

于 2013-11-13T06:33:31.503 回答
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();
?>
于 2013-11-13T06:18:23.967 回答
0

这是使用 Bash shell 的示例,它从/admin/reports/status/php从 Drupal 7 检索的页面中读取地址 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
于 2017-08-18T13:51:57.083 回答