0

我在 1 个 drupal 站点上有一段代码,用于在多站点设置中创建另一个 drupal 站点的节点。看起来我得到了 sessionid 并且登录得很好,但是在尝试创建“页面”节点时,我得到“访问被拒绝”。在服务-> 设置下,我有“密钥验证”,“使用密钥”未选中,“使用 sessid”已选中。我为登录用户提供了agev 权限:“创建页面内容”、“管理服务”等......下面是我的代码:

<p>Test Page 1</p>
<? $url = 'http://drupal2.dev/xmlrpc.php'; ?>
<?
$conn = xmlrpc($url, 'system.connect');
print_r($conn);
?>
<p>--</p>
<?
$login = xmlrpc($url, 'user.login', $conn['sessid'], 'superuser_name', 'superuser_password');
print_r($login);
?>
<p>--</p>
<?
$data=array('type'=>'page', 'title'=>'Test', 'body'=>'test');
$data_s=serialize($data);
$result = xmlrpc($url, 'node.save', $login['sessid'], $data_s);
echo $result;

//echo $data_s;

?>
<?
if($error = xmlrpc_error()){
    if($error->code > 0){
        $error->message = t('Outgoing HTTP request failed because the socket could not be opened.');
    }

    drupal_set_message(t('Operation failed because the remote site gave an error: %message (@code).',
            array(
                '%message' => $error->message,
                '@code' => $error->code
            )
        )
    );

}
?>

这个脚本的输出是:


Array ( [sessid] => 9eebdde9bf0bfd9610cc2f03af131a9c [user] => Array ( [uid] => 0 [hostname] => ::1 [roles] => Array ( [1] => anonymous user ) [session] => [cache] => 0 ) )

--

Array ( [sessid] => c0ca4c599e41e97e7a7ceb43ee43249e [user] => Array ( [uid] => 1 [name] => eric [pass] => 13583b155536098b98df41bb69fcc53 [mail] => email@gmail.com [mode] => 0 [sort] => 0 [threshold] => 0 [theme] => [signature] => [signature_format] => 0 [created] => 1271813934 [access] => 1275867734 [login] => 1275868794 [status] => 1 [timezone] => [language] => [picture] => [init] => email@gmail.com [data] => a:0:{} [roles] => Array ( [2] => authenticated user ) ) )

--

拒绝访问

4

2 回答 2

1

我最近发现 PHP 会话 ID 比我想象的要复杂。

为了让它们工作,您的 XMLRPC 传输需要完全支持用于 Drupal 身份验证的 cookie。

如果没有 cookie,每个请求都被视为一个新的匿名请求,并被赋予一个新的会话 ID。因此,您已登录的事实对您进行的下一个 xmlrpc 调用没有任何意义。

我正在用 python 做一些工作,并制作了一个自定义传输对象来支持 cookie,现在这一切都对我有用。我在这里找到了如何在 python 中执行此操作:

http://osdir.com/ml/python.cherrypy/2005-12/msg00142.html

(编辑添加)我还可以补充一点,服务模块的错误报告非常糟糕。例如,如果您将参数作为字符串发送,而它需要一个数组(该字符串在数组中),您通常会收到拒绝访问错误,这并不能真正反映存在参数错误的事实。

通过在“管理”>“站点构建”>“服务”>“浏览”下进行测试,检查该服务是否按预期工作,然后单击您要使用的服务。

于 2010-07-12T03:40:13.800 回答
-1

site 1 code:

function exmple2_cron() {  
  homes_sync_get_node_list();
}

function homes_sync_get_node_list() { 

  $methods = xmlrpc('http://example.com/map/xmlrpc.php', array('system.listMethods' => array()));
  $node_ids = xmlrpc('http://example.com/map/xmlrpc.php', array('node.getAllHomes'=>array()));

   if (xmlrpc_error()) {  
    $error = xmlrpc_error();

      watchdog('homes_sync', 'Error getting node list from parent server. Error: @error.', array('@error' => $error);
  } 
  else {
    foreach ($node_ids as $nid) {
      $nodes[] = $nid;
    }
  variable_set('parent_home_nodes', $nodes);
  watchdog('homes_sync', 'Successfully retrieved node list from parent server.', array(), WATCHDOG_NOTICE);
  }

  homes_sync_perform_update();
}

function homes_sync_perform_update() {
  $node_ids = variable_get('parent_home_nodes', 0);
  foreach ($node_ids as $nid) {
    $data = xmlrpc('http://example.com/map/xmlrpc.php', array('node.get' => array($nid)));print_r($data);exit;
    $result = db_fetch_array(db_query('SELECT n.nid, n.title, n.type FROM {node} n WHERE n.title = "%s" AND n.type = "%s"', $data['title'], 'page'));
    if (xmlrpc_error()) {
      $error = xmlrpc_error();
      watchdog('homes_sync', 'Could not perform XMLRPC request. Error: @error.', array('@error' => $error), WATCHDOG_CRITICAL);
    } else {
      if (is_array($data)) {
        $node = "";
        if ($result && $result['nid']) {
          $node->nid = $result['nid'];
        }

    $node->type = $data['type'];
    $node->uid = 1;
    $node->status = $data['status'];
    $node->created = $data['created'];
    $node->changed = $data['changed'];
    $node->comment = $data['comment'];
    $node->promote = $data['promote'];
    $node->moderate = $data['moderate'];
    $node->sticky = $data['sticky'];
    $node->tnid = $data['tnid'];
    $node->translate = $data['translate'];
    $node->title = $data['title'];
    $node->body = $data['body'];
    $node->teaser = $data['teaser'];
    $node->format = $data['format'];
    $node->name = $data['name'];
    $node->data = $data['data'];
    $node->path = $data['path'];

    node_save($node);
       unset($node);
      }
    }
  }
}

remote site code:

function example_xmlrpc() {
$methods = array();
$methods[] =  array(  
    'node.getAllHomes',
    'homes_service_node_get_all_homes', 
     array('int'),
  );  
return $methods;  
}

function homes_service_node_get_all_homes() {
  $query = db_query('SELECT n.* FROM {node} n');
  foreach ($query as $record){ 
    $nid[] = $record;
  }
  return $nid;
}
于 2013-08-02T10:39:00.197 回答