【发布时间】:2015-08-23 21:26:53
【问题描述】:
我正在开发一个 API 来获取数据,就像其他标准 API 一样,它限制了 item 的返回数,例如在我的情况下,每页仅返回 1 项。所以,我必须得到
- total_count,
- page_size(在我的情况下是 1),
- 和 current_page_number。
问题是,无论我将其设置为 recursive ,它仍然会在第一个周期返回值。例如如果总共有 3 个项目,并且每页限制为 1 个项目,它将在第一个循环期间返回该值。
为什么以及如何解决这个问题?
非常感谢
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);
}
}
【问题讨论】:
-
您正在检查
$video->id是否在$stored_video数组中,但是甚至没有定义这样的值。
标签: php function recursion return return-value