遍历 Facebook Graph API 结果时出现延迟写入mysql数据库

发布于 2024-11-05 02:18:00 字数 1046 浏览 1 评论 0原文

我通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

亣腦蒛氧 2024-11-12 02:18:00

我建议分两次执行此操作,以避免延迟问题。

  1. 设置合理的脚本超时长度,从 API 获取详细信息并缓存结果。

如果一切顺利..

  1. 拿起缓存并插入到您的数据库中。

I would counsel doing this in two hits to avoid latency problems.

  1. set a reasonable script timeout length, fetch the details from the API and cache the results.

if all goes well ..

  1. Pick up the cache and insert into your database.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文