1

我正在尝试使用 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));

?>

输出 [protocol] => HTTP/1.1 [status_message] => OK [headers] => Array ([Date] => Tue, 17 Apr 2012 04:40:48 GMT [Server] => Apache/2.2.21 (Win32 ) PHP/5.3.10 [X-Powered-By] => PHP/5.3.10 [Set-Cookie] => SESScfc90a62c81b7bfc6f292320b1d0b8ca=89q5rhq3neom3le1l4b2l67297; expires=Thu, 10-May-2012 08:14:08 GMT; path=/ [过期] => 1978 年 11 月 19 日星期日 05:00:00 GMT [最后修改] => 2012 年 4 月 17 日星期二 04:40:48 GMT [缓存控制] => 后检查=0,预检查=0 [Content-Length] => 4485 [Connection] => close [Content-Type] => text/html; charset=utf-8) [code] => 200)

4

0 回答 0