我正在从公司的IG提要中获取最新3个帖子的数据,并在我们的网站上对它们进行进一步处理,以用于IG小部件。
我已经进行了一些故障排除,看来问题是解码原始数据的json_decode部分。
这是从IG API获取的JSON数据:
{"pagination": {"next_max_id": "2034341501387371123_8393175932", "next_url": "https://api.instagram.com/v1/users/8393175932/media/recent?access_token=8393175932.65497a8.29744121571744afacbe375999b2525f\u0026count=1\u0026max_id=2034341501387371123_8393175932"}, "data": [{"id": "2034341501387371123_8393175932", "user": {"id": "8393175932", "full_name": "Coffea", "profile_picture": "https://scontent.cdninstagram.com/vp/e88d6cdddc5697515447cde6330b03ff/5D76FD6E/t51.2885-19/s150x150/38097055_446825915802974_1995256258839445504_n.jpg?_nc_ht=scontent.cdninstagram.com", "username": "coffea.earth"}, "images": {"thumbnail": {"width": 150, "height": 150, "url": "https://scontent.cdninstagram.com/vp/3b467c8e7913ecca2874fcbb055fd5b9/5D645F5D/t51.2885-15/e35/s150x150/59153051_2234956400103169_2351775859365829130_n.jpg?_nc_ht=scontent.cdninstagram.com"}, "low_resolution": {"width": 320, "height": 320, "url": "https://scontent.cdninstagram.com/vp/f34190b7534acd762f6435e12b3dd610/5D5E5025/t51.2885-15/e35/s320x320/59153051_2234956400103169_2351775859365829130_n.jpg?_nc_ht=scontent.cdninstagram.com"}, "standard_resolution": {"width": 640, "height": 640, "url": "https://scontent.cdninstagram.com/vp/8643e994a3a87ca8d93f25bcedd3ebf3/5D5FC6D8/t51.2885-15/sh0.08/e35/s640x640/59153051_2234956400103169_2351775859365829130_n.jpg?_nc_ht=scontent.cdninstagram.com"}}, "created_time": "1556732426", "caption": {"id": "17866442770374263", "text": "Coffee superpower \ud83d\udc4a\ud83d\udca5\nHundreds of millions of people all around the world drink coffee every day. If even a part of them starts thinking about how their everyday habits impact the environment, the results can be enormous. In Coffea, we believe the magic is hidden behind the tiniest things. Even such a common matter like coffee has the power to change the world and from ordinary people makes superheroes thanks to its superpower. Do not hesitate to ask us about the conditions and the environment where the coffee has grown up and who stands behind it. #coffee #coffea #superpower #superman #superhero #rwanda #coffeeorigin #singleorigin #change #environment #roastedcoffee #greencoffee #coffeeimport #ecology #changetheworld #earth #brno", "created_time": "1556732426", "from": {"id": "8393175932", "full_name": "Coffea", "profile_picture": "https://scontent.cdninstagram.com/vp/e88d6cdddc5697515447cde6330b03ff/5D76FD6E/t51.2885-19/s150x150/38097055_446825915802974_1995256258839445504_n.jpg?_nc_ht=scontent.cdninstagram.com", "username": "coffea.earth"}}, "user_has_liked": false, "likes": {"count": 34}, "tags": ["roastedcoffee", "brno", "coffee", "change", "superman", "ecology", "coffeeorigin", "superpower", "greencoffee", "superhero", "rwanda", "environment", "earth", "coffea", "changetheworld", "coffeeimport", "singleorigin"], "filter": "Normal", "comments": {"count": 1}, "type": "image", "link": "https://www.instagram.com/p/Bw7btEuB3Jz/", "location": null, "attribution": null, "users_in_photo": []}], "meta": {"code": 200}}
这是基本的PHP json_decode函数:
$latestpost_coded = file_get_contents("https://api.instagram.com/...&count=1");
$latestpost_data = json_decode($latestpost_coded);
$latestpost = $latestpost_data->data;
我已经确认这是使它变慢的代码的一部分。非常慢,例如10秒的加载时间。最初,该代码加载了3个最新帖子,但是3个结果的搜索结果和1个结果的搜索结果一样慢。
答案 0 :(得分:0)
您应使用 cURL 而不是 file_get_contents(),因为在此讨论中,它确实会影响json_decode:
https://www.reddit.com/r/PHPhelp/comments/3hmxvs/json_decode_slow_af/
这是从其中一个答案中进行的cURL尝试:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"#### YOUR URL GOES HERE ####");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec ($ch);
$json = json_decode($data,TRUE);
// continue with normal json shit
curl_close ($ch);
答案 1 :(得分:0)
最终,我通过每天半夜运行一次cron作业来获取IG API结果来解决该问题,将其保存到本地文件中,然后IG小部件正在使用此文件,该文件运行得很好。由于它只是IG预览窗口,因此对于我所需的结果而言是令人满意的。
问题似乎出在我们的托管服务器和IG服务器之间的通信错误。
CURL由于某种原因未能获取结果,而没有引发任何错误,因此该解决方案对我来说是死胡同。