繁体   English   中英

如何使用php从instagram api响应中将数组数据插入mysql db?

[英]How to insert array data into mysql db from instagram api response using php?

我正在调用instagram api,并在$ record中得到一个作为数组的响应。

我想知道如何从$ record获取用户名,first_name,profile_picture,id,last_name变量,并将它们插入mysql db(如果它们尚未在mysql db中)? 谁能告诉我如何做到这一点? 提前致谢。

根据Instagram API文档 ,以下api:

https://api.instagram.com/v1/users/XXXX/follows?access_token==XXXX&count=-1 

编辑:现在我可以打印数组变量,但如何将它们插入到mysql db中并避免重复?

foreach($record->data as $user) // each user data (JSON array) defined as $user
{
echo "User Name:".$user->username;
echo "<br>Bio:".$user->bio;
echo "<br>Website:".$user->website;
echo "<br>Profile Picture:".$user->profile_picture;
echo "<br>Full Name:".$user->full_name;
echo "<br>id:".$user->id;
echo "<br><br>";
}

返回值:

{
    "data": [{
        "username": "washington",
        "first_name": "George",
        "profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_6623_75sq.jpg",
        "id": "1",
        "last_name": "Washington"
    },
    {
        "username": "SammyDavis",
        "first_name": "Sammy",
        "profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_29648_75sq_1294520029.jpg",
        "id": "29648",
        "last_name": "Davis"
    },
    {
        "username": "FrankTheTank",
        "first_name": "Frank",
        "profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_13096_75sq_1286441317.jpg",
        "id": "13096",
        "last_name": "Roberts"
    }]
}

php脚本:

$url = "https://api.instagram.com/v1/users/XXXX/follows?access_token==XXXX&count=-1";
$api_response = get_data(''.$url);
$record = json_decode($api_response); // JSON decode 

//now i want to get username,first_name,profile_picture,id,last_name variables and insert it into mysql db


/* gets the data from a URL */
function get_data($url) {
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}
foreach ($record->data as $rows){
    //now you can get vars from $rows
    $username = $rows->username;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM