简体   繁体   English

如何从 PHP 脚本在 joomla 3.0 中插入和发布新闻

[英]How insert and publish news in joomla 3.0 from PHP script

I try insert some articles from a PHP script but i don´t get it.我尝试从 PHP 脚本插入一些文章,但我不明白。 I need to know what fields are required and how I can create the query to do it.我需要知道需要哪些字段以及如何创建查询来执行此操作。

Now, I´m write the next code:现在,我正在编写下一个代码:

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);

    }

}

First order of business is to create the article data object.首要任务是创建文章数据对象。 I use a method to scrub out an existing id or asset_id references in case I'm migrating article data from one instance to another.我使用一种方法清除现有的 id 或 asset_id 引用,以防我将文章数据从一个实例迁移到另一个实例。 This could be replaced by your logic to build out the article data object as well.这也可以由您构建文章数据对象的逻辑代替。 Just be sure to use an associative array:请务必使用关联数组:

function processArticleData($obj) {
    $data = array();
    foreach ($obj as $key => $value) {
        $data[$key] = $value;
    }

    $data['id'] = 0;
    unset($data['asset_id']);

    return $data;
}

Next you load the JTable content class, bind the data and save.接下来加载 JTable 内容类,绑定数据并保存。 Joomla does all the rest: 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');
}

The benefits of this approach is it removes any "guessing" about required fields or potential errors, as if there is an issue with the data Joomla will throw an exception or error stating what the issue was.这种方法的好处是它消除了对必填字段或潜在错误的任何“猜测”,就好像数据有问题 Joomla 会抛出异常或错误,说明问题是什么。 If you wanted/needed to get real fancy, you could even load the JForm object for content, bind your data to it and then validate before binding to the JTable object.如果您想要/需要获得真正的幻想,您甚至可以为内容加载 JForm 对象,将数据绑定到它,然后在绑定到 JTable 对象之前进行验证。

Creating the data object has only two requirements.创建数据对象只有两个要求。 The first is to use an associative array and the second all key names match columns in the #__content table.第一个是使用关联数组,第二个是所有键名匹配#__content 表中的列。 An example data object would look like this:示例数据对象如下所示:

 $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
);

I use some more Joomla helper functions to make my job easier, but this should provide a good starting point for you to get the ball rolling.我使用了更多的 Joomla 辅助功能来简化我的工作,但这应该为您提供一个很好的起点来让球滚动。

* EDIT * * 编辑 *

Noticed a typo which I corrected.注意到一个错字,我更正了。 Not sure if you copy/pasted but switch the below line for the array declaration above and test again:不确定您是否复制/粘贴但切换上面数组声明的以下行并再次测试:

    $data       = processModuleData($obj);

This script should do it....assuming it exists in the root of your site:这个脚本应该这样做....假设它存在于您网站的根目录中:

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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM