Last updated September 26, 2012. Created by jimthunderbird on March 21, 2012.
Edited by jackbravo. Log in to edit this page.
Assume that I created an rest server endpoint:
http://localhost:3000/rest_api/user/login and I have an account with username: jimthunderbird and password 123456, here is how to log the user in using curl and the services module in Drupal 7
<?php
// REST Server URL
$request_url = 'http://localhost:3000/rest_api/user/login';
// User data
$user_data = array(
'username' => 'jimthunderbird',
'password' => '123456',
);
// cURL
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $request_url);
curl_setopt($curl, CURLOPT_POST, 1); // Do a regular HTTP POST
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($user_data)); // Set POST data
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($curl);
print $response;
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
// Check if login was successful
if ($http_code == 200) {
// Convert json response as array
$logged_user = json_decode($response);
}
else {
// Get error msg
$http_message = curl_error($curl);
die($http_message);
}
print_r($logged_user);
?>
Comments
For newbies
For this example endpoint set in "Path to endpoint field" is "rest_api" rest of the end point came from resource path that is available in this service (set in service on "resource" tab) in this case user > login.
Pawel