Give Your Command Line Script Full Access to Magento

In the past year, I have had the pleasure of having to write a lot of scripts that accomplish certain tasks inside Magento. It is not immediately obvious how to do this, but once you know how, you will love the power that it gives you.

In your PHP script simply put the following at the top:

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

// Load Up Magento Core
define('MAGENTO', realpath('/path/to/magento'));

require_once(MAGENTO . '/app/Mage.php');

$app = Mage::app();

?>

You can now do anything you would normally do inside Magento. I usually end up loading and manipulating a models in my scripts. You will notice at the top that I set error reporting to be outputted. I like to do this to ensure that errors are being displayed on the command line (it will override any other settings).

Leave a Reply

Your email address will not be published. Required fields are marked *