5

I'm querying an API for a list of results, using PHP and cURL.

In order to enable pagination, the API returns links to the next and previous pages of results, in the header, as HTTP Link Headers. https://www.w3.org/wiki/LinkHeader

When I print_r the object I get back from curl_exec, the links in the header (between angled brackets) are empty. I verify that I should be being returned links, by looking in the Swagger docs. (see example). In the Swagger docs, the links are returned within angle brackets - is there something about the way cURL is getting/returning the headers that means content within angle brackets is lost?

Source code:

$curl = curl_init("https://api.mendeley.com/datasets?limit=3");
curl_setopt($curl, CURLOPT_HEADER, 1); 
curl_setopt($ch,CURLOPT_ENCODING , "gzip");
$rest = curl_exec($curl);
print_r($rest);

Expected output (from Swagger docs):

"link": "<https://api.mendeley.com/datasets?marker=0027ea1f-bec6-4581-a967-9a8c75dfba5f&limit=3&fields=results.id&reverse=false&_=1505585347125&order=asc>; rel=\"next\"",

Actual output:

Link: ; rel="next" 
Michael Jones
  • 80
  • 1
  • 7
  • See this answer: https://stackoverflow.com/a/41135574/2872038 – Joshua Jones Sep 16 '17 at 18:43
  • If the header value is actually empty, I would guess there are no subsequent result sets. – Joshua Jones Sep 16 '17 at 18:44
  • Thanks for the help on this - I tried this and it nicely puts each header into an array, making much easier to use, thankyou! But unfortunately the pagination links are still blank (I'm getting the first 3 of a list of ~2000 results, and doing the same query in Swagger I do get pagination links back). 'code' [link] => Array ( [0] => ; rel="next" ) – Michael Jones Sep 17 '17 at 08:47
  • Perhaps the fact the links are within angle brackets means they aren't coming through correctly? Do I need to set the curl command to get the object in a different format maybe; or escape its output? When I use json_decode the whole object comes out blank. Thanks! – Michael Jones Sep 17 '17 at 08:49
  • Are you viewing this "actual output" in web browser? – yergo Oct 08 '19 at 19:41

2 Answers2

0

Ok I got there - In order to preserve the URLs in the headers, contained within angle brackets, I needed to URL encode the output from curl_exec:

$rest = rawurlencode(curl_exec($ch));

Example output:

Link%3A%20%3Chttps%3A%2F%2Fapi.mendeley.com%2Fdatasets%3Fmarker%3Db6ea8b2a-c4fa-4a84-989a-66cf7801abbf%26limit%3D3%26sort%3Dpublish_date%26fields%3Dresults.id%26reverse%3Dfalse%26order%3Ddesc
Michael Jones
  • 80
  • 1
  • 7
0

having code like this:

$content = <<<HTML
Link: <meta.rdf>; rel=meta

Content.
HTML;

print_r($content);

shows in browser exactly:

Link: ; rel=meta Content.

because things within angle brackets are considered to be tags and are not rendered. If you use "crtl+u" shortcut to view sourcecode, you will see:

Link: <meta.rdf>; rel=meta

Content.

And this means that everything is working fine, and you have everything you need.

I bet that it's your case.

yergo
  • 4,761
  • 2
  • 19
  • 41