This is the legacy 4D documentation web site. Documentations are progressively being moved to developer.4d.com

Home

 
4D v20 R7
PHP
Executing PHP scripts in 4D

Executing PHP scripts in 4D  


 

 

4D lets you directly execute PHP scripts. This possibility gives you access to the wealth of utility libraries available via PHP. More particularly, these libraries provide the following functions (this list is not exhaustive):

  • ciphering (MD5) and hashing,
  • handling of ZIP files,
  • handling of pictures,
  • LDAP access,
  • COM access (control of MS Office documents), etc.

To execute a PHP script or function, you can use the PHP Execute command or system workers.

For a complete description of PHP commands and syntax, please refer to the abundant PHP documentation available on the Internet. As an example, here are the addresses for a few reference sites:

http://us.php.net/manual/en/

http://phpdeveloper.org/

To use PHP with 4D, you need to use a PHP interpreter. The PHP interpreter must respect two conditions:

  • It must be compiled in FastCGI,
  • It must be located on the same machine as 4D.

You need to configure the interpreter so that it listens to a specific address and TCP port. These parameters can be specified either via database settings, or for the session via the SET DATABASE PARAMETER command. Of course, you must manage the starting and functioning of the interpreter yourself. 

To download and install a PHP interpreter, you have two possibilities:

  • Download PHP from the official PHP website. On Windows, you can select your version. On macOS, you have to compile your own version, or find a pre-built version on other sites.
  • Download PHP for macOS and Windows from the 4D product download page. Note that this version, used in 4D up to 4D v20 R2, is no longer maintained.

For detailed information about downloading, compiling, and installing PHP on Windows or macOS, refer to the following 4D blog post: Deprecation of PHP commands and removal of 4D built-in PHP interpreter.

The php.ini initialization file is placed in the Resources folder of the project. The php.ini file can be used, more particularly, to declare the location of the PHP extensions. If this file is not present during the first call to PHP, 4D will create it with default configuration options.

If you do not use the default 4D php.ini file, you need to make sure that the php.ini you use contains the following entries:

  • auto_prepend_file which provides the complete pathname to the 4D_Execute_PHP.php utility script. This script is found in [4D application]Resources/php/Windows or /Mac. Without this entry, only complete scripts can be executed: calls to a routine inside a script will not work.
  • display_errors sets a "stderr" so that 4D can be informed when an error occurs during the execution of PHP code. Example:
    ; stderr - Display the errors to STDERR (only affects the CGI/CLI)
    ; To direct the errors to STDERR for the CGI/CLI:
    display_errors = "stderr"

Note that the current version on the official PHP website does not load the mbstring package by default (which is required by 4D). If you use it, you might need to add these lines to the php.ini file:

extension_dir = "./ext/"
extension=mbstring

Note about timeout

If the duration of PHP processing is relatively long (beyond 30 seconds), by default a 'timeout' error will be returned in 4D and the processing will fail. In this case, you can set the default timeout in order to allocate more time to PHP execution. There are two ways to do this:

  • by setting the max_execution_time variable in the php.ini file (pass a value in seconds). Warning: this setting affects all the scripts.
  • by calling the set_time_limit(nbSec) command in the PHP execution script that is performing the long processing. Pass the maximum duration allocated to the execution of the PHP script in nbSec. We recommend using this setting since it only affects this script. Usually, for security reasons, it is preferable to keep a lower timeout value for PHP scripts.



See also 

PHP Execute

 
PROPERTIES 

Product: 4D
Theme: PHP

 
PAGE CONTENTS 
 
HISTORY 

Modified: 4D v20 R3

 
ARTICLE USAGE

4D Language Reference ( 4D v20 R7)