服务模块易于使用,但是配置起来可能很棘手,特别是如果您不熟悉此概念。因此,我将发布屏幕快照,以使“ Drupal Answers”用户可以轻松配置服务模块。
以下是我的计算机上安装的服务模块的版本:
创建一个名为“ rest”的端点,如下所示:
选择服务器的类型和端点路径:
选择要启用的资源列表并指定别名:
选择您要启用的响应格式化程序和请求解析器:
您可以如下所示测试配置:
您可以获取所有节点的列表,如下所示:
而具体的节点为:
以下是MichaelCole在http://drupal.org/node/910598#comment-4677738处提供的出色示例脚本,这些脚本可从任何外部PHP应用程序创建节点。
我正在复制他的代码以确保此答案的完整性。
//--------------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);