PHP Instagram API - How to get MIN_TAG_ID and MAX_TAG_ID

守給你的承諾、 提交于 2019-12-10 20:51:32

问题


Guys I'm working around the Instagrams API to get all images with a specific tag.

Here is my code:

<?PHP
 function callInstagram($url)
    {
    $ch = curl_init();
    curl_setopt_array($ch, array(
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_SSL_VERIFYHOST => 2
    ));

    $result = curl_exec($ch);
    curl_close($ch);
    return $result;
    }

    $tag = 'sweden';
    $client_id = "XXXX";
    $url = 'https://api.instagram.com/v1/tags/'.$tag.'/media/recent?client_id='.$client_id;

    $inst_stream = callInstagram($url);
    $results = json_decode($inst_stream, true);

    //Now parse through the $results array to display your results... 
    foreach($results['data'] as $item){
        $image_link = $item['images']['thumbnail']['url'];
        $Profile_name = $item['user']['username'];
        echo '<div style="display:block;float:left;">'.$Profile_name.' <br> <img src="'.$image_link.'" /></div>';
    }

With this code I get 20 of the images with tag sweden.

I need to know how I can get the min_tag_id and max_tag_id for this tag so I can make a something like pagination.

So can you give me any advice how can I get the ID of the last post/image displayed ?

Thanks in advance!


回答1:


From the instagram docs:

PAGINATION

Sometimes you just can't get enough. For this reason, we've provided a convenient way to access more data in any request for sequential data. Simply call the url in the next_url parameter and we'll respond with the next set of data.

{
    ...
    "pagination": {
        "next_url": "https://api.instagram.com/v1/tags/puppy/media/recent?access_token=fb2e77d.47a0479900504cb3ab4a1f626d174d2d&max_id=13872296",
        "next_max_id": "13872296"
    }
}
On views where pagination is present, we also support the "count" parameter. Simply set this to the number of items you'd like to receive. Note that the default values should be fine for most applications - but if you decide to increase this number there is a maximum value defined on each endpoint.

https://instagram.com/developer/endpoints/




回答2:


Instagram's API returns a "pagination" key with the "next_url" and "next_max_id" nodes. Modify your code so that if $result['pagination']['next_url'] != '', the loop continues.

Documentation here: https://instagram.com/developer/endpoints/



来源:https://stackoverflow.com/questions/29264434/php-instagram-api-how-to-get-min-tag-id-and-max-tag-id

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!