How to loop through Json pages with a cursor or pagination key











up vote
0
down vote

favorite












I am working with a API which gives me a 100 names per json page, along with that it gives me a cursor which has a pagination:key to go through the other pages and check another 100 names with another pagination:key and e.t.c... The API will always give you another pagination and key in each page even if you reach the end of the "names". So the array with the "names" will look like data. My problem/question Is there a way to loop through these pages This is my code so far:



$channelsApi = 'https://api.twitch.tv/helix/users/follows?to_id='.$rid.'&first=100';
$channelName = '';
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer ".$_GET['fragmentaccess_token'].""
),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $channelsApi . $channelName
)
);
$response = curl_exec($ch);
curl_close($ch);
// echo $response;
$json = json_decode($response, true);


I was able to make it through other pages if i simply did



foreach ($json['pagination'] as $curs){
if(isset($curs) && $json['data'] != NULL){
//$connection = new mysqli("localhost","root","","twitchun");
$channelsApi = 'https://api.twitch.tv/helix/users/follows?
to_id='.$rid.'&first=100&after='.$curs;


But i do not want to copy paste my code a million times if i have to go through a million names.



I have seen other posts where they use :do while: in php, but I was a little confused to how exactly to do that!



I am really in a dead end, so any advice is a great help for me!



EDIT : This is how the json response looks like



flyyrossi{"total":26,
"data":[{"from_id":"262922764",
"from_name":"goddessmiseria",
"to_id":"228906764",
"to_name":"BaychevLive",
"followed_at":"2018-11-19T00:37:21Z"},
{"from_id":...

...,"pagination":{"cursor":"eyJiIjpudWxsLCJhIjp7IkN1cnNblahblahblah"}}


To access the next I need the $channelsApi + the pagination in the json to go to the next page with "from_name".



My Overall problem is I want to get All of the "from_name"s.










share|improve this question




















  • 3




    It is really not clear what your problem is here. What are you trying to process from the json, can you show at least a small example
    – RiggsFolly
    Nov 19 at 17:19












  • Is it clear after the edit ?
    – Daniel Baychev
    Nov 19 at 18:02















up vote
0
down vote

favorite












I am working with a API which gives me a 100 names per json page, along with that it gives me a cursor which has a pagination:key to go through the other pages and check another 100 names with another pagination:key and e.t.c... The API will always give you another pagination and key in each page even if you reach the end of the "names". So the array with the "names" will look like data. My problem/question Is there a way to loop through these pages This is my code so far:



$channelsApi = 'https://api.twitch.tv/helix/users/follows?to_id='.$rid.'&first=100';
$channelName = '';
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer ".$_GET['fragmentaccess_token'].""
),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $channelsApi . $channelName
)
);
$response = curl_exec($ch);
curl_close($ch);
// echo $response;
$json = json_decode($response, true);


I was able to make it through other pages if i simply did



foreach ($json['pagination'] as $curs){
if(isset($curs) && $json['data'] != NULL){
//$connection = new mysqli("localhost","root","","twitchun");
$channelsApi = 'https://api.twitch.tv/helix/users/follows?
to_id='.$rid.'&first=100&after='.$curs;


But i do not want to copy paste my code a million times if i have to go through a million names.



I have seen other posts where they use :do while: in php, but I was a little confused to how exactly to do that!



I am really in a dead end, so any advice is a great help for me!



EDIT : This is how the json response looks like



flyyrossi{"total":26,
"data":[{"from_id":"262922764",
"from_name":"goddessmiseria",
"to_id":"228906764",
"to_name":"BaychevLive",
"followed_at":"2018-11-19T00:37:21Z"},
{"from_id":...

...,"pagination":{"cursor":"eyJiIjpudWxsLCJhIjp7IkN1cnNblahblahblah"}}


To access the next I need the $channelsApi + the pagination in the json to go to the next page with "from_name".



My Overall problem is I want to get All of the "from_name"s.










share|improve this question




















  • 3




    It is really not clear what your problem is here. What are you trying to process from the json, can you show at least a small example
    – RiggsFolly
    Nov 19 at 17:19












  • Is it clear after the edit ?
    – Daniel Baychev
    Nov 19 at 18:02













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am working with a API which gives me a 100 names per json page, along with that it gives me a cursor which has a pagination:key to go through the other pages and check another 100 names with another pagination:key and e.t.c... The API will always give you another pagination and key in each page even if you reach the end of the "names". So the array with the "names" will look like data. My problem/question Is there a way to loop through these pages This is my code so far:



$channelsApi = 'https://api.twitch.tv/helix/users/follows?to_id='.$rid.'&first=100';
$channelName = '';
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer ".$_GET['fragmentaccess_token'].""
),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $channelsApi . $channelName
)
);
$response = curl_exec($ch);
curl_close($ch);
// echo $response;
$json = json_decode($response, true);


I was able to make it through other pages if i simply did



foreach ($json['pagination'] as $curs){
if(isset($curs) && $json['data'] != NULL){
//$connection = new mysqli("localhost","root","","twitchun");
$channelsApi = 'https://api.twitch.tv/helix/users/follows?
to_id='.$rid.'&first=100&after='.$curs;


But i do not want to copy paste my code a million times if i have to go through a million names.



I have seen other posts where they use :do while: in php, but I was a little confused to how exactly to do that!



I am really in a dead end, so any advice is a great help for me!



EDIT : This is how the json response looks like



flyyrossi{"total":26,
"data":[{"from_id":"262922764",
"from_name":"goddessmiseria",
"to_id":"228906764",
"to_name":"BaychevLive",
"followed_at":"2018-11-19T00:37:21Z"},
{"from_id":...

...,"pagination":{"cursor":"eyJiIjpudWxsLCJhIjp7IkN1cnNblahblahblah"}}


To access the next I need the $channelsApi + the pagination in the json to go to the next page with "from_name".



My Overall problem is I want to get All of the "from_name"s.










share|improve this question















I am working with a API which gives me a 100 names per json page, along with that it gives me a cursor which has a pagination:key to go through the other pages and check another 100 names with another pagination:key and e.t.c... The API will always give you another pagination and key in each page even if you reach the end of the "names". So the array with the "names" will look like data. My problem/question Is there a way to loop through these pages This is my code so far:



$channelsApi = 'https://api.twitch.tv/helix/users/follows?to_id='.$rid.'&first=100';
$channelName = '';
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer ".$_GET['fragmentaccess_token'].""
),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $channelsApi . $channelName
)
);
$response = curl_exec($ch);
curl_close($ch);
// echo $response;
$json = json_decode($response, true);


I was able to make it through other pages if i simply did



foreach ($json['pagination'] as $curs){
if(isset($curs) && $json['data'] != NULL){
//$connection = new mysqli("localhost","root","","twitchun");
$channelsApi = 'https://api.twitch.tv/helix/users/follows?
to_id='.$rid.'&first=100&after='.$curs;


But i do not want to copy paste my code a million times if i have to go through a million names.



I have seen other posts where they use :do while: in php, but I was a little confused to how exactly to do that!



I am really in a dead end, so any advice is a great help for me!



EDIT : This is how the json response looks like



flyyrossi{"total":26,
"data":[{"from_id":"262922764",
"from_name":"goddessmiseria",
"to_id":"228906764",
"to_name":"BaychevLive",
"followed_at":"2018-11-19T00:37:21Z"},
{"from_id":...

...,"pagination":{"cursor":"eyJiIjpudWxsLCJhIjp7IkN1cnNblahblahblah"}}


To access the next I need the $channelsApi + the pagination in the json to go to the next page with "from_name".



My Overall problem is I want to get All of the "from_name"s.







php json api loops pagination






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 17:31









RiggsFolly

68.9k1764109




68.9k1764109










asked Nov 19 at 17:06









Daniel Baychev

227




227








  • 3




    It is really not clear what your problem is here. What are you trying to process from the json, can you show at least a small example
    – RiggsFolly
    Nov 19 at 17:19












  • Is it clear after the edit ?
    – Daniel Baychev
    Nov 19 at 18:02














  • 3




    It is really not clear what your problem is here. What are you trying to process from the json, can you show at least a small example
    – RiggsFolly
    Nov 19 at 17:19












  • Is it clear after the edit ?
    – Daniel Baychev
    Nov 19 at 18:02








3




3




It is really not clear what your problem is here. What are you trying to process from the json, can you show at least a small example
– RiggsFolly
Nov 19 at 17:19






It is really not clear what your problem is here. What are you trying to process from the json, can you show at least a small example
– RiggsFolly
Nov 19 at 17:19














Is it clear after the edit ?
– Daniel Baychev
Nov 19 at 18:02




Is it clear after the edit ?
– Daniel Baychev
Nov 19 at 18:02

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379514%2fhow-to-loop-through-json-pages-with-a-cursor-or-pagination-key%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53379514%2fhow-to-loop-through-json-pages-with-a-cursor-or-pagination-key%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'