I also faced the similar issue once and I modified the code as following which worked for me. You also need to pass the access
and metadata
in the article parameters.
$article_data = array(
'id' => 0,
'catid' => 2,
'title' => 'My article title',
'alias' => 'my-article-alias',
'introtext' => 'My intro text',
'fulltext' => 'My full text',
'state' => 1, //if you want to keep the article published else 0
'alias' => 'my-article-alias',
'state'=>1,
'language' => '*',
'access' => 1,
'metadata' => json_encode(array('author' => '', 'robots' => ''))
);
$article_id = createArticle($article_data);
if(!$article_id){
echo "Article create failed!";
}
else{
echo 'Article created';
}
function createArticle($data)
{
$data['rules'] = array(
'core.edit.delete' => array(),
'core.edit.edit' => array(),
'core.edit.state' => array(),
);
$basePath = JPATH_ADMINISTRATOR.'/components/com_content';
require_once $basePath.'/models/article.php';
$article_model = JModelLegacy::getInstance('Article','ContentModel');
// or $config= array(); $article_model = new ContentModelArticle($config);
if(!$article_model->save($data)){
$err_msg = $article_model->getError();
return false;
}else{
$id = $article_model->getItem()->id;
return $id;
}
}
Note: For static, it will work only once as article article alias needs to be unique.
Hope this helps.