Plytix post-request script
Last updated
Was this helpful?
Was this helpful?
Plytix Post-Request ScriptPlytix authorisation post-request script (retrieve token)<?php
/**
* Handler function.
*
* @param array $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
* ]
*/
function handle($data)
{
$curl = curl_init();
$body = [
'api_key' => $data['variables']['api_key'],
'api_password' => $data['variables']['api_password'],
];
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://auth.plytix.com/auth/api/get-token',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => json_encode($body),
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json'
),
));
curl_close($curl);
$response = curl_exec($curl);
if ($response === false) {
$error = 'cURL Error: ' . curl_error($curl);
echo $error;
throw new Exception($error);
}
$responseData = json_decode($response, true);
if ($responseData === null) {
$error = 'JSON decoding error: ' . json_last_error_msg();
echo $error;
throw new Exception($error);
}
$data['variables']['access_token'] = $responseData['data'][0]['access_token'];
return $data;
}