Response scripts are just like any other custom script, except they receive additional information from the request - see lines 11 to 14 in the example below:
<?php/** * Handler function. * * @paramarray $data [ * 'payload' => (string|null) the payload as a string|null * 'variables' => (array[string]string) any variables as key/value * 'meta' => (array[string]string) any meta as key/value * 'flow' => (array[mixed]) current flow data, including variables * 'response' => [ * 'headers' => ['Content-Type' => 'application/json', .......], * 'body' => '....', * 'status' => 200 * ] * ] * * @return array $data Structure as above, plus 'logs' => (array[string]) Logs to be written to flow run log after script execution
*/functionhandle($data){return $data;}
These options are only valid when the script is applied to a connector step as a response script.
Response code
The response_code determines how the process flow behaves if a connection request fails. Supported response_code values are:
Value
Notes
0
Continue
1
Fail the connector step and retry. The connector step is marked as failed and the queue will attempt it again.
2
Fail the process flow and queue it to retry. The process flow is marked as failed and queued for a retry.
3
Fail the process flow and do not retry.
Message
The message is optional. If supplied, it is output in the run logs.
Example
functionhandle($data){// Stops the flow with a message $data['response_code'] =3 $data['message'] ='Flow stopped by response script';return $data;}
Step 2: Apply the response script
To apply your response script, access settings for the required connector shape and select your script from the response script dropdown field.
Examples
Scenario 1
Here we handle the scenario where a connection response appears OK because the status code received is 200, but in fact the response body includes a string (Invalid session) which contradicts this. So, when this string is found in the response body, we want to retry the process flow.
In this case we return a response_code of 2 with an message of Invalid session:
<?php/** * Handler function. * * @paramarray $data [ * 'payload' => (string|null) the payload as a string|null * 'variables' => (array[string]string) any variables as key/value * 'meta' => (array[string]string) any meta as key/value *. 'response' => [ * 'headers' => ['Content-Type' => 'application/json', .......], *. 'body' => '....', * 'status' => 200 * ] * ] */functionhandle($data){ handle invalid session error in PVX $data['response']['status'] =200; $data['response']['body'] ='Invalid session';if (str_contains($data['response']['body'],'Invalid session')) {return ['response_code'=>2// retry flow'message'=>'Invalid session' ]; }
Scenario 2
Here we show how the payload received from a connection request is checked for an order number and an order status - retrying the the process flow if a particular order status is found:
<?php/** * Handler function. * * @paramarray $data [ * 'payload' => (string|null) the payload as a string|null * 'variables' => (array[string]string) any variables as key/value * 'meta' => (array[string]string) any meta as key/value *. 'response' => [ * 'headers' => ['Content-Type' => 'application/json', .......], *. 'body' => '....', * 'status' => 200 * ] * ] */// check if order is ready to be processed yet $data['payload'] = ['order_id'=>1,'status'=>'Pending', ]if ($data['payload']['status'] ==='Pending') {return ['response_code'=>2,// retry flow'message'=>'Order not ready for processing, adding flow back to queue' ] }