Failed to Start application when posting article with PHP script

by arsarc   Last Updated October 10, 2018 19:10 PM

I'm trying to use a combination of this script about posting articles and the previous script it links to.

So what I did was I have my JoomlaFolder with joomla installed and set up, and I created a test.php file and stuck it inside the folder.

In the test.php file, I put:

<?php
/**
 * @package    Joomla.Site
 *
 * @copyright  Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
 * @license    GNU General Public License version 2 or later; see LICENSE.txt
 */

/**
 * Define the application's minimum supported PHP version as a constant so it can be referenced within the application.
 */

 define('JOOMLA_MINIMUM_PHP', '5.3.10');

 if (version_compare(PHP_VERSION, JOOMLA_MINIMUM_PHP, '<'))
{
    die('Your host needs to use PHP ' . JOOMLA_MINIMUM_PHP . ' or higher to run this version of Joomla!');
}

// Saves the start time and memory usage.
$startTime = microtime(1);
$startMem  = memory_get_usage();

/**
 * Constant that is checked in included files to prevent direct access.
 * define() is used in the installation folder rather than "const" to not error for PHP 5.2 and lower
 */
define('_JEXEC', 1);

if (file_exists(__DIR__ . '/defines.php'))
{
    include_once __DIR__ . '/defines.php';
}

if (!defined('_JDEFINES'))
{
    define('JPATH_BASE', __DIR__);
    require_once JPATH_BASE . '/includes/defines.php';
}

require_once JPATH_BASE . '/includes/framework.php';

$article_data = array(
    'id' => 3,
    'catid' => 2,
    'title' => 'My article title',
    'alias' => 'my-article-alias',
    'introtext' => 'My intro text',
    'fulltext' => 'My full text',
    'state' => 1, //if you want to keep the article published else 0
    'alias' => 'my-article-alias',
    'state'=>1,
    'language' => '*',
    'access' => 1,
    'metadata' => json_encode(array('author' => '', 'robots' => '')),
    'featured' => 1
);

$article_id = createArticle($article_data);
if(!$article_id){
    echo "Article create failed!";
}
else{
    echo 'Article 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';
    $article_model =  JModelLegacy::getInstance('Article','ContentModel');
    // or  $config= array(); $article_model =  new ContentModelArticle($config);
    if(!$article_model->save($data)){
        $err_msg = $article_model->getError();
        return false;
    }else{
        $id = $article_model->getItem()->id;
        return $id;
    }

}

?>

And then I went to localhost/JoomlaFolder/test.php and tried to run it, and I got:

Error: Failed to start application: Failed to start application

So what I want to ask is, what library do I need to include to make this script work?

Or am I missing something that I need to change in order to get this to work?

Tags : php


Related Questions