Closed (fixed)
Project:
Services
Version:
7.x-3.x-dev
Component:
Documentation
Priority:
Normal
Category:
Support request
Assigned:
Unassigned
Reporter:
Created:
2 Apr 2012 at 14:28 UTC
Updated:
9 Jan 2013 at 21:50 UTC
Hi there,
need some help with updating a user... I can't figure out, how to dial with the update array.. I have this.. this is not working (of cause Iogged in..) What I have to do, to update a user?
)
// -> login....
$base_url = 'http://xx/rest';
$headers = array(
'Cookie' => $data->session_name . '=' . $data->sessid
);
$account = array(
'data' => array(
'profile_ls_id' => array(array('value' => '111111')),
'name' => 'test_userd1xxxxxx12',
'pass' => 'xyzzy1',
'mail' => 'xxxxxxxxxxxx@gf.de'
)
);
update_user($base_url, $account, $headers);
function update_user($base_url, $account, $headers){
$query = http_build_query($account, '', '&');
$response = drupal_http_request(
$base_url.'/user/272', // uid of update user
$headers,
'PUT',
$query
);
}
Comments
Comment #1
Apfel007 commentedThis is my code for now - the update function send me an error "The name test_userd1xxxxxx12 is already taken." Is this a bug? Why the update function try to use insert a username?
Comment #2
ygerasimov commentedIndeed it looks strange that you can't update user. I would recommend to use curl instead of drupal_http_request. At least our tests use curl for building requests and they succeed with PUT type.
Please take a look at test that updates user: http://drupalcode.org/project/services.git/blob/refs/heads/6.x-3.x:/test...
Let me know if you still have any questions.
Comment #3
Apfel007 commentedHi ygerasimov,
thanks for that hint...
Could you explain me, whats the difference or advantage between curl and http-request..
Comment #4
ygerasimov commentedJust different type of connection. drupal_http_request uses sockets connection but curl is different. Anyhow I don't think this matters much for your use case. I am just not sure whether you get proper PUT request with sockets. With curl you will definitively would be able to do that and there are examples available.
Comment #5
Apfel007 commentedOK I started with Curl.. but need some more help - this should update a user, but it doesn't! I get not error message.. "mysqli_real_escape_string() expects parameter 2 ..." What wrong with this code snippet? I think the problem ist the $account-data. Is this array build right? It seems that the object returns "http_code: 406"
Comment #6
ygerasimov commentedPlease advise what you are trying to update? password?
First of all please enable debug mode on your rest server and check in logs what arguments you got passed.
Comment #7
Apfel007 commentedHi,
in the end I try to add something to a profile field, add a role and add user to a og-group (for that I need a custom action/recource..right?)
First I tried something easy - change the email-adress to understand how it works..
Passed arguments:
Array
(
[0] => 258
[1] => Array
(
)
)
The array is nearly empyt - it seems that'S someting wrong with my account-array..
Comment #8
Apfel007 commentedHi, I try to use this class to update a user profile field..
and call it with
Could someone give me a hint how to solve this? The update of the email-adress is working.. but not the field update. It seems something to do with the "update-array"
Thanks
Comment #9
Apfel007 commentednow the category is transmitted, but the profile field is not saved... if it has a value before this value is deleted... now
Comment #10
devkinetic commentedsubscribing. Currently I have not found a way to successfully update a user to add a role, change email, etc.
Comment #11
krem commentedHi there,
I am also having a hard time updating users details in Drupal 7... Any more help or guidance on the topic would be highly appreciated ;)
Comment #12
kylebrowning commentedHave you guys looked at the tests? They are a great resource to see how it works and to prove that it does work.
Comment #13
krem commentedI found my way around (Drupal 7), following is an example using Titanium appcelerator syntax but the code should easily be adaptable to other languages :
Using that code I am able to update an account details. I haven't done all tests to say which fields are mandatory but 'current_pass' was an important one to include in the call.
I hope it helps ;)
Clem
Comment #14
ygerasimov commentedClosing this issue. Please feel free to reopen if anyone still have problems with it.
Best source of all answers about how to update user are tests.
Comment #16
hgneng commentedI have a hard time with service API and updating user info. I would like to share with you.
First, we can add "&XDEBUG_SESSION_START=netbeans-xdebug" at the end of service endpoint URL to activate debug. We may need to take a look at sites/all/modules/services/resources/user_resource.inc to get the API of user.update. But that's only a clue. We won't know the first argument "kid" is passed through URL service-endpoint/user/$uid and the second argument is passed through PUT. And I tried to access service-enpoint/user/update for many times until I found there are operations and actions for service types.
I only knew GET and POST method before. In RESTServer.inc, we have following code:
And I know that I should use PUT instead of POST to invoke update method.
When using PUT, we don't specify data through CURLOPT_POSTFIELDS. Instead:
I got about info from tests code of services module after I understood what "tests" means in this thread.
When getting error of 401, 406 etc, we can search those numbers in RESTserver.inc and set breakpoint on them to see what's wrong.
Finally, here is the code I successfully run on Drupal 7:
Hope my post can save someone's time.
Cameron
Comment #17
tanzeel commentedAfter searching all around the web i just figured out the issue i was facing to send update profile request. To send update request you need to take care of following curl properties:
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-type: application/json"));
$serialize_args = json_encode($data);
curl_setopt($curl, CURLOPT_POSTFIELDS, $serialize_args);
Where $data is array of data to update.
I was missing CURLOPT_HTTPHEADER and not encoding the data in curl request.
Hope this can help someone.