doulangtiao4508 2017-04-05 15:35
浏览 20

Facebook图形API和php SDK性能问题

I'm trying to duplicate a public facebook page timeline on website, using the php SDK. Script works fine except for the execution time that is pretty long.
Searching around i've read that facebook API can be pretty slow sometimes, by the way i'd like some help figuring out if my script is fine or if some improvement can be done for a faster execution time.

Here's my simplified code :

    require_once 'Facebook/autoload.php';
    use Facebook\FacebookRequest;
    $fb = new Facebook\Facebook([
      'app_id' => 'myappid',
      'app_secret' => 'appsecret',
      'default_graph_version' => 'v2.8',
    ]);
    $request = '/11239244970/posts?access_token=my_acces_token_';
    $search = $fb->get($request);
    $raw= $search->getGraphEdge();
    $data = $raw->asArray();
    $results = [];
    foreach($data as $el){
      if(isset($el['message'])){
        try{
          $messaggio = $el['message'];
          $creato =  $el['created_time'];
          $newDate = $creato->format('d/m/Y');
          $laData = str_replace("/","-",$newDate);
          $id = $el['id'];
          $evento = $el['story'];
          $atch = $id.'/attachments?access_token=my_acces_token_';
          $xx = $fb->get($atch);
          $yy= $xx->getGraphEdge();
          $atachment = $yy->asArray();
          $like = $id.'/likes?summary=true&access_token=my_acces_token_';
          $likes = $fb->get($like);
          $likes = $likes->getGraphEdge();
          $like_count = $likes->getTotalCount();
          $atachments = [];
          }
        catch(Exception $e) {

        }
          //$likes = $likes->asArray();
          foreach($atachment as $at){
              if(isset($at['subattachments'])){
                $atchs = $at['subattachments'];
                foreach($atchs as $atx){
                  $atachments[] = $atx['media']['image']['src'];
                }
              }else if(isset($at['media']['image'])){
                  $atachments[] = $at['media']['image']['src'];
                }

            }

          $temp = [$messaggio,$laData,$evento,$like_count,$atachments];
          $results[]= $temp;
      }

    }
    print_r($results);

That piece of code grabs the last (i think by default) 25 posts and check if they have message and atachments.

As i said this code works fine, it is getting what i need except for execution time that is about 5 to 10 seconds. Any suggestions or improvement? Thanks

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
    • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
    • ¥16 mybatis的代理对象无法通过@Autowired装填
    • ¥15 可见光定位matlab仿真
    • ¥15 arduino 四自由度机械臂
    • ¥15 wordpress 产品图片 GIF 没法显示
    • ¥15 求三国群英传pl国战时间的修改方法
    • ¥15 matlab代码代写,需写出详细代码,代价私
    • ¥15 ROS系统搭建请教(跨境电商用途)
    • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。