1

I am pulling a response from an API using PHP to display a list of content on my WordPress website from an external service. The way they have their API set up, I need to create a loop that checks if there is a "next page" of data that I can access. The API returns a header item called "Links", with the following content:

<https://external.service.com/myusername/api/opportunities>; rel="first",<https://external.service.com/myusername/api/opportunities?before=512a7905-65a3-4845-bb8f-d2363c9e1d95>; rel="prev",<https://external.service.com/myusername/api/opportunities?after=2ab72e09-82d9-4c80-a3bb-4a2fea248695>; rel="next"

I can use the "Next" link to call the API again, which gives me the next page of data. A bit odd, but that's the way they have their API set up...not much I can do about it.

I'm assuming the best way to do this is by using a regular expression to copy the "Next" link to a variable, and create a loop which calls the link stored in the variable, updating it each time. I'm having trouble coming up with the regular expression that could do this, though. Is it possible to do without it catching the other three links as well?

2 Answers 2

0

The good news (especially for me, since regEx is hard) is that it can be done without regex. Using my favourite antique function for extracting substrings by Justin Cook

It seems to me that all you actually need to grab is the url parameter of the final url in the string. To that end I use this function to grab everything after "after=" and before ">", then simply reassemble into the final link.

There are other approaches including exploding the string and doing some replacements on the results so you get all the links, but you stated you are only looking for the last one

$fullstring = '<https://external.service.com/myusername/api/opportunities>; rel="first",<https://external.service.com/myusername/api/opportunities?before=512a7905-65a3-4845-bb8f-d2363c9e1d95>; rel="prev",<https://external.service.com/myusername/api/opportunities?after=2ab72e09-82d9-4c80-a3bb-4a2fea248695>; rel="next"';

function get_string_between($string, $start, $end){
  $string = ' ' . $string;
  $ini = strpos($string, $start);
  if ($ini == 0) return '';
  $ini += strlen($start);
  $len = strpos($string, $end, $ini) - $ini;
  return substr($string, $ini, $len);
}

$parameter = get_string_between($fullstring, 'after=', '>');

$finalUrl = "https://external.service.com/myusername/api/opportunities?after=".$parameter;

echo $finalUrl;
0

If you want to get the prev, next, or both URLs, you can do the following

/(?<=rel="(prev|next)"),<([^>]+)/g

You can check what it will match here https://regex101.com/r/WnuhdG/1

Without diving too deep into regex it looks for content the is between <> that before it exists rel="prev", or rel="next",

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.