Hi all,

I've set up a Services 6.x-3.3 access point at /api with all the 'user' type resources switched on. I've modified some CURL code I found on the web to submit a user login request through JSON

$request_url = 'http://<siteurl>/api/user/login';

$user_data = array(
  'username' => 'username',
  'password' => 'test'
);

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $request_url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($user_data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1);

$response = curl_exec($curl);

$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
print_r($http_code);
// Check if login was successful
if ($http_code == 200) {
  echo $response;
}
else {
  // Get error msg
  $http_message = curl_error($curl);
  die($http_message);
}

The login part works fine. But the $response returned by the server is not properly formatted JSON - it's a string with session and user information concatenated without any separation. Something like "f33bf8d92ec87aabb4f825fdd845e280SESS707500f7eec141cfda8fbc15a7a683be1use......" (double quotes not included). As a result I can't decode it into something more meaningful.

Am I missing something here?

Comments

kylebrowning’s picture

Status: Active » Closed (cannot reproduce)

Im not sure why youre getting that, but I cannot reproduce.

When I http POST d6.local.com/api/user/login username=admin password=admin

I get

{
    "sessid": "b9270c44703809f9a3ccedfd41727b75",
    "session_name": "SESSe084ef31b5ec643674b50ea0cddce1a7",
    "user": {
        "access": "1364316021",
        "created": "1349812230",
        "data": "a:0:{}",
        "init": "test@test.com",
        "language": "",
        "login": 1364316082,
        "mail": "test@test.com",
        "mode": "0",
        "name": "admin",
        "picture": "",
        "roles": {
            "2": "authenticated user"
        },
        "signature": "",
        "signature_format": "0",
        "sort": "0",
        "status": "1",
        "theme": "",
        "threshold": "0",
        "timezone": null,
        "uid": "1"
    }
}
kylebrowning’s picture

Maybe try setting the Accept header to be application/json

Content-Type only specifies what your sending.

jay_N’s picture

Status: Closed (cannot reproduce) » Closed (works as designed)

Kyle, thanks! The Accept header was exactly what was missing. I now get a nicely formatted JSON response.

For anyone else's benefit, here's my working code for the 'user.login' method in JSON format:


// REST Server URL
$request_url = 'http://supermegasite.com/api/user/login';

// User data
$user_data = array(
  'username' => 'Mybond.admin',
  'password' => 'admin'
);

// cURL
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $request_url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Accept: application/json'));
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($user_data)); // Set POST data in JSON format
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_FRESH_CONNECT, 1);

$response = curl_exec($curl);

$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);

// Check if login was successful
if ($http_code == 200) {
  // Convert json response into PHP array of $user object
  $output = json_decode($response);
  print_r($output);
}
else {
  // Get error msg
  $http_message = curl_error($curl);
  die($http_message);
}


kylebrowning’s picture

Might want to edit that to remove the credentials and real URL?

kylebrowning’s picture

nevermind, you did.

CARRY ON!