johni07
johni07

Reputation: 771

Joomla add article via php script Application Instantiation Error

i created a script in order to insert some articles via a php script. This script is working on my local machine (xampp), but when i deploy the whole joomla project to my web server i get the following error message:

Error displaying the error page: Application Instantiation Error: Application Instantiation Error

By adding some echo calls, i was able to find the line which causes the error:

$app = JFactory::getApplication('site');

Now i am wondering how to fix this behaviour and make my function also run on the web server.

Below i will provide my system informations and the beginning of my php function until the line, which causes the error message:


Systeminformations


PHP snippet

<?php

echo "STARTING;   ";

// get db connection
include('../includes/mysql.inc.php');

// get all sql querys
include('./autoNewsQuerys.inc.php');

/**
 * Prepare joomla framework to insert article correctly
 */
if (!defined('_JEXEC')) {
    define('_JEXEC', 1);
    define('JPATH_BASE','/is/htdocs/wp1088688_4E1H7PYJFK/www');
    require_once(JPATH_BASE . '/includes/defines.php');
    require_once(JPATH_BASE . '/includes/framework.php');
    defined('DS') or define('DS', DIRECTORY_SEPARATOR);
}

echo "searching app;";
$app = JFactory::getApplication('site');
echo "Found app;";

 

Full browser output

STARTING; searching app;Error displaying the error page: Application Instantiation Error: Failed to start the session because headers have already been sent by "/is/htdocs/wp1088688_4E1H7PYJFK/www/1500AutoNews/autoNews.php" at line 3.

So as you can see the error is caused by the JFactory call. 'Found app' is never printed out.

Upvotes: 0

Views: 315

Answers (2)

Amit Ray
Amit Ray

Reputation: 3485

if it was working on localhost and it is not working on Web server then the biggest issue is with defining the base path. Give a hardcoded base path of your joomla installation. I suggest you to check your base path using a php script. Create a php file and name it path.php. Put it in the Joomla main folder. Content of the file

<?php
echo "The Base path is ".getcwd();
?>

Once you get the path just change the Base_Path. For ex:

define('JPATH_BASE', '\var\www\joomla');

Upvotes: 1

itoctopus
itoctopus

Reputation: 4251

Just copy the Joomla index.php file to another file and then replace the $app->execute code with your code.

Note: You don't need to include any MySQL library - since you are loading the Joomla environment then you have access to the JDatabase class, which is a database abstraction layer.

Another note: this file autoNewsQuerys.inc.php likely contains some issues (maybe you are trying to instantiate the app there, but the Joomla environment hasn't loaded yet). You should move it to the end.

Upvotes: 1

Related Questions