Sample code provided there is flawed as there is no need to get CSRF token two times. One time is enough after user login. Also, CSRF token needs to be obtained in the same curl session as login, as Drupal Services returns empty otherwise, try the following code once:
<?php
// LOGIN AND GET CSRF TOKEN
/*
* Server REST - user.login
*/
// REST Server URL
$request_url = 'http://mysite....com/server/user/login.json';
// User data
$user_data = array(
'username' => 'xxxx',
'password' => 'xxxx',
);
$user_data = http_build_query($user_data);
// cURL
$curl = curl_init($request_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json')); // Accept JSON response
curl_setopt($curl, CURLOPT_POST, 1); // Do a regular HTTP POST
curl_setopt($curl, CURLOPT_POSTFIELDS, $user_data); // Set POST data
curl_setopt($curl, CURLOPT_HEADER, FALSE); // Ask to not return Header
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_FAILONERROR, TRUE);
$response = curl_exec($curl);
//print_r($response);
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
//print "<br>".$http_code."</br>";
// Check if login was successful
if ($http_code == 200) {
// Convert json response as array
$logged_user = json_decode($response);
//print_r($logged_user);
}
else {
// Get error msg
$http_message = curl_error($curl);
die($http_message);
}
// Define cookie session
$cookie_session = $logged_user->session_name . '=' . $logged_user->sessid;
//GET CSRF TOKEN
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'http://mysite....com/services/session/token',
));
curl_setopt($curl, CURLOPT_COOKIE, "$cookie_session");
// $csrf_token = curl_exec($curl);
//print_r($csrf_token);
$ret = new stdClass;
$ret->response = curl_exec($curl);
$ret->error = curl_error($curl);
$ret->info = curl_getinfo($curl);
//print_r($ret->response);
$csrf_token = json_decode($ret->response)->token;
/*
* Server REST - node.create
*/
// REST Server URL
$request_url = 'http://mysite....com/server/node';
// Node data
$node_data = array(
'title' => 'A node created with services 3.x and REST server',
'type' => 'page',
'body[und][0][value]' => '<p>Body</p>',
);
$node_data = http_build_query($node_data);
// cURL
$curl = curl_init($request_url);
// Accept JSON response
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json', 'X-CSRF-Token: ' .$csrf_token));
curl_setopt($curl, CURLOPT_POST, 1); // Do a regular HTTP POST
curl_setopt($curl, CURLOPT_POSTFIELDS, $node_data); // Set POST data
curl_setopt($curl, CURLOPT_HEADER, FALSE);
// use the previously saved session
curl_setopt($curl, CURLOPT_COOKIE, "$cookie_session");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_FAILONERROR, TRUE);
$response = curl_exec($curl);
print "<br><br>";
print_r($response);
$http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
// Check if login was successful
if ($http_code == 200) {
// Convert json response as array
$node = json_decode($response);
}
else {
// Get error msg
$http_message = curl_error($curl);
die($http_message);
}
print_r($node);
?>