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

LVL 5
CalmSoulAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Phil PhillipsDevOps ArchitectCommented:
To just populate the array, you can do something like:

$urls = 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;
   $urls[] = $url;
   $page++;
} while ($hasMore);

Open in new window



Though, judging from your script, you might want something like this instead:

<?php

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[] = $data;
   $page++;
} while ($hasMore);

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

?>

Open in new window

Ray PaseurCommented:
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

CalmSoulAuthor Commented:
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

Ray PaseurCommented:
You would probably want to use two nested loops, probably foreach() or while() loops.  You're also getting into the area of complexity where you may need to consider error handlers, in case some of the URLs do not respond with the right data.  It's not anything "hard," just step-by-step application development.
<?php // demo/temp_phil.php

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

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

foreach ($useraccounts as $useracct)
{
    $page = 1;
    do
    {
        $url = "https://api.dailymotion.com/user/$useracct/videos?page=$page&limit=100";
        echo PHP_EOL . "READING $url";
        $jso = file_get_contents($url);
        if (!$jso) trigger_error("READ FAILURE: $url", E_USER_ERROR);
        $data = json_decode($jso);
        if (!$data) trigger_error("JSON FAILURE: $url", E_USER_ERROR);
        if (!$data->list)
        {
            var_dump($data);
            trigger_error("DATA FAILURE: $url", E_USER_ERROR);
        }
        $sets = array_merge($sets, $data->list);
        $hasMore = $data->has_more;
        if (!$hasMore)
            break;
        $page++;
    } while ($hasMore);
}

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

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.