【问题标题】:json_decode from Instagram API terribly slow来自 Instagram API 的 json_decode 非常慢
【发布时间】: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


【解决方案1】:

您应该使用 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);

【讨论】:

  • 我觉得 OP 对 JSON 本身没有问题,但是从 instagram API 获得响应所花费的时间。
  • 我不认为是这样,IG API结果被快速获取。
  • 试一试,好像 OP 的问题和你一样!
  • 我已经尝试过了,由于某种原因,我没有从 CURL 代码中得到任何结果 - 但没有错误
  • 那么 $data 和 $json 是未定义的还是它们有任何值?
【解决方案2】:

最终我解决了这个问题,每天午夜运行一次 cron 作业以获取 IG API 结果,将其保存到本地文件中,然后 IG 小部件使用该文件,该小部件运行良好。由于它只是一个IG预览窗口,这对于我所需的结果来说已经非常令人满意了。

似乎问题在于我们的托管服务器和 IG 服务器之间的通信错误。

CURL 出于某种原因未能获取结果,没有抛出任何错误,所以这个解决方案对我来说是一个死胡同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-29
    • 1970-01-01
    • 2017-11-24
    • 2014-05-15
    • 2019-04-21
    • 2014-05-10
    相关资源
    最近更新 更多