如何登录Drupal并从远程网站获取当前会话ID?

时间:2012-04-17 05:05:06

标签: drupal

我正在尝试使用drupal_http_request方法从远程站点登录Drupal。我需要从Drupal获取会话ID。当我运行代码时,我得到如下响应。但是,如果我输入错误的用户凭据,那么它也会给我相同的响应。我正在使用Drupal 6和服务3.请帮助

<?php
// This is the base URL for our installation
require_once 'drupal/includes/bootstrap.inc';
include('drupal/includes/common.inc');
$base_url = 'http://localhost/drupal/';

// necessary or the response is empty:
$headers = array('Content-Type' => 'application/x-www-form-urlencoded');
// Login
$data = array(
  'name' => 'admin',
  'pass' => 'password',
);
$data = http_build_query($data, '', '&');
$response = drupal_http_request($base_url . 'user/login', $headers, 'POST', $data);
$data = json_decode($response->data);

print_r($response);
// uncommented and fixed cookie line so that the second request is using the same session
echo $headers['Cookie'] = $data->session_name . '=' . $data->sessid;
$response = drupal_http_request($base_url . 'user/3', $headers);
print_r(json_decode($response->data));

&GT;

输出 [protocol] =&gt; HTTP / 1.1 [status_message] =&gt; OK [headers] =&gt;数组([日期] =&gt;星期二,2012年4月17日04:40:48 GMT [服务器] =&gt; Apache / 2.2.21(Win32)PHP / 5.3.10 [X-Powered-By] =&gt; PHP / 5.3.10 [Set-Cookie] =&gt; SESScfc90a62c81b7bfc6f292320b1d0b8ca = 89q5rhq3neom3le1l4b2l67297; expires = Thu,10-May-2012 08:14:08 GMT; path = / [Expires] =&gt; Sun,19 Nov 1978 05:00:00 GMT [Last-Modified] =&gt;星期二,2012年4月17日04:40:48 GMT [Cache-Control] =&gt; post-check = 0,pre-check = 0 [Content-Length] =&gt; 4485 [连接] =&gt;关闭[Content-Type] =&gt; text / html; charset = utf-8)[code] =&gt; 200)

0 个答案:

没有答案