PHP 函数再次返回给函数


php function pass return to the function again

我在Instagram API中做,并且对函数中的循环有点困惑。

我尝试创建代码以从 instagram 用户那里获取所有图像,但 API 仅限制为 20 张图像。我们必须做下一个调用到下一页。

我正在对我的应用程序使用 https://github.com/cosenary/Instagram-PHP-API,这是获取图像的函数。

function getUserMedia($id = 'self', $limit = 0)
{
    $params = array();
    if ($limit > 0) {
        $params['count'] = $limit;
    }
    return $this->_makeCall('users/' . $id . '/media/recent', strlen($this->getAccessToken()), $params);
}

我尝试拨打电话,返回值为

{
"pagination": 
{
"next_url": "https://api.instagram.com/v1/users/21537353/media/recent?access_token=xxxxxxx&max_id=1173734674550540529_21537353",
"next_max_id": "1173734674550540529_21537353"
}, [.... another result data ....]

第一个函数结果,并产生20张图像。

我的问题是:

  1. 如何使用next_max_id参数将 return 从该函数再次传递给该函数,以便它将循环并再次使用该函数?
  2. 如何将结果合并为 1 个对象数组?

如果不好,我很抱歉我的英语和我的解释。

谢谢你的帮助。

你应该使用递归函数并在next_url找到 null/empty<</p>

div class="answers" 时停止该函数>

从Instagram-PHP-Api文档中,在我看来,您应该使用pagination()方法来接收下一页:

$photos = $instagram->getTagMedia('kitten');
$result = $instagram->pagination($photos); 

只需使用条件 (if) 来验证$result是否有内容,如果有,则使用 pagination() 再次调用以请求下一页。递归地执行此操作。

但我认为在没有 Instagram-PHP-API 的情况下使用 while 循环实现是个好主意:

$token = "<your-accces-token>";
$url = "https://api.instagram.com/v1/users/self/media/recent/?access_token=".$token;
while ($url != null) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($ch);
    curl_close($ch);
    $photos = json_decode($output);
    if ($photos->meta->code == 200) {
        // do stuff with photos
        $url = (isset($photos->pagination->next_url)) ? $photos->pagination->next_url : null; // verify if there's another page
    } else {    
        $url = null; // if error, stop the loop
    }
    sleep(1000); // to avoid to much requests on Instagram at almost the same time and protect your rate limits API
}

祝你好运!