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.
php json api loops pagination
add a comment |
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.
php json api loops pagination
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
add a comment |
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.
php json api loops pagination
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
php json api loops pagination
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
add a comment |
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
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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