Execute Joomla CLI script to bulk import JSON data to articles

For Joomla! 3.x Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general

Moderator: ooffick

Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
Post Reply
mike_hermary
Joomla! Intern
Joomla! Intern
Posts: 67
Joined: Fri Aug 20, 2010 3:04 am

Execute Joomla CLI script to bulk import JSON data to articles

Post by mike_hermary » Thu Jan 27, 2022 12:37 am

I have the following PHP code working to bulk add array items from JSON to com_content. The code is executed by directly accessing the file in the web browser. For example, http://examples.com/json-articles.php.

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;
  }
}
Now I am needing to convert it to a CLI app that can be executed at a set time everyday using a CRON job. I have reviewed existing CLI scripts in the CLI directory and have coded the following so far. I used this Joomla StackExchange post: https://joomla.stackexchange.com/questi ... /5361#5361 and started with the garbagecron.php CLI script as references.

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();
Any help or tips is greatly appreciated.

Post Reply

Return to “Joomla! 3.x Coding”