遍历 Facebook Graph API 结果时出现延迟写入mysql数据库
我通过 php sdk 调用 Facebook 图形 api,当我通过几个嵌套的 foreach 循环调用并遍历数组时,即使我限制了被调用的记录数,它所花费的时间也比应有的时间要长得多。这是我的代码:
foreach ($userfriends['data'] as $friends) {
foreach ($friends as $key => $value) {
if($key == "id"){
$friend_id = $value;
}
// API CALL PULL FRIEND LIKES
try {
$username = $key;
$uservar = '/'.$username.'/likes?fields=id,category&limit=20';
$userlikes = $facebook->api($uservar);
}
catch (FacebookApiException $e) {
error_log($e);
}
foreach ($userlikes['data'] as $likes) {
foreach ($likes as $key => $value) {
if($key == "id"){
$like_id = $value;
}
if($key == "category"){
$userlike_category_id = $value;
}
}
// WRITING FRIEND LIKES TO DATABASE
$sql="INSERT INTO likes (like_id, category, friend_id)
VALUES ('$like_id', '$userlike_category_id', '$friend_id');";
mysql_query($sql,$con);
}
}
}
I'm calling the Facebook graph api via the php sdk and when I call and traverse the array through several nested foreach loops it takes far longer than it should even when I limit the # of records being called. Here's my code:
foreach ($userfriends['data'] as $friends) {
foreach ($friends as $key => $value) {
if($key == "id"){
$friend_id = $value;
}
// API CALL PULL FRIEND LIKES
try {
$username = $key;
$uservar = '/'.$username.'/likes?fields=id,category&limit=20';
$userlikes = $facebook->api($uservar);
}
catch (FacebookApiException $e) {
error_log($e);
}
foreach ($userlikes['data'] as $likes) {
foreach ($likes as $key => $value) {
if($key == "id"){
$like_id = $value;
}
if($key == "category"){
$userlike_category_id = $value;
}
}
// WRITING FRIEND LIKES TO DATABASE
$sql="INSERT INTO likes (like_id, category, friend_id)
VALUES ('$like_id', '$userlike_category_id', '$friend_id');";
mysql_query($sql,$con);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议分两次执行此操作,以避免延迟问题。
如果一切顺利..
I would counsel doing this in two hits to avoid latency problems.
if all goes well ..