PHP als client script aufrufen

edit | delete

Autor: Vilma Plum, Ingmar Pforr

eingetragen: Dienstag, 08. Januar 2008 um 09:24 Uhr (2/2008 Kalenderwoche)

geändert: Dienstag, 13. Dezember 2011 um 15:07 Uhr (50/2011 Kalenderwoche)

Keywords: php cli cronjob cron

Kategorien: Apache, PHP,

Text:

PHP kann neben dem Aufruf über den Webserver auch als eigenständiges Modul ausgeführt werden. Das setzt voraus, dass PHP zusätzlich als cli installiert ist. Diese Variante ist besonders nett für den Aufruf als cronjob. Auf diese Weise muss kein Webaufruf im cronjob getätigt werden.
Zur sauberen Funktion muss innerhalb der Scripte bei den Pfadangaben leider auf einige Besonderheiten geachtet werden:
includes innerhalb desselben Verzeichnisses sind valide;
includes in höher gelegene Verzeichnisse sind es nicht; hier sollte der Pfad als absoluter angegeben werden.


Zend Framework Anwendungen auf CLI ausführen
1. in setincludepath müssen absolute Pfade stehen
2. ZendControllerFront dispatch() wird nicht ausgeführt
index.php siehe Quellcode
3. Script(e) mit require aufrufen
4. auf Kommandozeile php.ini-Pfad angeben

Quellcode:  

include 'var/www/resimao/htdocs/gen_config.inc.php';

Zend Framework Anwendungen auf CLI ausführen
optional: setze Umgebungsvariable Zend Tool Pfad für das ZF CLI
setze PHP Pfad
optional: setze eigene Umgebungsvariable für Cronjob
führe php CLI aus mit Angabe des php.ini-Pfades

Windows cmd.exe:
set ZEND_TOOL_INCLUDE_PATH=C:\libraries\ZendFramework-1.10.2\library
set Path=%Path%;c:\xampp\php\
set CRONJOB=true
php -c C:\xampp\apache\bin c:\xampp\htdocs\iuu\public\index.php


index.php:
<?php
/** ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
 *
 * @name       	index.php
 * @abstract   	Bootstrap for Zend Framework
 * @author     	Norman Rauthe, Ralf von der Mark
 * @copyright  	Copyright (c) 2010, BLE
 * @version    	1.0.0 30.05.2010
 *
 * ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ */
error_reporting(E_ALL);//nur waehrend der Entwicklungsphase!!
//ini_set('memory_limit', '256M');


//-------from standard-------------------
// Define path to application directory
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

//Name des Gesamtverfahren, vgl. Name des Unterverfahren, z.B. 'Einfuhr'
defined('PROCEEDING_NAME')
|| define ('PROCEEDING_NAME', 'Kontrollverfahren IUU-Fischerei');

//require the application environment constant
require_once APPLICATION_PATH . '/configs/_config.php';
//define paths
//define('FILES_BASE_PATH', 'c:/temp/iuu/');
define('FILES_BASE_PATH', APPLICATION_PATH . '/../data');
define('MESSAGES_IN_PATH', FILES_BASE_PATH . '/messages/in');
define('MESSAGES_IN_NEW_PATH', MESSAGES_IN_PATH . '/new');
define('SCRIPT_RUN_1_PATH', MESSAGES_IN_PATH . '/script_run_1');
define('SCRIPT_RUN_2_PATH', MESSAGES_IN_PATH . '/script_run_2');
define('FAILED_PATH', MESSAGES_IN_PATH . '/failed');

define('ARCHIVE_PATH', FILES_BASE_PATH . '/archive');

//define proceeding specific objects
define('PROCEEDING_FILE_REFERENCE_PREFIX', '523-04.10-IUU-');

set_include_path(
'.'
. PATH_SEPARATOR . APPLICATION_PATH . '/../library/'
. PATH_SEPARATOR . APPLICATION_PATH . '/components/'
. PATH_SEPARATOR . APPLICATION_PATH . '/models/'
. PATH_SEPARATOR . '/usr/share/ZendFramework-1.10/library/'
. PATH_SEPARATOR . 'c:/libraries/ZendFramework-1.10.2/library/'
//. PATH_SEPARATOR . 'c:/libraries/ZendFramework-1.11.11/library/'
. PATH_SEPARATOR . 'C:/libraries/phpids-0.6.5/phpids-0.6.5/lib/'
. PATH_SEPARATOR . '../library/dompdf_0-6-0_beta2/dompdf/'
. PATH_SEPARATOR . get_include_path() );


//Controller erstellen:
require_once 'Zend/Loader/Autoloader.php';
$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->setFallbackAutoloader(true);

$locale = new Zend_Locale('de_DE');
Zend_Registry::set('Zend_Locale', $locale);

$controller = Zend_Controller_Front::getInstance();

$controller ->setControllerDirectory('../application/controllers/');
// for development choose true
$controller->throwExceptions(true)
->registerPlugin(new Plugin_Config())
->registerPlugin(new Plugin_Db())
//->registerPlugin(new Plugin_Auth())

->registerPlugin(new Plugin_Translate())
->registerPlugin(new Plugin_Security())
//           ->registerPlugin(new Plugin_Registry())
->setBaseUrl();///webgr_php/public/

//ACL
$_acl = new Acl();
$_auth = Zend_Auth::getInstance();
$controller->registerPlugin(new Plugin_Auth($_acl, $_auth));
$controller->registerPlugin(new Plugin_Navigation($_acl, $_auth));

$controller->registerPlugin(new Plugin_Layout());

//ask SAPI
//'cli' means PHP Command Line Interface SAPI is used
if (php_sapi_name() == 'cli') {
    //self defined environment variable 'CRONJOB' means you want to run the cronjob script(s)
    $envCronjob = getenv('CRONJOB');
    if ($envCronjob == true) {
        $writer = new Zend_Log_Writer_Stream(APPLICATION_PATH . '/../data/logs/logfile');
        $logger = new Zend_Log($writer);
        $logger->info('importMails process start');
        require APPLICATION_PATH . '/models/jobs/importMails.php';
        $logger->info('importMails process end');
        exit();
    } else {
        echo 'Error: no env set. e.g. set CRONJOB=true';
        exit();
    }
}

//run
$controller->dispatch();