【问题标题】:how to fetch the result from array如何从数组中获取结果
【发布时间】:2015-04-07 07:41:06
【问题描述】:

我正在尝试从 instagram 获取我的关注者和关注者的列表,(对于一个 php 网站)

我使用了这个代码

$followedby = file_get_contents("https://api.instagram.com/v1/users/$userid/followed-by?access_token=$accessToken");
echo $followedby;

这是我得到的输出样本

{"pagination":{},"meta":{"code":200},"data":
    [
        {
            "username":"abc",
            "profile_picture":"https:\/\/instagramimages-a.akamaihd.net\/profiles\/profile_pic.jpg",
            "id":"36938409",
            "full_name":"abc"
        },
        {
            "username":"cdf",
            "profile_picture":"https:\/\/igcdn-photos-e-a.akamaihd.net\/hphotos-ak-xfa1\/t51.2885-19\/pic.jpg",
            "id":"1174996540",
            "full_name":"cdf"
        }
    ]
}

谁能告诉我如何从这个结果中获取用户名、img、全名

【问题讨论】:

    标签: php api phpmyadmin instagram-api


    【解决方案1】:

    作为您的输出,我们似乎存在一个 JSON 字符串

    你可以用两种语言 PHP 或 JavaScript/jQuery 来处理它

    PHP

    $data = json_decode($response); //to decode the string into an object
    $count = count($data->data); //count how many users there are in the string
    $i;
    for($i=0;$i<$count;$i++) //run through the users
    {    
        echo $data->data[$i]->username . "\r\n";
        echo $data->data[$i]->profile_picture . "\r\n";
        echo $data->data[$i]->full_name . "\r\n";
        echo "\r\n";
    }
    

    JavaScript

    var obj = JSON.parse(response);
    var i;
    for(i = 0; i<obj.data.length; i++) {
        document.getElementById('sandbox').innerHTML += obj.data[i].username;
        document.getElementById('sandbox').innerHTML += " ";
    }
    

    【讨论】:

      猜你喜欢
      • 2014-11-03
      • 1970-01-01
      • 1970-01-01
      • 2017-09-30
      • 2011-07-09
      • 2018-05-15
      • 2019-01-09
      • 1970-01-01
      • 2012-02-11
      相关资源
      最近更新 更多