How to Create Nodes in Drupal 7 Programmatically

Node is a generic term for a piece of content in Drupal. A node has properties like title,body,creationdate,status etc. We can create own custom content type using; create nodes. Drupal provides a Admin interface to create nodes, for Somecases we need to Automate this process. In such cases We Need to create nodes Programmatically for getting data from external sources.

The Below code snippet will create node in custom code. Here is the code snippet for creating a node object and setting the properties node object finally node_save function is called to save the content.

// Code To create and save Node

$node = new StdClass();
$field=new stdClass(); //Field class needed for storing the node body
$node->type = "article"; // node type page,article,post etc
$node->language = LANGUAGE_NONE; //LANGUAGE_NONE
$node->title = "My Test Node Title" //Node Title
$node->body[$node->language][0]["value"] = "My Test Node Content Added Programmatically in Drupal"; //Node body
$node->body[$node->language][0]["summary"] = text_summary($node->body[$node->language][0][";value";]);
$node->body[$node->language][0]["format"] = "filtered_html"; //Text format
$node->status = 1; //Status is published
$node->uid = 1; //Userid

// Node Saved Successfully

Requirement :- Drupal 7