如何从数组中获取结果

时间:2015-04-07 07:41:06

标签: php api phpmyadmin instagram-api

我想从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,fullname

1 个答案:

答案 0 :(得分:0)

作为你的输出似乎我们存在一个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 += " ";
}