I need some help in sending data in an array (POST) to a webservice (REST). I need some examples in how to use the function drupal_http_request().

Now, I'm using cURL with PHP but I need to replace it with drupal_http_request(). And I see that it's easier to use in Drupal.

If my first page is the login (http://[ip]/users/sign_in) and I'm using "User:Pass" to enter to main page(http://[ip]/event). Then, I select the menu for view the data that I send from my form in Drupal (http://[ip]/event/mobile/management).

How to do this with drupal_http_request()?


function send_data_form_submit($form, &$form_state) {

    $name = $form_state['values']['name'];
    $email = $form_state['values']['email'];

    $attendee = array(
        'name' => urlencode($name),
        'email' => urlencode($email)

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

    $url_login = 'http://[ip]/eventmobile/users/sign_in';
    $url_target = 'http://[ip]/eventmobile/attendee_management/attendee';

    $use  = array(
      'email' => '[email protected]',
      'password' => '123456'
    $user = http_build_query($use, '', '&');

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

    $method = 'POST';

    $result_1 = drupal_http_request(url($url_login, array(absolute => true)), $headers, $method, $user); //I get a http_code = 200. OK
    $result_2 = drupal_http_request(url($url_target,array(absolute => true)), $headers, $method, $attendee); // I get a http_code = 500. Internal Server Error


UPDATE 2 (Using cURL):

$attendee = array(
        'name' => urlencode($name),
        'email' => urlencode($email)
$userpwd = "[email protected]:123456"
   $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url_target);
        curl_setopt($ch, CURLOPT_HEADER, TRUE);
        curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
        curl_setopt($ch, CURLOPT_TIMEOUT, 50);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        $result = curl_exec($ch);
        $getinfo = curl_getinfo($ch, CURLINFO_HTTP_CODE); //I get http_code=200

        if(preg_match('#Location: (.*)#', $result, $r))
                $url_redirect = trim($r[1]); //The URL of LOGIN

        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
        curl_setopt($ch, CURLOPT_URL, $url_redirect);
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
        curl_setopt($ch, CURLOPT_USERPWD, $userpwd);
        curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        $page = curl_exec($ch); // Return string: Evaluating...
        $getinfo = curl_getinfo($ch, CURLINFO_HTTP_CODE); //I get http_code=200

        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $attendee);
        curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_TIMEOUT, 50);
        $result = curl_exec($ch); // Return string: Evaluating...
        $getinfo = curl_getinfo($ch, CURLINFO_HTTP_CODE);// I get http_code=200

  • how does the $result_1 response look like? Feb 28, 2012 at 16:21
  • you missed an r $user = http_build_query($useR, '', '&'); Feb 28, 2012 at 16:23
  • @Marius Ilie $result_1 have a: "data = Evaluating..." "Protocol = HTTP/1.1" "status_message = OK" "code = 200"
    – Dvex
    Feb 28, 2012 at 16:26
  • the response seems fine. the second call worked ok with curl? because I never had any problems when switching from curl to drupal_http_request before... Feb 28, 2012 at 16:34
  • 1
    I think the problem is with the server, not the way you send data Feb 28, 2012 at 16:53

2 Answers 2


Your $url_login looks like the default URL scheme for a Ruby on Rails website that uses the devise login system. Here is a function I've used to login to a rails/devise backed web site.

/** Authenticate via Rails/Devise site */
function devise_login($email, $pass) {
   //Build up data to send to devise
   $data = array('user' => array('email' => $email, 'password'=>$pass));
   //Pull the url out of the drupal settings
   $url = variable_get('devise_authentication_url', '');
   if ($url != '') {
     $response = drupal_http_request($url, array(
       'method' => 'POST', 
       'data' => json_encode($data), 
       'max_redirects' => 0,
       'headers' => array('Content-Type' => 'application/json', 'Accept' => 'application/json'))

     return = json_decode($response->data); 

   return FALSE;
  • the variable ´$url´ is my ´$url_login´??
    – Dvex
    Feb 28, 2012 at 19:14

Have you tried this?

$headers =  = array();
$url = "the_link_to_post_to";

//now make the data string: it should be of type 'var_name=value&var_name2=value2&....' etc
$data = "username=user_name&password=chosen_password";

drupal_http_request($url, $headers, 'POST', $data);
  • Uuhhmm ok.. I update my post with the code that i'm doing now.. but, i have a error (http_code=500) when i send data by post METHOD.
    – Dvex
    Feb 28, 2012 at 16:10

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.