I have a php script needing Zend classes. It can be run in a browser, but errors occur when run the script by command lines in command prompt.
require_once 'Zend/Loader.php'; // It can work in a browser but failed by command lines
I also tried:
require_once 'C:\wamp\www\zf_project\library\Zend\Loader.php';
and
ini_set('include_path',
ini_get('include_path') .
PATH_SEPARATOR .
dirname(__FILE__). DIRECTORY_SEPARATOR. 'library');
But failed.
Then I need to load the class:
Zend_Loader::loadClass('Zend_Rest_Client');
How can I use Zend classes?
Thanks in Advance!
If all you want is to use Zend classes via autoloading—without bootstrapping your whole application—all you need to do in ZF1 (which it what you seem to be using):
<?php
// if ZF is not in your include path to begin with
set_include_path(implode(PATH_SEPARATOR, array('/path/to/zend/library', get_include_path())));
include 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::getInstance(); // registers autoloader
// now can access Zend classes without having to include
$client = new Zend_Http_Client(...);
Also note, you don't need to call Zend_Loader::loadClass
to load a class, this is done automatically by the autoloader when you use the class name in normal code, for example by calling the constructor as I've done above.
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);