Is there an existing module that sends data from any form from my Drupal site to a web service?


Ok, I decided to continue my project using cURL. I created my form that contains 2 fields: "Name" and "Phone" and a submit button. (All in my own module)

Then I show my code that I just did. However, when I see the web service. The shipping data, not shown. I did a debug and uh I realized that when running: "curl_exec ($ch)" returns me FALSE.

function mymodule_form_submit($form, &$form_state){    
    $name = $form_state['values']['name'];
    $phone = $form_state['values']['phone'];

    $att = array(
        'name' => $name,
        'phone' => $phone

    $attendee = http_build_query($att, '', '&');

    $url = 'http://[IP]/event/management/attendee';

    $headers = array('Content-Type: application/x-www-form-urlencoded');

    $userpasswd = 'user:pass';

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_USERPWD, $userpasswd);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $attendee);
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE);
    curl_setopt($ch, CURLOPT_FAILONERROR, FALSE);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    $result = curl_exec($ch);

The params,in the web service, are: attendee['name'], attendee['phone']. What is my error?

UPDATE 2: NOW with this code, the variable $result return me a bool(TRUE). That tell me that the data was pass to the webservice. But, when I see the web service, I see no records with the data that I supposedly sent.

  • Here are a few first steps to debugging this: Check if the data you are sending are valid/not null. Also try the same request from the command line to make sure it works and let us know.
    – Kostas
    Feb 24, 2012 at 8:52
  • @Kostas The data from my form is valid ($name, $phone). Have a value and put this values in my array. When execute a function: curl_exec($ch) return me a false. Feb 24, 2012 at 14:30
  • Did you try the 2nd point? Send the request with curl from the bash (not sure about Windows, but there must be a program or sth).
    – Kostas
    Feb 26, 2012 at 19:14

3 Answers 3


Well, it probably depends on the type of web service you want to send data to, but there is this module, REST API Query, for REST web services (http://drupal.org/project/rest_api_query).

This week we needed to send an issue report form data from a Drupal site to a Redmine issue tracker, using the Redmine web API. We played around with the above module, but finally decided to do it in plain PHP, using CURL.

Here's the code we are using (inside the custom Drupal module we made), so you can get a general idea. You can easily adapt to your specific needs.

$url = {The API url here};
$userpasswd = {api-user}:{api-pass};
$xml = {XML payload};

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_USERPWD, $userpasswd);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
curl_setopt($ch, CURLOPT_FAILONERROR,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$result = curl_exec($ch);

UPDATE: There is a drupal core function that performs the same task, drupal_http_request. I guess it would be better to use that one, doing it the drupal way and all :-) I have not yet tried it out though. http://api.drupal.org/api/drupal/includes!common.inc/function/drupal_http_request

  • is for rest web service... but.. this code is for send the data from my form in format XML? I don't understand.. I need to send the data to the webservice client.. and this web service client with the data that i sent.. return me a json file with the same data. Feb 21, 2012 at 19:13
  • Well, I was using a webservice that expected XML. If you need to send/receive JSON, you could change th content type and payload. REST does not refer to the format of data exchanged (en.wikipedia.org/wiki/Representational_state_transfer). Check the documentation of the webservice you need to use or send me the link and I might be able to help you out.
    – Kostas
    Feb 22, 2012 at 8:10
  • Well, for the json isn't important now. But, my question is: In this code, where i see o where i send the data of my form in drupal?.. For example: 'first_data' = $form_values[submit][first_data]; And this 'first_data' send to webservice.. Feb 22, 2012 at 14:45
  • In this example, it is the $xml variable. It is assigned a value in the 3rd line and it is passed as a parameter in cURL in the 5th line of curl_setopt's. It is sent as the "payload" of POST. POST and UPDATE requests in HTTP can have a payload, basically data that is sent alongside the request to the server. In this case data sent to the webservice. In the line just above, you could change the content-type to "application/json" or whatever you will be using.
    – Kostas
    Feb 23, 2012 at 7:41
  • Sorry to bother you but what you could see what I have done? thanks Feb 23, 2012 at 20:19

I think your best bet would be to create a mini module yourself that will hook on the form submit event and gather all the data from your form and send them over to the other side. I have done something similar by getting some data out of the db, encoding them onto a url and posting it to the remote server.

Actually consider storing the info before sending it over, as services and connections are not always available and you might need to retry in a few minutes or the next cron.

function mymodule_form_alter(&$form, $form_state, $form_id) { 
   if (isset($form['#node']) && $form['#node']->type .'_node_form' == $form_id) {
        $form['buttons']['submit']['#submit'][] = 'my_custom_function'; 

Then you create a custom function:

function mymodule_custom_function(){
    //if that fails add the failed data on the DB to retry later.

function mymodule_cron(){
     //retry failed attempts

Or something like that.

  • If you are building this in a larger environment, you should consider developing this entirely asynchronous: Place the data in a queue/spool and have cron pick up items from that queue and send them along. E.g. Twitter encourages this design by making their API services (deliberately?) potentially really slow in response. You could even consider a working-queue service such as resque github.com/defunkt/resque.
    – berkes
    Feb 9, 2012 at 9:38
  • @mxa055 Do you have a example? or What function would you recommend or use hooks to do this? Feb 21, 2012 at 19:14
  • I have added an example on the original answer.
    – masimplo
    Feb 23, 2012 at 14:14

Check out the Services module. You might have to write a bit of a plug-in to get it from your form (Webform?) but Services provides the framework.


A standardized solution of integrating external applications with Drupal. Service callbacks may be used with multiple interfaces like REST, XMLRPC, JSON, JSON-RPC, SOAP, AMF, etc. This allows a Drupal site to provide web services via multiple interfaces while using the same callback code.

  • uuhmm.. its a form ordinary (with fields cck for example or a form with label's, field's and a button created by me).. Only i need the web service receives data of my form. Feb 7, 2012 at 19:38
  • 1
    OP needs the other way around, where Drupal is the initiator. Services.module works for situations where external is the initiator.
    – berkes
    Feb 9, 2012 at 9:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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