1

我正在做一个小项目,用户可以在这个项目中看到标记为“kitties”的图像。Instagram 只允许 5000 个请求/小时,我认为它不会达到这个,但我选择以任何方式缓存。也因为我不知道如何让反向链接工作。我只能获取下一页的链接,然后最近页面的链接成为当前页面,一个指向自身的链接。此外,该 api 可以返回奇怪数量的图像,有时 14 次,有时 20 次等等。我希望它始终每页显示 20 张图像,并且只有 5 页(100 张图像)。然后每 5/10 分钟更新一次这个文件。

所以,我的计划是将 100 张图像存储到一个文件中。我得到它的工作,但它是令人难以置信的慢。代码如下所示:

$cachefile = "instagram_cache/".TAG.".cache";
$num_requests = 0; //Just for developing and check how many request it does

//If the file does not exsists or is older than *UPDATE_CACHE_TIME* seconds
if (!file_exists($cachefile) || time()-filemtime($cachefile) > UPDATE_CACHE_TIME)
{
    $images = array();
    $current_file = "https://api.instagram.com/v1/tags/".TAG."/media/recent?client_id=".INSTAGRAM_CLIENT_ID;
    $current_image_index = 0;


    for($i = 0; $i >= 0; $i++)
    {
        //Get data from API
        $contents = file_get_contents($current_file);

        $num_requests++;
        //Decode it!
        $json = json_decode($contents, true);

        //Get what we want!
        foreach ($json["data"] as $x => $value)
        {                
            array_push($images, array(
                'img_nr' => $current_image_index,
                'thumb' => $value["images"]["thumbnail"]["url"],
                'fullsize' => $value["images"]["standard_resolution"]["url"],
                'link' => $value["link"], 
                'time' => date("d M", $value["created_time"]),
                'nick' => $value["user"]["username"],
                'avatar' => $value["user"]["profile_picture"],
                'text' => $value['caption']['text'],
                'likes' => $value['likes']['count'],
                'comments' => $value['comments']['data'],
                'num_comments' => $value['comments']['count'],
            ));

            //Check if the requested amount of images is equal or more...
            if($current_image_index > MAXIMUM_IMAGES_TO_GET)
                break;

            $current_image_index++;

        }
        //Check if the requested amount of images is equal or more, even in this loop...
        if($current_image_index > MAXIMUM_IMAGES_TO_GET)
            break;
        if($json['pagination']['next_url'])
            $current_file = $json['pagination']['next_url'];

        else
            break; //No more files to get!

    }
    file_put_contents($cachefile, json_encode($images));

这感觉就像一个非常丑陋的黑客,关于如何让这个工作更好的任何想法?

或者有人可以告诉我如何使“反向链接”正常工作?(是的,我可以是 js 并且在历史上是 -1,但不是!)。

任何想法、建议、帮助、评论等都将受到赞赏。

4

1 回答 1

0

为什么不订阅实时并将图像存储在数据库中?然后,当它们被渲染时,您可以检查图像 url 是否有效(检查照片是否已被删除)。从您自己的数据库获取数据将比从 Instagram 快得多

于 2013-02-12T05:14:35.487 回答