I've made a code search application that interacts with GitHubs API, that i want to add pagination to, pagination data is held in the header like so:
Link: <https://api.github.com/user/repos?page=3&per_page=100>; rel="next",
<https://api.github.com/user/repos?page=50&per_page=100>; rel="last"
My code:
// API CONNECTION
$url = 'https://api.github.com/search/code?q=' . $term . '+language:' . $lang . '&per_page=' . $pp;
$cInit = curl_init();
curl_setopt($cInit, CURLOPT_URL, $url);
curl_setopt($cInit, CURLOPT_RETURNTRANSFER, 1); // 1 = TRUE
curl_setopt($cInit, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($cInit, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($cInit, CURLOPT_USERPWD, $user . ':' . $pwd);
curl_setopt($cInit, CURLOPT_HTTPHEADER, array('Accept: application/vnd.github.v3.text-match+json')); // ADD THE HIGHLIGHTED CODE SECTION
// MAKE CURL OUTPUT READABLE
$output = curl_exec($cInit);
$items = json_decode($output, true);
curl_close($cInit); // CLOSE OUR API CONNECTION
Now, i've added in curl_setopt($cInit, CURLOPT_HEADER, true);
And now, for whatever reason - when i do var_dump($items)
which worked before i added CURLOPT_HEADER
to my code - instead returns a NULL
. Which in turn breaks the entire project.
Doing some debugging i found that var_dump($output)
is still outputting data, and as expected has the header included. However, the Link Header
looks like this:
Link: ; rel="next", ; rel="last"
When it shouldnt. To my knowledge, it looks like the link header has actually broken my code.
I've tried various things like trying to urlencode
$output
before i decode it, but to no avail. So, how do i fix this?