Hi,

I have a problem on adding/updating a node in drupal. I am using a php script to connect to Drupal json services and I am getting the data from it, in a json format. I have seen examples around on how to add a node but they are using xmlrpc, what I want is json. I tried, but it did not work for me.. I wonder what's wrong. I hope someone might help me to solve this.

Here is my sample code.

require_once("BondClass.php");

  // SYSTEM CONNECT
  $api_key = '99999your9999api999keyd4eae7c3';
  $sessid = '9999your9999sess9999id';
  $systemconnect = curl_init();
  curl_setopt($systemconnect, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($systemconnect, CURLOPT_POST, 1);
  curl_setopt($systemconnect, CURLOPT_URL, 'http://www.phworkforce.com/pms/?q=services/json');

  //prepare the field values being posted to the service
  $systemconnectdata = array('method' => 'system.connect', 'api_key' => $api_key, 'sessid' => $sessid, 'arg1' => 'some value', 'arg2' => 'somevalue', 'argN'=>'another value');
  curl_setopt($systemconnect, CURLOPT_POSTFIELDS, $systemconnectdata);

  //make the request
  $systemconnectresult = curl_exec($systemconnect);
  echo $systemconnectresult;
  echo '<br><br>';


  // USER LOG IN
  $userlogin = curl_init();
  curl_setopt($userlogin, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($userlogin, CURLOPT_POST, 1);
  curl_setopt($userlogin, CURLOPT_URL, 'http://www.xxxx.com/pms/?q=services/json');

  //prepare the field values being posted to the service
  $userlogindata = array('method' => 'user.login', 'sessid' => $sessid, 'username' => 'username', 'password' => 'password');
  curl_setopt($userlogin, CURLOPT_POSTFIELDS, $userlogindata);

  //make the request
  $userloginresult = curl_exec($userlogin);
  $userloginresult2 = json_decode($userloginresult);
  $sessid2 = $userloginresult2->data->sessid;

  echo $userloginresult;

  echo '<br><br>';


// NODE SAVE. Let's try to make an update
$change = array(
"nid" => 73,
"type" => "bond",
"status" => 1,
"changed" => "1224609039",
"revision_timestamp" => "1224609039",
"title" => "Banco de Oro Universal Bank ***UPDATE", //original data is Banco de Oro Universal Bank
"uid" => 1,
"name" => "username" //must be change to a valid username
);
$new = array(
"type" => "page",
"uid" => 1,
"name" => "username", //must be change to a valid username
"title" => "Sample Title of page",
"body" => "Sample Body of page"
);

$updatednode = json_encode($change);
$newnode = json_encode($new);

$nodesave = curl_init();

  curl_setopt($nodesave, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($nodesave, CURLOPT_POST, 1);
  curl_setopt($nodesave, CURLOPT_URL, 'http://www.xxxx.com/pms/?q=services/json');

  //prepare the field values being posted to the service
$nodesavedata = array('method' => 'node.save', 'sessid' => $sessid2, 'node' => $newnode);

  curl_setopt($nodesave, CURLOPT_POSTFIELDS, $nodesavedata);

  //execute the curl request
  $nodesaveresult = curl_exec($nodesave);

  //echo $nodesaveresult;
  //echo $updatednode;

  echo $newnode;

Comments

markrei’s picture

"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." - Lao Tzu
"God helps those who help themselves." - Ben Franklin
"Search is your best friend."

chicoski’s picture

"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." - Lao Tzu
"God helps those who help themselves." - Ben Franklin
"Search is your best friend."

Namrra’s picture

"Search is your best friend."

Hi, it's been 4 days since I started searching the net and I can't find the resource that I want. Maybe it's just I am using a wrong keyword.. or maybe can you just let me know if there is any source/link for me to solve this?