Skip to content

Instantly share code, notes, and snippets.

@cotto
Created October 31, 2011 18:20
Show Gist options
  • Save cotto/1328318 to your computer and use it in GitHub Desktop.
Save cotto/1328318 to your computer and use it in GitHub Desktop.
test script to create a user through Drupal's user resource
<?php
$base_url = 'http://YOUR.SITE.HERE/api';
// Login
$login_data = array(
'username' => 'YOUR_USER',
'password' => 'YOUR_PASSWORD',
);
$req = new HttpRequest("$base_url/user/login.json", HttpRequest::METH_POST);
#$req->setRawPostData(json_encode($login_data));
$req->setRawPostData(json_encode($login_data));
$req->setContentType('application/json');
$req->send();
if ($req->getResponseCode() != 200) {
echo "login failed\n";
print_r($req);
exit;
}
$response = $req->getResponseBody();
$response = json_decode($response);
$session_cookie = array($response->session_name => $response->sessid);
$user = array(
'name' => 'quux' . microtime(1),
'mail'=> 'quux'.microtime(1).'@example.com',
'pass'=>'quuxpass',
);
$json_user = json_encode($user);
$req = new HttpRequest("$base_url/user.json", HttpRequest::METH_POST);
$req->addCookies($session_cookie);
$req->setRawPostData($json_user);
$req->setContentType('application/json');
$req->send();
if ($req->getResponseCode() != 200) {
echo "create failed:\n";
print_r($req);
echo "\n";
}
print_r($req);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment