≡ Menu

Getting Started with Drupal Services Module & REST Server via PHP

We recently migrated a client from SugarCRM to a lighter-weight, custom built, Drupal 7 CRM solution that more tightly met their needs. (The decision making process of moving away from SugarCRM as well as the migration/implmentation could be it’s own multi-part blog post).

Our client has several landing pages they use for lead generation and had previously used SugarCRM’s SOAP and REST APIs to capture this information directly in SugarCRM as a lead. Naturally they wanted similar functionality when moving to Drupal. We had always heard of the power of the Services module, but never had the chance to work with it in production. This was our chance!

The documentation for the services module was good, but didn’t really provide many code examples that could be used by someone looking for a quick start. I began poking around and quickly fell in love with the REST Server provided by the services module and decided it was the way to go.

With a bit of guidance from kylebrowning, I wrote a simple class to make our implementation a bit simpler by abstracting some of the details such as session tracking. I wrote DrupalREST.PHP and threw it on github.

After creating a content type in our Drupal installation with some CCK fields, we used the 19 lines of code below to create the node/lead.

$api = new DrupalRESTWebServices('http://url/to/your/endpoint', 'username', 'password', FALSE);
$node = array (
	'name' => 'creator',
	'status' => 1,
	'type' => 'content_type',
	'field_status' => array('und' =>array('key' => '1')),
	'field_first_name' => array('und' => array(array('value' => $form_values['submitted']['first_name']['#value']))),
	'field_last_name' => array('und' => array(array('value' => $form_values['submitted']['last_name']['#value']))),
	'field_phone_number' => array('und' => array(array('value' => $form_values['submitted']['phone']['#value']))),
	'field_address' => array('und' => array(array('value' => $form_values['submitted']['address']['#value']))),
	'field_city' => array('und' => array(array('value' => $form_values['submitted']['city']['#value']))),
	'field_state' => array('und' => array(array('value' => $form_values['submitted']['state']['#value']))),
	'field_zip' => array('und' => array(array('value' => $form_values['submitted']['zip']['#value']))),
	'field_email' => array('und' => array(array('value' => $form_values['submitted']['email']['#value']))),
	'field_lead_source' => array('und' => array(array('value' => $form_values['submitted']['source']['#value']))),
	'field_google_keyword' => array('und' => array(array('value' => $form_values['submitted']['keyword']['#value']))),
);
$api->login();
$api->createNode(array('node' => $node));

Let me know if this helped you get started with Drupal Services Module & the REST Server by leaving a comment!

NOTE: I ported this to C# as DrupalREST.NET.

Comments on this entry are closed.

  • corniquet July 25, 2011, 11:06 am

    Thank you for this job !

    I really appreciated the DrupalREST.NET version in C#.

    We have the project to update a drupal commerce site from a .NET desktop application and your job is a major step forward for us.

  • Gyorgy September 10, 2011, 11:30 am

    Hello,

    DrupalREST does not seem to be working for me because it does not do a system.connect call to get the seession id.
    system.connect is required even before login
    More info on system.connect http://drupal.org/node/129812

    DrupalREST is trying to get the session out of user.login call but to me it does not seem to be working.

    • Gyorgy September 10, 2011, 12:52 pm

      Actually, I take this back. It seems system.connect is not needed in D7. For some reason I am still not getting a response, and I believe it is because I have several response types enabled from json and xml, and the call needs to specify the format like /user/login.xml or user/login.json