Setting Up Your Magento Scripting Environment

One of the things that I do quite often in Magento is write scripts to accomplish certain tasks. Mostly these tasks involve me updating prices, creating a sale, or fixing a data problem in products. The code base for Magneto is heavily object oriented which makes it incredibly easy to pull in the full power of Magento and use it in outside scripts.

However, before you dive into this you want to make sure that you do a little forethought so that you do not accidentally leave yourself open to security attacks and the like.

The first thing you want to do is determine where you are going to store all of these scripts. The first thing I would recommend is that you do not put the scripts in a spot that is accessible over the internet. This removes the possibility that your scripts will be run by someone you did not intend to since it cannot be called from a web browser.

However, I understand that not everybody has the luxury. In shared hosting environments you probably have your root directory and that is all. In that case you will want to use an .htaccess file to secure the directory (this tutorial assumes you are using Apache). Let’s assume that you are going to create a directory called “scripts” in your Magento root directory. So in your scripts directory you are going to create a file called “.htaccess” and in it you are going to place the following text

# No browsing this directory
Order deny,allow
Deny from all

This is just some fancy Apache wording that basically says nobody can access anything in this directory through the web server (in this example Apache).

Now that we have your scripts directory setup (and protected) we can now go over creating a base file that you can reuse to create all sorts of scripts.

Create a file in your scripts directory called “magento_base.php” and place the following inside of it:

1.  <?php
2.  // I want to see errors
3.  error_reporting(E_ALL);
4.  ini_set('display_errors', '1');
5.
6.  // Modify the PHP Environment
7.  ini_set('memory_limit', '2048M');
8.
9.  // Load Up Magento Core
10. define('MAGENTO', realpath('/path/to/magento/install'));
11.
12. require_once(MAGENTO . '/app/Mage.php');
13.
14. Mage::app();

Let’s explain what we are doing here.

Obviously line 1 is the opening PHP tag. Do not forget this or your scripts will not run.

Lines 3 and 4 tell PHP to show errors (In a script you want to see your errors and it is not a big deal since you are the only one who can access the script). Trust me this will save you a headache when your script does not seem to run but you get no feedback.

Line 7 is somewhat optional. PHP has a preset memory limit that is set in your php.ini file. This line will override that to be what you want it to (in this case it is 2 Gigabytes). If you find your scripts are getting out of memory errors, this should help eliminate that error.

Line 10 creates a global variable called “MAGENTO”. We set it to be the path of our Magento installation. We run it through PHP’s realpath function so that it will work no matter what we put in there (as long as it is a correct path).

Line 12 pulls in Magento’s base file. This gives you the power to use Magento’s “Mage” object (the heart and soul of Magento).

Line 14 is very important. This “bootstraps” Magento. It connects to the database, allows you to call all the objects inside Magento. It basically sets up your script.

Now that you have the text in this file, you should do a test run to make sure there are no errors. From the command line simply type:

php magento_base.php

If you get no output whatsoever, you are all set. You can now copy this file as many times as you want to do all your Magento scripting.

Leave a Reply

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