【发布时间】:2019-05-09 09:03:05
【问题描述】:
我正在获取我们公司的 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 个结果的结果与一个结果的结果一样慢。
【问题讨论】:
-
你代码中的慢行很可能不是
json_decode(),而是file_get_contents()。 -
可能是对的,但由于某种原因,CURL 似乎不起作用,还有其他选择吗?
-
« Some reason » 不是正当理由。 curl_error() 返回的任何错误消息?因为您通过 https 进行查询,所以我敢打赌它与证书有关。尝试使用 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); —不要在生产环境中使用它—如果可行,请提供有效且最新的 SSL 捆绑文件。见davidwalsh.name/php-ssl-curl-error
标签: php json instagram decode instagram-api