When you run it from web browser, the include path is set in public/index.php and then bootstrap the application. Similarly, you can copy public/index.php (e.g. as setup.php) and include it in your command line code. Also, copy the bootstrap bits that you need into that file.
Note that in ZF2 there is "console route" that let you create MVC command line script.
Here's my setup.php, notice how I load configuration with "new Zend_Config". Just 'require' this file in file you want to run from command line (console).
Edit: you have to set APPLICATION_PATH correctly in '/relative/path/to/application/'.
<?php
error_reporting(E_ALL & ~E_NOTICE | E_STRICT);
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/relative/path/to/application/'));
// Define application environment
define('APPLICATION_ENV', 'development');
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));
// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
require_once 'Zend/Application.php';
$app = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
$res = $app->getOption('resources');
$config = new Zend_Config($res);