Batch calls with Facebook Graph API & PHP

前端 未结 5 1230
遇见更好的自我
遇见更好的自我 2020-12-13 07:55

Designing my first app against the Graph API, using version 2.1.2 of the Facebook supplied PHP library. Trying to maximize performance, etc out of the box and want to batch

5条回答
  •  渐次进展
    2020-12-13 08:13

    I have given example for batch calls of fql queries. It might help someone.

    //$current_user=facebook id

     $query1="SELECT uid, name FROM user WHERE is_app_user=1 AND uid IN (SELECT uid2 FROM friend WHERE uid1 = $current_user)";
     $query2="SELECT uid, name, work_history FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = $current_user )";
     $query3="SELECT uid, name, work, education FROM user WHERE uid = $current_user";
     $queries = array(
               array('method'=>'GET', 'relative_url'=>'method/fql.query?query='.str_replace(' ','+',$query1)),
               array('method'=>'GET', 'relative_url'=>'method/fql.query?query='.str_replace(' ','+',$query2)),
               array('method'=>'GET', 'relative_url'=>'method/fql.query?query='.str_replace(' ','+',$query3))
                );
    
                $objs = $facebook->api('/?batch='.json_encode($queries), 'POST');
    

    $objs gets json array of whole result of thre queries.

    And it is saving time a lot. This 3 queries individually takes total 9 seconds. With multiquery it takes 7 seconds. And with batch request it takes 3.6 seconds.

提交回复
热议问题