Avatar of CalmSoul
CalmSoul
Flag for United States of America asked on

keep filling the array with URL until false

I have the following code - I would like to keep filling the array with URL https://api.dailymotion.com/user/cricshare/videos?page=1&limit=100 until JSON response"has_more":false

For example:
First URL:
https://api.dailymotion.com/user/cricshare/videos?page=1&limit=100 => "has_more":true
put URL in array and get next URL
Second URL:
https://api.dailymotion.com/user/cricshare/videos?page=2&limit=100 => "has_more":true
put URL in array and get next URL
Third URL:
https://api.dailymotion.com/user/cricshare/videos?page=2&limit=100 => "has_more":false
don't get any URL finish the array as "has_more":false

<?php


error_reporting(E_ALL);

$urls = array(
    'https://api.dailymotion.com/user/cricshare/videos?page=1&limit=100',
    'https://api.dailymotion.com/user/cricshare/videos?page=2&limit=100',
    'https://api.dailymotion.com/user/cricshare/videos?page=3&limit=100'
);

$sets = array();
foreach ($urls as $url) {
    $json   = file_get_contents($url);
    $data   = json_decode($json);
    $sets[] = $data;
}

$new = json_encode($sets, JSON_PRETTY_PRINT);
echo '<pre>';
echo $new;

?>

Open in new window

PHP

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
SOLUTION
Phil Phillips

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Ray Paseur

Variation on Phil_Phillips solution (which works very well)...

This returns only the "lists" from each of the URLs.  The return value is one long array that merges the lists.

<?php // demo/temp_phil.php

/**
 * See http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28635433.html
 */
error_reporting(E_ALL);

$sets = array();
$page = 1;
do
{
    $url = sprintf("https://api.dailymotion.com/user/cricshare/videos?page=%d&limit=100", $page);
    $data = json_decode(file_get_contents($url));
    $hasMore = $data->has_more;
    $sets = array_merge($sets, $data->list);
    $page++;
} while ($hasMore);

$new = json_encode($sets, JSON_PRETTY_PRINT);
echo '<pre>';
echo $new;

Open in new window

CalmSoul

ASKER
Thanks Phil and Ray!!

How about if I need to do this ? Getting for multi accounts

$useraccounts = array("cricshare", "cricshare1", "cricshare2");


$url = sprintf("https://api.dailymotion.com/user/$useraccounts/videos?page=%d&limit=100", $page);

Open in new window

ASKER CERTIFIED SOLUTION
Ray Paseur

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck