如何从PHP脚本在joomla3.0中插入和发布新闻


How insert and publish news in joomla 3.0 from PHP script

我试着从PHP脚本中插入一些文章,但我不明白。我需要知道需要哪些字段,以及如何创建查询才能做到这一点。

现在,我要写下一个代码:

foreach($RSS_DOC->channel->item as $RSSitem){
    $alias    = md5($RSSitem->title);
    $fetch_date = date("Y-m-j G:i:s"); //NOTE: we don't use a DB SQL function so its database independent
    $item_title = $RSSitem->title;
    $item_description = $RSSitem->description;
    $item_date  = date("Y-m-j G:i:s", strtotime($RSSitem->pubDate));
    $item_url   = $RSSitem->link;
    // Does record already exist? Only insert if new item...
    $item_exists_sql = "SELECT alias FROM jos_content where alias = ".$alias;
    $item_exists = mysqli_query($enlace,$item_exists_sql);
    if(mysqli_num_rows($item_exists)<1){
        $mensaje = "<p>".$item_description."</p>Publicado: ".$item_date."<br/><a href='".$item_url."'>Ver noticia original</a>";
        $item_insert_sql = "INSERT INTO jos_content(title, alias, introtext, state, catid, created, created_by, access,featured) VALUES ('" .$item_title. "', '" . $alias. "', '" . $mensaje. "',1, 10,'".$fetch_date."',448,1,1)";
        $insert_item = mysqli_query($enlace,$item_insert_sql);
    }
}

业务的第一步是创建文章数据对象。我使用一种方法来清除现有的id或asset_id引用,以防将文章数据从一个实例迁移到另一个实例。这也可以用构建文章数据对象的逻辑来代替。只需确保使用关联数组:

function processArticleData($obj) {
    $data = array();
    foreach ($obj as $key => $value) {
        $data[$key] = $value;
    }
    $data['id'] = 0;
    unset($data['asset_id']);
    return $data;
}

接下来加载JTable内容类,绑定数据并保存。Joomla做所有其他的事情:

function addArticle($obj) {
    // Initialise variables;
    $data       = processModuleData($obj);
    $table      = &JTable::getInstance('content');
    // Bind the data.
    if (!$table->bind($data))
    {
        echo "<h1>Error Binding Article Data</h1>";
        return false;
    }
    // Check the data.
    if (!$table->check())
    {
        echo "<h1>Error Checking Article Data</h1>";
        return false;
    }
    // Store the data.
    if (!$table->store())
    {
        echo "<h1>Error Storing Article Data</h1>";
        return false;
    }
    return $table->get('id');
}

这种方法的好处是,它消除了对所需字段或潜在错误的任何"猜测",就好像数据有问题一样。Joomla会抛出一个异常或错误来说明问题所在。如果您想要/需要获得真正的幻想,您甚至可以为内容加载JForm对象,将数据绑定到它,然后在绑定到JTable对象之前进行验证。

创建数据对象只有两个要求。第一种是使用关联数组,第二种是所有关键字名称都与#__content表中的列匹配。一个示例数据对象如下所示:

 $data = array(
    'title' => $title,
    'alias' => $alias,
    'introtext' => $introtext,
    'fulltext' => $fulltext,
    'catid' => $catid,
    'images' => '',
    'urls' => '',
    'attribs' => '',
    'metakey' => '',         
    'metadesc' => '',
    'metadata' => '',
    'language' => '',
    'xreference' => '',         
    'created' => JFactory::getDate()->toSql(),
    'created_by' => JFactory::getUser()->id,
    'publish_up' => JFactory::getDate()->toSql(),
    'publish_down' => JFactory::getDbo()->getNullDate(),
    'state' => 1
);

我使用了一些更多的Joomla辅助函数来简化我的工作,但这应该为你开始工作提供一个很好的起点。

*编辑*

注意到一个打字错误,我纠正了。不确定是否复制/粘贴,但切换下面的行以获取上面的数组声明并再次测试:

    $data       = processModuleData($obj);

这个脚本应该这样做……假设它存在于您的站点的根目录中:

if (!defined('_JEXEC')) {
    define( '_JEXEC', 1 );
    define ('JPATH_BASE', 'c:''wamp''www''mysiteroot');
    require_once ( JPATH_BASE .'/includes/defines.php' );
    require_once ( JPATH_BASE .'/includes/framework.php' );
    $mainframe = JFactory::getApplication('site');
}
function getContentTable($type = 'Content', $prefix = 'JTable', $config = array())
{
    return JTable::getInstance($type, $prefix, $config);
}
function addArticle($title, $alias)
{
    $table = getContentTable();
    $table->title = $title;
    $table->alias = $alias;
    $table->catid = 2;
    $table->state = 1;
    // and so on!
    // then save it
    $table->store();
}
$result = addArticle("foo", "bar");