Access Joomla 2.5 from external script to get article by id
There are a few things you don't need in your script and I have changed it to Joomla 2.5 coding standards:
define('_JEXEC', 1);
define('JPATH_BASE', realpath(dirname(__FILE__)));
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 = 260');
$db->setQuery($query);
$fullArticle = $db->loadResult();
echo $fullArticle;
Hope this helps
How to log into joomla through an external script?
<?php
//http://domain.com/script/script.php?username=username&passwd=password
define( '_JEXEC', 1 );
define('JPATH_BASE', '../' );
define( 'DS', DIRECTORY_SEPARATOR );
require_once('../configuration.php');
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );
require_once ( JPATH_BASE .DS.'libraries'.DS.'joomla'.DS.'factory.php' );
/* Create the Application */
$mainframe =& JFactory::getApplication('site');
jimport('joomla.plugin.helper');
$credentials = array();
$credentials['username'] = JRequest::getVar('username', '', 'method', 'username');
$credentials['password'] = JRequest::getVar('passwd', '', 'method', 'passwd');
//perform the login action
$error = $mainframe->login($credentials);
$user = JFactory::getUser();
//now you are logged in
$mainframe->logout();
//now you are logged out
How to get current Joomla user with external PHP script
While I don't see anything in the code that's unsafe, it's best to make your AJAX/JSON calls to a standard Joomla component. There's a good article on how to do this here: http://blog.syncleon.com/2009/05/ajax-ify-your-joomla-website.html I've also written about JavaScript, Joomla, and asynchronous requests in my book http://www.packtpub.com/files/learning-joomla-1-5-extension-development-sample-chapter-8-using-javascript-effects.pdf (skip down to page 168).
Essentially, what you do is create a view for the output of your AJAX call, then create a view.xml.php (or view.json.php) file instead of a view.html.php. When you add &format=xml
to the end of your request URL, it will pull from view.xml.php instead of view.html.php.
Accessing session data outside Joomla
Actually that's not as easy as it sounds. Joomla uses its own session handling with come unique session-id-generation and some encryption in place, so the only way to get into the Joomla session data is to use the appropriate Joomla functions (as others have suggested). I recently had a project where we needed to transfer a Joomla authenticated user into a separate application. We did this by adding a Joomla adapter which instantiates the Joomla user classes, reads the user data, puts everything into an encrypted cookie and redirects back to our application. In there we read the encrypted cookie, instantiate our own user object and discard the cookie. As this is not 100% secure we're changing the system to write the user data in a database table and read it from our application - we avoid the unsecure way through a cookie that way, because even though the cookie is encrypted (and contains sensitive user information which suffice to authenticate a user) it'll be transfered on wire and could be sniffed.
define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(dirname(__FILE__)));
define( 'DS', DIRECTORY_SEPARATOR );
require_once (JPATH_BASE . DS . 'includes' . DS . 'defines.php');
require_once (JPATH_BASE . DS . 'includes' . DS . 'framework.php');
$mainframe = JFactory::getApplication('site');
The above is the basic script required to access Joomla resources.
Get data from database in Joomla 2.5
As long as you are trying to write a Joomla component, I recommend you to start learning writing your code with MVC architecture. Joomla Documentation on this subject is available here. Anyhow, I'm gonna give you a simple but temporary solution and you have to rewrite it later using Joomla's MVC.
I presume that you want to select column1
and column2
data from your #__example_table
and show then in a simple table.
First, you need to create a component with an empty view. You can use this online tool for creating a component at a glance; and of course it's free for creating components with empty views. After building, downloading and installing your component, let's call it com_mycomponent
, there will be a folder called com_mycomponent
in your /components
folder. open it up and you'll see a views
folder and inside it there's gonna be a folder for your view that I'm gonna call that myview
.
In your view folder, there's a file called view.html.php
which contains you view's class and also you can see a folder called tmpl
which contains a default.php
file for your view's template.
Now open view.html.php
in an editor and create a public attribute called $items
and getData()
method after display()
method like this:
<?php
class MycomponentViewMyview extends JView {
.
.
.
public $items;
public function display($tpl = null) {
.
.
.
$this->items = $this->getData();
parent::display($tpl);
}
public function getData() {
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select(array('column1', 'column2'));
$query->from('#__example_table');
$query->where('condition = 1');
$query->order('id DESC');
$db->setQuery($query);
$results = $db->loadObjectList();
if (!empty($results)) {
return $results;
}
return false;
}
}
In this example I've used Joomla's database API which are described here; and consider that dots means rest of the codes which you don't need to change.
Now open /components/com_mycomponent/views/myview/tmpl/defailt.php
and delete everything here and write something like this:
<?php
defined('_JEXEC') or die; // No direct access
if (count($this->items)) {
?>
<table style="width: 100%">
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
</tr>
</thead>
<tbody>
<?php foreach ($this->items as $item) { ?>
<tr>
<td><?php echo $item->column1; ?></td>
<td><?php echo $item->column2; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<?php
} else {
echo 'Sorry! No data found...';
}
I think the example is quite clear and there's no need of explanation, but I'm gonna give you a brief explanation about what's happening. First of all we created an method to get data from database and store them in a variable which is accessible from outside. Then we call that method from display method which is somehow the constructor of the view class. Then in the view's template I used a loop for the records stored in $items
variable and show them in table rows.
- I tried to explain the solution in most simple possible way so that everyone could use it, and if it bothers you, I'm sorry.
- Feel free to ask question and correct me.
Related Topics
Set Active State on Navigation Dynamically
Laravel 4 Custom Named Password Column
Round to Max Thousand, Hundred etc in PHP
Mysqli_Stmt::Execute() Expects Exactly 0 Parameters, 1 Given
Multiple Excerpt Lengths in Wordpress
How to Print SQL Statement in Codeigniter Model
How to Enable PHP to Work with Postgresql
Special Characters in Fpdf with PHP
Add PHP Variable Inside Echo Statement as Href Link Address
PHP Get Request, Sending Headers
Dynamically Changing Image Colours
Using Regex to Filter Attributes in Xpath with PHP
Codeigniter Sessions Not Working After Migration
Fastest Hash for Non-Cryptographic Uses
How to Run a Cronjob Every X Minutes
Detecting Ajax in PHP and Making Sure Request Was from My Own Website
"No Such File or Directory" or "No Such Host Is Known" When Running Migrations