WebTechKitchen; Your Web Technology Kitchen, contact us to create, or maintain your websites and other digital properties.

Drupal 7 update a user using rest, curl and services module

Submitted by barnettech on Thu, 05/12/2011 - 12:00

Again there's a bug it seems that the first element of the array I pass in to the services module gets thrown away.


<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://drupal_base_url/user/login");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP script');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "name=username&pass=password&form_id=user_login");
ob_start();  //dont print out this output 
curl_exec ($ch);

curl_setopt($ch, CURLOPT_URL,"http://base_drupal_url/test_endpoint/users/4");
//curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json'));
//curl_setopt($ch, CURLOPT_HTTPGET, 1);
ob_end_clean();  //resume printout output to screen

/*$ret = new stdClass;
$ret->response = curl_exec($ch); // execute and get response
$ret->error    = curl_error($ch);
$ret->info     = curl_getinfo($ch);*/

/*if ($ret->info['http_code'] == 200) {
      $ret->response = json_decode($ret->response);
    }*/

//print_r($ret);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
//curl_setopt($ch, CURLOPT_FAILONERROR, true);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$resourceData = array(   'throwaway' => array('mail' => 'test'),   // this first element gets thrown away for some reason
                         'data' => array(                 
                                          'mail' => 'testmail@yahoo.com',                    
                                         ),
                     ); 
                     
$data2 = http_build_query($resourceData, '', '&');

curl_setopt($ch, CURLOPT_POSTFIELDS, "data=".$data2);

$ret = new stdClass;
$ret->response = curl_exec($ch); // execute and get response
$ret->error    = curl_error($ch);
$ret->info     = curl_getinfo($ch);

/*if ($ret->info['http_code'] == 200) {
      $ret->response = json_decode($ret->response);
    }*/
print $ret->response;
print $ret->error;



curl_close ($ch);
unset($ch);



?>