2

I want to add a new page node using drupal services and REST server.

$postdata = http_build_query(array(    
      'type' => 'page',
      'title' => 'aaa',
      'field_test[en][0][value]' => '<p>test</p>\n',
      'field_test[en][0][format]' => 'filtered_html'
      )
);

$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-Type: application/json'."\r\n".
                     'Cookie: ' . $sessid."\r\n",                   
        'content' => $postdata
    )
);

    $context = stream_context_create($opts);
    $contents = file_get_contents($main_url."node", false, $context);

    echo json_encode($contents);

When I run above code I get an error:

Warning: file_get_contents(http://192.168.1.129:80/test/node) 
[function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 
401 Unauthorized: 
Missing required argument node in D:\xampp\htdocs\serv\server.php on line 61
0

3 Answers 3

3

I think that you need make an authenticated request, unless you want any user to create nodes.
I have created nodes before using curl. You need enable the "Session authentication", activate the user login action in the resources section of you service, and enable request "application/x-www-form-urlencoded" in the server section.

This is the code

    $base_url = 'http://localhost/endpoint';
    $params = http_build_query(array(
       'username' => 'admin',
       'password' => 'qwerty',
    ));
    // set up the request
    $curl = curl_init($base_url . '/user/login.json');
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
    // make the request
    $response = curl_exec($curl);
    curl_close($curl);
    $user_data = json_decode($response);
    // login successful
    if (isset($user_data->sessid)) {
      $node_params = http_build_query(array(
        'type' => 'page',
        'title' => 'Test page',
      ));
      // set up the authenticated request
      $curl_node = curl_init($base_url . '/node.json');
      curl_setopt($curl_node, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($curl_node, CURLOPT_POST, true);
      curl_setopt($curl_node, CURLOPT_POSTFIELDS, $node_params);
      curl_setopt($curl_node, CURLOPT_COOKIE, $user_data->session_name . '=' . $user_data->sessid);
      // make the authenticated request
      $node_response = curl_exec($curl_node);
      curl_close($curl_node);
      var_dump(json_decode($node_response));
    }
1

SOLVED:

This line:

'content' => $postdata

I changed by:

'content' => $postdata
    'content' => 

    '&node[type]=page'.
    '&node[title]='.$_POST['title'].
    '&node[body][und][0][value]='.$_POST['body'].
    '&node[body][und][0][format]=filtered_html'

And now it works :)

0

This code is worked for me to create new node.

You need to enable "login" action under user section of resources.

<?php
  $username = "admin"; //site user
  $password = "admin123"; //site password
  $session;
  $endpoint = "http://localhost/sitename/endpoint/";
  $debug;

 $ch = curl_init($endpoint . 'user/login.json');
 $post_data = array(
  'username' => $username,
  'password' => $password,
 );
$post = http_build_query($post_data, '', '&');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER,array (
  "Accept: application/json",
  "Content-type: application/x-www-form-urlencoded"
));

$response = json_decode(curl_exec($ch));
//Save Session information to be sent as cookie with future calls
$session = $response->session_name . '=' . $response->sessid;
//GET CSRF Token
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => 1,
  CURLOPT_URL => 'http://localhost/sitename/services/session/token',
));
curl_setopt($ch, CURLOPT_COOKIE, "$session");

$response = curl_exec($ch);
$error    = curl_error($ch);
$info     = curl_getinfo($ch);

$csrf_token = $response;
$node = array('type'=>'documents','title'=>'tall123');
$post = http_build_query($node, '', '&');
$ch = curl_init($endpoint . 'node/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_HTTPHEADER,
array (
  "Accept: application/json",
  "Content-type: application/x-www-form-urlencoded",
  "Cookie: $session",
  'X-CSRF-Token: ' .$csrf_token
));

$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
print_r($response);

?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.