creating drupal nodes programmatically with drupal_execute function
You can create Drupal node from your PHP code using drupal_execute() or node_save() functions. I prefere drupal_execute as it gives you validation errors. Here is an example that includes adding the node to the taxonomy term.
To find the form id of the form you wish to simulate go to the desired form, than do view source. Search for form_id. You will find a line similar to:
<input type="hidden" name="form_id" id="edit-article-teaser-node-form" value="article_teaser_node_form" />
in this example the form id is in the value: article_teaser_node_form , and not in the id
function insert_node(){
// prepare form values to be inserted
$values['values']['title'] = $this->node->title;
$values['values']['field_intro'][0]['value'] = $this->node->intro;
$values['values']['field_body1'][0]['value']= $this->node->text;
// taxomony term where this node will be attached to
$tid = 32; // lets say your term id is 32, term ids are unique across all vocabularies
$values['values']['taxonomy'] = Array(1 => Array($tid => $tid));
$values['values']['op'] = t('Save');
$node = array('type' => 'news_article'); // this is the type of your node
//insert values using Drupal forms api
if ($tid) {
drupal_execute('news_article_node_form', $values, (object)$node); //news_article_node_form is the id of your form
$errors = form_get_errors();
}else {
$errors['Custom error'] = '<p style= "color: red;"> Node: "'. $this->node->title .'" - Not inported. Taxonomy term "'. $this->node->category .'" is invalid</p>';
}
//print_r($errors);
if ($errors){
foreach($errors as $key=>$value){
$output = '<p style= "color: red;"> Error: - '.$key.', - '.$value.'</p>';
}
}
else{
$output = null;
}
return $output;
}
