drupal使用node.save和XMLRPC调用另一个站点。 “拒绝访问”消息

时间:2010-06-06 23:48:11

标签: drupal xml-rpc access-denied drupal-nodes drupal-services

我在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 ) ) )

-

拒绝访问

2 个答案:

答案 0 :(得分:1)

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

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

如果没有Cookie,则会将每个请求视为新的匿名请求,并为其提供新的会话ID。因此,您已登录的事实对您进行的下一个xmlrpc调用没有任何意义。

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

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

(edit-add)我可能还会补充说,服务模块的错误报告非常糟糕。例如,如果您在期望一个数组(在数组中包含字符串)时将参数作为字符串发送,您通常可以获得拒绝访问的错误,这些错误并不能真正反映出存在参数错误的事实。

通过在Admin&gt;下测试服务,检查服务是否按预期运行网站建设&gt;服务&gt;浏览并单击要使用的服务。

答案 1 :(得分:-1)

网站1代码:

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);
      }
    }
  }
}

远程站点代码:

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;
}