我正在调用 instagram api 并以 $record 中的数组形式获得响应。
我想知道如何从 $record 中获取 username、first_name、profile_picture、id、last_name 变量并将它们插入到 mysql db 中,如果它们还没有在 mysql db 中?谁能告诉我如何做到这一点?提前致谢。
根据Instagram API Docs以下 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;
}