Search code examples
phpdrupal-7

How to pass a Drupal node object to another Drupal Instance?


Can I pass the node object $node to another Drupal instance (another Drupal site)?

  • Lets just forget about Node Conflictions .. and problems etc..
  • Is it POSSIBLE to pass a Drupal Node Object to another Drupal Instance?
  • How can I pass that Node to another Drupal Site?

Solution

  • Speaking purely hypothetically, you could. I am going to ignore a lot of the problems you would face in attempting this (max POST size, assuming both sites have same node types & fields, ect..)

    On your Drupal site "A" (your sender), I'm assuming your script is a PHP script within a custom module called "mysendermodule" and on your Drupal site "B" (your receiver) you have a custom module called "myrecievermodule".

    Your sender module would need to encode the $node object in a way that it can send it as a POST variable (again, we are ignoring the issue of the MAX Post size). I'm going to choose to json encode it followed by base64 encoding to remove any special characters. Your Sender module would make a POST request to the Destination using cURL. It would then wait for a response to see if it was successful, Like so:

    <?php
        function mysendermodule_sendNode($node){
            $destination = "http://www.otherdrupalsite.com/recieve-node";
            //encode the node to be sent
            $encoded = base64_encode(json_encode($node));
            //built POSTVARS
            $postvars = 'node='.$encoded;
    
            //make cURL call to send the node
            $ch = curl_init($destination);
            curl_setopt($ch,CURLOPT_POST,1);
            curl_setopt($ch,CURLOPT_POSTFIELDS,$postvars);
            curl_setopt($ch, CURLOPT_HEADER      ,0);  // DO NOT RETURN HTTP HEADERS
            curl_setopt($ch, CURLOPT_RETURNTRANSFER  ,1);  // RETURN THE CONTENTS OF THE CALL
            $return= curl_exec($ch);
            curl_close($ch);
            //see if we got back a success message
            if ($return == 'TRUE'){
                return true;
            }
            return false;
        }
    

    Now on your other Drupal site (the receiver) with the custom receiver module. It would first need to create a destination point to receive the node, using the menu hook. It would then need to decode the received node, programatically insert it using node_save and returning a success or failure. Like so:

    <?php   
        function myrecievermodule_menu(){
            $items['recieve-node'] = array(
                'page callback' => 'myrecievermodule_recieveNode',
                'access arguments' => array('access content'),
                'type' => MENU_CALLBACK,
            );
            return $items;
        }
    
        function myrecievermodule_recieveNode(){
            $message = 'FALSE';
            //did we recieve a node?
            if ($_POST['node']){
                //decode it
                $node = json_decode(base64_decode($_POST['node']));
                //does it have a valid node object field?
                if (isset($node->title)){
                    //attempt to save it (will return nid if successful)
                    if (node_save($node)){
                        $message = 'TRUE';
                    }
                }
            }
            //return just the output of message.
            ob_clean();
            ob_start();
            echo $message;
            ob_end_flush();
            exit;
        }
    

    Again this ignores a lot of the many issues you would face in implementing this, however it is possible.