2
我想從API中獲取一些數據,並將其放入數組,然後將其放入MySQL中。PHP數組foreach
我的代碼:
$find_sql = "SELECT * FROM `scrape`";
$users_to_scrape = $app['db']->fetchAll($find_sql);
$instagram = $app['instagram'];
$oauth = json_decode(file_get_contents($app['oauth_path']));
$instagram->setAccessToken($oauth);
foreach($users_to_scrape as $user_to_scrape) {
printf("Getting info for %s <%s>\n", $user_to_scrape['instagram_id'], $user_to_scrape['user_name']);
$follows = $instagram->getUser($user_to_scrape['instagram_id'], 999);
foreach($follows->data as $follow) {
echo var_dump($follows);
$data = array(
'instagram_id' => $follow->id,
'followed_by_instgram_id' => $user_to_scrape['instagram_id'],
'user_name' => $follow->username,
'full_name' => iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($follow->full_name)),
'profile_picture' => $follow->profile_picture,
'followers' => $follow->counts->followed_by,
'follows' => $follow->counts->follows
);
printf("+ %s <%s>\n", $data['instagram_id'], $data['user_name']);
//skapa tabell med follows (instagram_id,
if ($follow->counts->followed_by >= "30000") {
$app['db']->insert('follows', $data);
}
}
}
的Vardump返回此:
object(stdClass)#111 (2) {
["meta"]=>
object(stdClass)#112 (1) {
["code"]=>
int(200)
}
["data"]=>
object(stdClass)#113 (7) {
["username"]=>
string(9) "Dimbos"
["bio"]=>
string(97) "•Have fun in life Contact: [email protected]"
["website"]=>
string(24) "http://www.life.com"
["profile_picture"]=>
string(106) "https://xxertt.com/hphotos-ak-xaf1/t51.2885-19/11311351_362556250614181_543_a.jpg"
["full_name"]=>
string(10) "Dimbo"
["counts"]=>
object(stdClass)#114 (3) {
["media"]=>
int(113)
["followed_by"]=>
int(256673)
["follows"]=>
int(345)
}
["id"]=>
string(8) "38353560"
}
}
我收到的錯誤是這樣的:
注意:試圖讓非財產 - 對象/var/www/script.php在線
第40行,我們有這樣的: 'instagram_id' => $follow->id,
我還對下面的一組陣列得到錯誤。
難以想象。
根據'var_dump'看起來'$ follow'只是'follow'的一個實例,而不是'foreach'所期望的'follow'的數組。 – chiliNUT