如何从非drupal站点使用drupal服务node.save?

时间:2011-07-27 12:31:08

标签: php drupal drupal-6 xml-rpc

我正在尝试从一个非drupal站点在drupal站点中创建一个节点,但我不能让我的生活让它工作。

这是我用google搜索构建的代码......

$domain = 'http://mydomain.com';
$operation = 'node.save';
$api_key = 'my api key';
$timestamp = (string) time();
$nonce = substr(md5(uniqid('s', true)), 0, 10);
$hash = hash_hmac('sha256', $timestamp .';'.$domain .';'. $nonce .';'.$operation, $api_key);

$node_data = array(
'type'        => 'story',
'title'       => 'test story title',
'body'        => 'test story text'
);

$send_data = array(
  'method' => $operation,
  'args' => array(
      'hash'              => $hash,
      'domain_name'       => $domain,
      'domain_time_stamp' => $timestamp,
      'nonce'             => $nonce,
      'node'              => serialize($node_data)
  )
);

$request = xmlrpc_encode_request($operation, $send_data);

$context = stream_context_create(
  array(
    'http' => array(
      'method' => "POST",
      'header' => "Content-Type: text/xml",
      'content' => $request
    )
  )
);

$retrieve = file_get_contents($domain .'/services/xmlrpc', false, $context);
$response = xmlrpc_decode($retrieve);

echo '<pre>'. htmlspecialchars(print_r($response, 1)) .'</pre>';

但是我从服务器回来的所有内容是:“缺少必需的参数:节点”,而且我该死的通过了....

我可以得到一个“system.connect”响应,这让我感觉很好,但这就是我能得到的。我的服务设置不需要session_id。

帮我stackoverflow ...你是我唯一的希望

2 个答案:

答案 0 :(得分:2)

如果您使用的是服务7.x-3.0-rc1或更高版本,您还可以使用MichaelCole http://drupal.org/node/910598#comment-4677738提供的优秀示例脚本{{3}}

我正在为StackOverflow用户再次记录他的代码。

 //--------------login to the server------------------------
$service_url = 'http://example.dev/rest/user/login.xml'; // .xml asks for xml data in response
$post_data = array(
    'username' => 'test',
    'password' => 'test',
);
$post_data = http_build_query($post_data, '', '&'); // Format post data as application/x-www-form-urlencoded
// set up the request
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);  // have curl_exec return a string

curl_setopt($curl, CURLOPT_POST, true);             // do a POST
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data); // POST this data
// make the request
curl_setopt($curl, CURLOPT_VERBOSE, true); // output to command line
$response = curl_exec($curl);
curl_close($curl);
print "LOGIN RESPONSE:\n";
var_dump($response);



// parse the response
$xml = new SimpleXMLElement($response);
$session_cookie = $xml->session_name . '=' . $xml->sessid;
// print "SESSION_COOKIE: $session_cookie";

file_put_contents('session_cookie.txt', $session_cookie);

//----------------create a node -------------------------------

$node_data = array(
    'type' => 'ct_metadata_core',
    'title' => 'test layer',
    'field_core_lat_n[und][0]' => array('value' => '90'),
    'field_core_lat_s[und][0]' => array('value' => '-90'),
    'field_core_long_e[und][0]' => array('value' => '180'),
    'field_core_long_w[und][0]' => array('value' => '-180'),
    'field_core_description[und][0]' => array('value' => 'National Data Buoy Center'),
    'field_core_originator[und][0]' => array('value' => 'NDBC'),
    'field_core_url[und][0]' => array('url' => 'http://www.ndbc.noaa.gov/kml/marineobs_as_kml.php?sort=pgm'),
    'field_cont_res_name_org[und][0]' => array('value' => 'test'),

);


$service_url = 'http://example.dev/rest/node'; // .xml asks for xml data in response
$session_cookie = file_get_contents('session_cookie.txt');

$node_data = http_build_query($node_data, '', '&'); // Format post data as application/x-www-form-urlencoded
// set up the request
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);  // have curl_exec return a string

curl_setopt($curl, CURLOPT_COOKIE, "$session_cookie"); // use the previously saved session

curl_setopt($curl, CURLOPT_POST, true);             // do a POST
curl_setopt($curl, CURLOPT_POSTFIELDS, $node_data); // POST this data
// make the request
curl_setopt($curl, CURLOPT_VERBOSE, true); // output to command line
$response = curl_exec($curl);
curl_close($curl);
print "CREATE NODE RESPONSE:\n";
var_dump($response);


//----------------logout from the server-------------------------

$service_url = 'http://example.dev/rest/user/logout';
$session_cookie = file_get_contents('session_cookie.txt');

// set up the request
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);  // have curl_exec return a string

curl_setopt($curl, CURLOPT_COOKIE, "$session_cookie"); // use the previously saved session
curl_setopt($curl, CURLOPT_POST, true);             // do a POST
curl_setopt($curl, CURLOPT_POSTFIELDS, ""); // POST this data
// make the request
curl_setopt($curl, CURLOPT_VERBOSE, true); // output to command line
$response = curl_exec($curl);
curl_close($curl);
print "LOGOUT RESPONSE:\n";
var_dump($response);

答案 1 :(得分:0)

您似乎需要使用sessionid。点击'system.connect'方法返回会话ID,然后将其作为参数传递给node.save