我的MySql数据库中存有多个名称和访问令牌,我想为这些用户显示图像。
我在网上找到了一个图书馆,效果很好。我正在测试我的帐户,到目前为止一直很好。但是,当我显示所有帐户时,问题就出现了。 它只显示添加的最新帐户.. 虽然loop很适合从MySQL获取数据,但是当我添加这个Instagram代码时会出现问题。
这是代码: LIB / instagram_single.php
<?php
class InstaWCD{
function userID(){
$username = strtolower($this->username); // sanitization
$token = $this->access_token;
$url = "https://api.instagram.com/v1/users/search?q=".$username."&access_token=".$token;
$get = file_get_contents($url);
$json = json_decode($get);
foreach($json->data as $user){
if($user->username == $username){
return $user->id;
}
}
return '00000000'; // return this if nothing is found
}
function userMedia(){
$url = 'https://api.instagram.com/v1/users/'.$this->userID().'/media/recent/?access_token='.$this->access_token;
$content = file_get_contents($url);
return $json = json_decode($content, true);
}
}
$insta = new InstaWCD();
$insta->username = $username;
$insta->access_token = $access_token;
?>
我试图运行的代码。
<?php
$statement = $dbConn->prepare("SELECT v2_instagram_users.user_id, v2_instagram_users.instagram_id, v2_instagram_users.username, v2_instagram_users.Name, v2_instagram_users.Bio, v2_instagram_users.instagram_access_token, users.user_name, users.user_age, users.country, users.gender, users.gender_search, users.age_from, users.age_to
FROM v2_instagram_users, users
WHERE v2_instagram_users.user_id = users.id
ORDER by v2_instagram_users.id DESC");
$statement->execute();
while ($row = $statement->fetch(PDO::FETCH_BOTH)) {
$username = $row['username'];
$access_token = $row['instagram_access_token'];
$count = 20; // number of images to show
include 'lib/instagram_single.php'; //include instagram library
?>
<?php
$ins_media = $insta->userMedia();
$i = 0;
foreach ($ins_media['data'] as $vm):
if($count == $i){ break;}
$i++;
$img = $vm['images']['low_resolution']['url'];
$link = $vm["link"];
?>
<a target="_blank" href="<?php echo $link ?>">
<img src="<?php echo $img; ?>" width="175" style="float:left;" />
</a>
<?php endforeach ?>
<?php
}
?>
当我在数据库中有4个帐户时,任何人都有任何想法为什么只显示一个帐户及其图像?
Cheerz:)