PHP

From DreamFactory
Jump to: navigation, search
Line 1: Line 1:
We support writing event scripts and custom scripting service scripts in PHP. These are not sandboxed like V8js so care must be taken to prevent unwanted access to system resources, e.g, the file system of the host server that is running DreamFactory. Please see our [[DreamFactory/Tutorials#PHP|example scripts]] for PHP for more information.
+
DreamFactory supports writing event scripts and custom scripting service scripts in PHP. '''Note:''' These scripts are not sandboxed like V8js so care must be taken to prevent unwanted access to system resources, e.g, the file system of the host server that is running DreamFactory. Please see our [[DreamFactory/Tutorials#PHP|example scripts]] for PHP for more information. No additional software is required to run PHP scripts.
 +
 
 +
== Accessing Resources ==
 +
 
 +
DreamFactory passes in two [[DreamFactory/Features/Scripting#Resources Available To A Script|additional objects]] for use in the scripts.
 +
In PHP, these resources are represented as native arrays and can be accessed as normal. See the examples below.
 +
 
 +
Using the '''event''' resource...
 +
<source lang="php">
 +
$result = $platform['api']->get->__invoke('system/environment');
 +
</source>
 +
 
 +
Using the '''platform''' resource...
 +
<source lang="php">
 +
$verb = $event['request']['method'];
 +
 
 +
if ($verb !== 'GET') {
 +
    // currently always throws a 500 to client
 +
    throw new \Exception('Only HTTP GET is allowed on this endpoint');
 +
}
 +
 
 +
$params = $event['request']['parameters'];
 +
$required = ['n1', 'n2'];
 +
 
 +
foreach ($required as $element) {
 +
    if (!isset($params[$element])) {
 +
        $event['response'] = [
 +
            'status_code' => 400,
 +
            'content' => [
 +
                'success' => false,
 +
                'message' => "Required parameter $element not found in request."
 +
            ]
 +
        ];
 +
        return;
 +
    }
 +
}
 +
 
 +
$result = $params['n1']+$params['n2'];
 +
 
 +
// status_code defaults to 200
 +
return [
 +
    'success' => true,
 +
    'result' => $result
 +
];
 +
</source>
 +
 
 +
 
 +
== Including Other Scripts ==
 +
 
 +
Currently, PHP scripts can not directly include other PHP code via ''require()'' methods or ''use'' statements as you normally would. However, DreamFactory can access any other code installed in the system by using fully qualified name-spaced class names.

Revision as of 17:04, 23 February 2016

DreamFactory supports writing event scripts and custom scripting service scripts in PHP. Note: These scripts are not sandboxed like V8js so care must be taken to prevent unwanted access to system resources, e.g, the file system of the host server that is running DreamFactory. Please see our example scripts for PHP for more information. No additional software is required to run PHP scripts.

Accessing Resources

DreamFactory passes in two additional objects for use in the scripts. In PHP, these resources are represented as native arrays and can be accessed as normal. See the examples below.

Using the event resource...

$result = $platform['api']-&gt;get-&gt;__invoke('system/environment');

Using the platform resource...

$verb = $event['request']['method'];
 
if ($verb !== 'GET') {
    // currently always throws a 500 to client
    throw new \Exception('Only HTTP GET is allowed on this endpoint');
}
 
$params = $event['request']['parameters'];
$required = ['n1', 'n2'];
 
foreach ($required as $element) {
    if (!isset($params[$element])) {
        $event['response'] = [
            'status_code' =&gt; 400, 
            'content' =&gt; [
                'success' =&gt; false,
                'message' =&gt; "Required parameter $element not found in request."
            ]
        ];
        return;
    }
}
 
$result = $params['n1']+$params['n2'];
 
// status_code defaults to 200 
return [
    'success' =&gt; true,
    'result' =&gt; $result
];


Including Other Scripts

Currently, PHP scripts can not directly include other PHP code via require() methods or use statements as you normally would. However, DreamFactory can access any other code installed in the system by using fully qualified name-spaced class names.