【发布时间】:2015-10-18 19:10:31
【问题描述】:
我希望能够使用 Joomla CMS 中的 cli 功能从命令行以编程方式在 Joomla 中添加许多文章。
我基本上使用的是Create a Joomla! Article Programmatically,但我的脚本在只创建了一篇带有错误行的文章后就关闭了
显示错误页面时出错:应用程序实例化 错误:应用程序实例化错误
这是我在 Joomla 的 /cli 文件夹中运行的代码。
我正在使用 Joomla 3.4
<?php
const _JEXEC = 1;
if (file_exists(dirname(__DIR__) . '/defines.php'))
{
require_once dirname(__DIR__) . '/defines.php';
}
if (!defined('_JDEFINES'))
{
define('JPATH_BASE', dirname(__DIR__));
require_once JPATH_BASE . '/includes/defines.php';
}
require_once JPATH_LIBRARIES . '/import.legacy.php';
require_once JPATH_LIBRARIES . '/cms.php';
require_once JPATH_CONFIGURATION . '/configuration.php';
class AddArticle extends JApplicationCli
{
public function doExecute()
{
$count = 10;
while ($count > 0)
{
$count--;
$jarticle = new stdClass();
$jarticle->title = 'New article added programmatically' . rand();
$jarticle->introtext = '<p>A programmatically created article</p>';
$table = JTable::getInstance('content', 'JTable');
$data = (array)$jarticle;
// Bind data
if (!$table->bind($data))
{
die('bind error');
return false;
}
// Check the data.
if (!$table->check())
{
die('check error');
return false;
}
// Store the data.
if (!$table->store())
{
die('store error');
return false;
}
}
}
}
JApplicationCli::getInstance('AddArticle')->execute();
【问题讨论】:
-
我想说的是:您必须有一个现有类别的类别 ID,并且您需要有一个创建者的用户 ID。您可以通过各种方式预定义这些,例如找到未分类的类别和第一个用户。
标签: php joomla command-line-interface article joomla3.4