29

I am new to drupal and I am using services module for using REST and RESTWS module. I used RESTWS and could get the content of the node by http://base_url/node/1.xml and now I need to add a node and users from my external PHP application in to drupal. I googled and I found here that we need to use http://base_drupal_url/drupal7/test_endpoint/users . I tried to create a service from Drupal 7, but I dont know what should I give on endpoint title ,name and endpoint path and I assume I need to give the same endpoint path in the curl.

Any help as to how to check if rest server is installed and how to create endpoint path would be of great help.

I am using Drupal 7 with services module services-7.x-3.0-rc3

  • Did you solve your problem? I have created an endpoint and resource path exactly as you but I am getting "Not Found The requested URL /~DrupalWorkstation/Drupal/drupal_7_16/rest/node was not found on this server." error. Please check my comment to the highest voted answer. Let me know if you have a solution for this. – Raj Pawan Gumdal Oct 30 '12 at 18:41
56

Services module is easy to use, but it can be tricky to configure especially if you are new to the concept. So, I am going to post the screenshots to make configuration of Services modules easy for "Drupal Answers" users.

Following is the version of Services module installed on my machine:

enter image description here

Create an endpoint called 'rest' as shown below:

enter image description here

Select the type of server and endpoint path:

enter image description here

Select the list of resources that you want to enable and specify aliases:

enter image description here

Select the response formatters and request parsers that you want to enable:

enter image description here

You can test your configuration as shown below:

enter image description here

You can get the list of all nodes as below:

enter image description here

And specific node as:

enter image description here

Following are the excellent examples scripts provided by MichaelCole here http://drupal.org/node/910598#comment-4677738 to create nodes from any external PHP application.

I am duplicating his code for completeness of this answer.

//--------------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);
  • 2
    So worth pointing out that he enabled every request parsing option. I wasted a lot of time before I realized that "application/x-www-form-urlencoded" is disabled by default. – drewish Apr 24 '12 at 20:28
  • where do I have to write the above code snippet? Is it inside modules/services/server/rest_server/lib? – subhojit777 May 22 '12 at 4:36
  • 1
    @subhojit777 above code is a stand alone PHP code snippet that can be used to consume services api hosted on an external server, and you can write it in you module file, but writing it in modules/services/server/rest_server/lib is probably not the best place. – Ajinkya Kulkarni May 24 '12 at 14:20
  • I have setup everything as you said on my localhost. I have a path to my drupal site running as: localhost/~DrupalWorkstation/Drupal/drupal_7_16. The index.php page is running when I hit above URL, but my configured endpoint and resource path would not work. My URL to trigger the service is: localhost/~DrupalWorkstation/Drupal/drupal_7_16/rest/node. I get the following error: "Not Found The requested URL /~DrupalWorkstation/Drupal/drupal_7_16/rest/node was not found on this server." Any suggestions on how to fix this problem? – Raj Pawan Gumdal Oct 30 '12 at 18:37
  • 1
    Arigato gozaimasu Ajinkya sensei. ARIGATO!!! – TheEYL Feb 14 '15 at 4:49
6

I suggest you read Creating a resource for Services 3.x.

Also, I'm note sure if Services and RESTWS are compatible; they both offer variations of the same thing, and as such may conflict.

  • I would like to know what should i give in the endpoint path in the service add option in drupal 7 – sridhar Jun 7 '11 at 5:57
  • and should i write any custom code in drupal to create a node from my php application? – sridhar Jun 7 '11 at 6:00
  • Services has built in resources that can be used. regarding RestWS and compatability: There's a port of RestWS to Services that was rejected by the RestWS maintainers. If anyone is interested it could be published as a separate module though. – VoxPelli Jul 5 '11 at 21:48

protected by Community May 9 '12 at 16:15

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.