1

In our system, the content is generated by a standalone PHP script that runs from command line. Is there a way to add this content programmatically to a remote Drupal 7 CMS from this standalone PHP script?

I am not able to find such information on drupal.org. I have found the following code:

$node = new stdClass(); 

// Set content type
$node->type = 'article';

// Prepare defaults
node_object_prepare($node);

// Define language (currently language neutral)
$node->language = LANGUAGE_NONE;

// Basic content
$node->title = 'Test';
$node->body[$node->language][0]['value'] = 'Body text';

// Example if using custom fields
$node->field_CUSTOM_FIELD[$node->language][0]['value'] = 'Value';
// Example if using fields that are taxonomy type
$node->field_CUSTOM_FIELD_TAXONOMY[$node->language][0]['tid'] = 1;

// Save node
node_save($node);

But this code has to run from Drupal 7 server process only. This code cannot be used in a standalone PHP script that runs remotely. Another option is Entity API but that code too has to run on Drupal Server only. Do I need to create my own module and code my standalone PHP script to connect to API exposed by this module?

1
  • 6
    @Veeshoo Basically, you've got two options. 1) Call drupal_bootstrap() yourself, and Drupal will load up it's files and modules, and you could run the code above in your own php. 2) Run Services on your receiving site, and push content over HTTP. There are other ways, but that's where I'd start. – Letharion Mar 6 '13 at 9:46
1

Services module can be used to allow to create node remotely

Check out some reference links as below

1
0

You are right that the script cannot run as a standalone script - it must be bootstrapped within Drupal before you can use the Drupal API to make changes to the site. The simplest way to do this is to run your script through drush using the php-script command which will allow you to run a bootstrapped script.

For this however your script needs access to the site's database and the site's code-base (you could do this on a copy of the code base if your script doesn't need/affect files - though that could be risky unless you know exactly what you are doing).

If this is not available (your script is running on a remote server without access to the database/code base of the site) then you need a solution with two components: one that generates some data on the remote server, and one that imports the data on the Drupal side.

One way to import external data within Drupal is to use the Feeds Module. It already has a number of possible formats (such as importing from formatted CSV files) and also allows you to write your own importers quite easily.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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