Recursive function in PHP function : how to prevent return value?

624 views Asked by At

I am working on an API to get data, just like the other standard API, it limit on the return numbers of item , e.g. each page return only 1 item in my case. So , I have to get the

  1. total_count,
  2. page_size (it is 1 at my case),
  3. and current_page_number.

The problem is, no matter I set it to recursive , it still return the value at the first cycle. e.g. If there is total 3 items, and limit to 1 item per page, it will return the value during the 1st loop.

Why and how to fix that?

Thanks a lot

function get_video_list($page_no = 0, $filter_video = array()) {
    $api = "http://api.brightcove.com/services/library?command=search_videos&page_size=1&video_fields=id%2Cname%2CcreationDate%2CFLVURL%2CpublishedDate%2ClinkURL%2CthumbnailURL%2Clength&media_delivery=http&sort_by=CREATION_DATE%3AASC&page_number=$page_no&get_item_count=true&token=" . READ_TOKEN;

    try {
        $ch = curl_init();

        if (FALSE === $ch) {
            throw new Exception('failed to initialize');
        }

        curl_setopt($ch, CURLOPT_URL, $api);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $content = curl_exec($ch);

        if (FALSE === $content) {
            throw new Exception(curl_error($ch), curl_errno($ch));
        }

        $video_list = json_decode($content, false, 512, JSON_BIGINT_AS_STRING);

        $page_number = ($video_list->page_number) + 1; //start at 0
        $page_size = $video_list->page_size;
        $total_count = $video_list->total_count;

        foreach ($video_list->items as $video) {
            if (in_array($video->id, $stored_video)) {
                $filter_video [] = $video;
            }
        }

        if ($total_count > $page_size * $page_number) {
            get_video_list($page_number, $filter_video);
        }

        return $filter_video;
    } catch (Exception $e) {
        trigger_error(sprintf('Curl failed with error #%d: %s', $e->getCode(), $e->getMessage()), E_USER_ERROR);
    }
}
2

There are 2 answers

0
RuubW On BEST ANSWER

You are not assigning the return value of the recursive call back to $filter_video, change to:

if ($total_count > $page_size * $page_number) {
    $filter_video = get_video_list($page_number, $filter_video);
}

Or, even better: pass by reference. This eliminates the need for return values altogether, especially useful for recursive functions (notice &$filter_video in the function declaration, see http://php.net/manual/en/language.references.pass.php).

function get_video_list($page_no = 0, &$filter_video = array()) {
    $api = "http://api.brightcove.com/services/library?command=search_videos&page_size=1&video_fields=id%2Cname%2CcreationDate%2CFLVURL%2CpublishedDate%2ClinkURL%2CthumbnailURL%2Clength&media_delivery=http&sort_by=CREATION_DATE%3AASC&page_number=$page_no&get_item_count=true&token=" . READ_TOKEN;

    try {
        $ch = curl_init();

        if (FALSE === $ch) {
            throw new Exception('failed to initialize');
        }

        curl_setopt($ch, CURLOPT_URL, $api);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $content = curl_exec($ch);

        if (FALSE === $content) {
            throw new Exception(curl_error($ch), curl_errno($ch));
        }

        $video_list = json_decode($content, false, 512, JSON_BIGINT_AS_STRING);

        $page_number = ($video_list->page_number) + 1; //start at 0
        $page_size = $video_list->page_size;
        $total_count = $video_list->total_count;

        foreach ($video_list->items as $video) {
            if (in_array($video->id, $stored_video)) {
                $filter_video [] = $video;
            }
        }

        if ($total_count > $page_size * $page_number) {
            get_video_list($page_number, $filter_video);
        }
    } catch (Exception $e) {
        trigger_error(sprintf('Curl failed with error #%d: %s', $e->getCode(), $e->getMessage()), E_USER_ERROR);
    }
}

Then call it like this:

$myVideoList = [];
get_video_list(0, $myVideoList);

// Do stuff with $myVideoList
0
Unex On

this should resolve your problem.

if ($total_count > $page_size * $page_number) {
      return get_video_list($page_number, $filter_video);
}