PHP

From DreamFactory
Jump to: navigation, search

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 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...

$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
];

Using the platform resource...

$payload = ['resource' => [['name' => 'test', 'complete' => false]]];
$result = $platform['api']->post->__invoke('mysql/_table/todo', $payload);

Transforming Response Structures

Using a post_process event handler, it's possible to transform data structures in any imaginable way. The following example iterates over a response, renaming the emp_no field to employee_number, and placing the first_name and last_name fields under a parent name property:

$responseBody = $event['response']['content'];

foreach ($responseBody['resource'] as $n => $record) {
    
    $record['employee_number'] = $record['emp_no'];
    
    // Nest a few elements
    $record['name'] = [];
    
    $record['name']['first_name'] = $record['first_name'];
    $record['name']['last_name']  = $record['last_name'];
    
    $responseBody['resource'][$n] = $record;
    
    // Remove unwanted elements
    unset($responseBody['resource'][$n]['first_name']);
    unset($responseBody['resource'][$n]['last_name']);
    unset($responseBody['resource'][$n]['emp_no']);
    
}

$event['response']['content'] = $responseBody;

Calling Other APIs

The following example demonstrates iterating over response content and for each returned status message, calling IBM Watson to translate the status from English to Spanish:

$api = $platform['api'];
$options = [];

$responseBody = $event['response']['content'];

foreach ($responseBody['resource'] as $n => $record) {
    
    $statusUpdates = [];
    
    if(isset($record['status_history'])) {
    
        foreach($record['status_history'] as $sh) {
        
           $translate = [];
        
           $translate['text'][] = $sh['STATUS'];
           $translate['source'] = "English";
           $translate['target'] = "Spanish";
    
           $payload = json_encode($translate);
        
           $url = "watson/";
           $post = $api->post;
           $result = $post($url, $payload, $options);    
         
           $sh['STATUS'] = $result['content']['translations'][0]['translation'];
    
           $statusUpdates[] = $sh;
    
        }
    
    }

    $record['status_history'] = $statusUpdates;
    
    $responseBody['resource'][$n] = $record;
    
}

$event['response']['content'] = $responseBody;

Including Other Scripts

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