nkou
nkou

Reputation: 36

Joomla 2.5 data retrieve in external php page

I use Joomla 2.5 for a website, but the home page is an external php page, where I need to get data from specific joomla articles and menu items.

  1. What is the best way to go, calling modules or connecting to database to retrieve what I need?
  2. How can I call the Joomla classes, functions etc to get my results?

What I've already tried:

The "module" solution from this post (Joomla Menu In External Page), which shows error concerning the session being already started.

Some code for data retrieval:

define('_JEXEC', 1);
define('JPATH_BASE', dirname(__FILE__).'/../../Joomla_2.5.9' );   // should point to joomla root
require_once ( JPATH_BASE .'/includes/defines.php' );
require_once ( JPATH_BASE .'/includes/framework.php' );
require_once ( JPATH_BASE .'/libraries/joomla/factory.php' );

$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('introtext')
 ->from('#__content')
 ->where('id = 1');
$db->setQuery($query);

$fullArticle = $db->loadResult();

echo $fullArticle;

This piece of code works great for getting the Article Text from a specific Article. Of course it's not that functional since I want to work around categories and multilingual content.

I will add whatever turns out to solve the problem in a better way. Any further ideas would certainly help!

Upvotes: 0

Views: 3090

Answers (2)

Tickseeker
Tickseeker

Reputation: 141

Nibra! The code of yours is very similar to mine:

<?php
define( '_JEXEC', 1 );
define( 'JPATH_BASE', '/home/user7633/public_html/cms' );
define( 'DS', '/' );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
JFactory::getApplication('site')->initialise();
$user = JFactory::getUser();
echo $user->username;
?>

The code done by this way does NOT work!

I am using Joomal 3.4.1 on PHP 5.4.36 and the code works only occasionally. Sometimes the username is empty and sometimes it is what excepted. I am logged in !

I am calling the PHP file from the "external URL" Menu Item.

Upvotes: 1

nibra
nibra

Reputation: 4028

You need to initialize the Joomla application:

$joomlaPath = dirname(__FILE__).'/../../Joomla_2.5.9';
define('_JEXEC', 1);
define('DS', DIRECTORY_SEPARATOR);

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

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

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

// Instantiate the application.
$app = JFactory::getApplication('site');

// Initialise the application.
$app->initialise();

First then you have access to the configured Joomla environment.

Upvotes: 2

Related Questions