Code: Select all
<?php
if (!defined( '_JEXEC' )) {
define( '_JEXEC', 1 );
define( 'JPATH_BASE', realpath(dirname(__FILE__)));
require_once ( JPATH_BASE .'/includes/defines.php' );
require_once ( JPATH_BASE .'/includes/framework.php' );
defined( 'DS' ) or define( 'DS', DIRECTORY_SEPARATOR );
}
$app = JFactory::getApplication( 'site' );
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.dtn.com/publishing/news/articles?categoryId=16%2C17%2C18&limit=3&maxAge=30&apikey=placeholder',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
));
$json = curl_exec($curl);
curl_close($curl);
$data = json_decode($json, true);
foreach ($data as $value) {
$articleData = array(
'id' => 0,
'catid' => 8,
'title' => ucwords(strtolower($value['title'])),
'alias' => '',
'introtext' => strip_tags($value['storySummary']),
'fulltext' => strip_tags($value['content']),
'state' => 1,
'access' => 2,
'created_by' => 332,
'language' => '*'
);
$articleId = createArticle($articleData);
if(!$articleId) {
echo JText::_('Article(s) could not be 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';
$config = array();
$articleModel = new ContentModelArticle($config);
if(!$articleModel->save($data)) {
$errMsg = $articleModel->getError();
return false;
} else {
$id = $articleModel->getItem()->id;
return $id;
}
}
Code: Select all
<?php
/**
* @package Joomla.Cli
*
* @copyright (C) 2012 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
/**
* A command line cron job to bulk import JSON data into Joomla articles
*/
// Initialize Joomla framework
const _JEXEC = 1;
// Load system defines
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';
}
if (!defined( '_JEXEC' )) {
define( '_JEXEC', 1 );
define( 'JPATH_BASE', realpath(dirname(__FILE__)));
require_once ( JPATH_BASE .'/includes/defines.php' );
require_once ( JPATH_BASE .'/includes/framework.php' );
}
// Get the framework.
require_once JPATH_LIBRARIES . '/import.legacy.php';
// Bootstrap the CMS libraries.
require_once JPATH_LIBRARIES . '/cms.php';
/**
* CLI script to bulk import JSON data into Joomla articles.
*
* @since 2.5
*/
class BulkArticles extends JApplicationCli {
public function doExecute() {
// Fetch the JSON data
$this->fetchData();
// Run the createArticle bulk data import to articles function
$this->createArticle($articleData);
}
private function fetchData() {
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://api.dtn.com/publishing/news/articles?categoryId=16%2C17%2C18&limit=10&maxAge=30&apikey=placeholder',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
));
$json = curl_exec($curl);
curl_close($curl);
$data = json_decode($json, true);
foreach ($data as $value) {
$articleData = array(
'id' => 0,
'catid' => 8,
'title' => ucwords(strtolower($value['title'])),
'alias' => '',
'introtext' => strip_tags($value['storySummary']),
'fulltext' => strip_tags($value['content']),
'state' => 1,
'access' => 2,
'created_by' => 332,
'language' => '*'
);
$articleId = createArticle($articleData);
if(!$articleId) {
echo JText::_( 'TPL_COULD_NOT_CREATE_ARTICLES' );
}
}
}
private 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';
$config = array();
$articleModel = new ContentModelArticle($config);
if(!$articleModel->save($data)) {
$errMsg = $articleModel->getError();
return false;
} else {
$id = $articleModel->getItem()->id;
return $id;
}
}
}
JApplicationCli::getInstance('BulkArticles')->execute();