<?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
* 'flow' => (array[mixed]) current flow data, including variables
* ]
*
* @return array $data Structure as above, plus 'logs' => (array[string]) Logs to be written to flow run log after script execution
*/
/**
* Get next page url from link header string
*/
function getNext(string $linkHeaderString): string|null {
if (!str_contains($linkHeaderString, 'rel="next"')) {
return null;
}
$stringParts = explode(',', $linkHeaderString);
$nextLinkString = count($stringParts) === 1 ? $stringParts[0] : $stringParts[1];
$matches = [];
preg_match_all('/<(.+)>;\s?rel="([A-z]*)"/', $nextLinkString, $matches);
return $matches[1][0];
}
function handle($data)
{
$payload = json_decode($data['payload'], true);
$returnPayload = [
'has_next_page' => false,
'method' => 'GET'
];
if (array_key_exists('response', $payload)) {
if (array_key_exists('headers', $payload['response'])) {
if (array_key_exists('link', $payload['response']['headers'])) {
/** check if the header contains the next page link
Header will be in the format:
<https://custom-peaks-1.myshopify.com/admin/api/2023-04/customers.json?limit=50&page_info=eyJkaXJlY3Rpb24iOiJwcmV2IiwibGFzdF9pZCI6NzIyMjM0MzA0MTE4MiwibGFzdF92YWx1ZSI6MTcwMzE2NDgxMDAwMH0>; rel="previous", <https://custom-peaks-1.myshopify.com/admin/api/2023-04/customers.json?limit=50&page_info=eyJkaXJlY3Rpb24iOiJuZXh0IiwibGFzdF9pZCI6Njg2MjY2MjUzMzI3OCwibGFzdF92YWx1ZSI6MTY4NjM1MDU1OTAwMH0>; rel="next"
*/
$links = $payload['response']['headers']['link'][0];
$nextPage = getNext($links);
if ($nextPage) {
$returnPayload['has_next_page'] = true;
$returnPayload['url'] = $nextPage;
$returnPayload['body'] = $payload['request']['body'];
$returnPayload['headers'] = $payload['request']['headers'];
}
}
}
}
return [
'payload' => json_encode($returnPayload)
];
}