dongyi9484 2014-06-28 18:00
浏览 89

Facebook API请求,即使FB usernam不存在

I am requesting data from a few different Facebook Like Pages in one request. If one Pages' ID is not correct, for some reason, the request fails:

{
   "error": {
  "message": "(#803) Some of the aliases you requested do not exist:    asd121233,das3b12s",
      "type": "OAuthException",
  "code": 803
   }
}

Is there a way to request those IDs that are "correct". I could request every page (cron job) to see if the IDs are correct, but that would be unnecessary if there is a way to request data even though one ID does not exist.

Thanks in advance

  • 写回答

1条回答 默认 最新

  • dtc9222 2014-07-03 07:24
    关注

    Batch requests can help, you POST your GET queries to /?batch endpoint (as encoded JSON), the response will return as:

    r($batch),

    each array contains a GET query you requests you can access data using $batch[{index}]['body']['{desired_field}']

    <?php
    
     $access_token = '{app_id}|{app_secret}'; // a valid user or an app access_token
    
      $queries = array(
        array('method' => 'GET', 'relative_url' => '/google?fields=about,name'),
        array('method' => 'GET', 'relative_url' => '/fiverras3t5wv4tbsdf?fields=about,name'), // this page doesn't exist for sure
        array('method' => 'GET', 'relative_url' => '/designtaxi?fields=about,name'),
        array('method' => 'GET', 'relative_url' => '/smashmag?fields=about,name'),
        array('method' => 'GET', 'relative_url' => '/XstackoverflowX?fields=about,name'), // this page doesn't exist for sure
        array('method' => 'GET', 'relative_url' => '/asd121233?fields=about,name'), // this page doesn't exist for sure
        );
    
      $p = array('access_token' => $access_token);
    
      $batch = api('/?batch='.json_encode($queries), 'POST', $p);
    
      foreach ($batch as $response) { // loop check each inner response
    
        if ($response['code'] == '200') { // if the graph has returned data
    
           $response = json_decode($response['body'], true);
    
           print '<p>About '. $response['name'].': ' . $response['about'].'</p>'.PHP_EOL;
    
        } else { // the pages doesn't exists (404 bad request)
    
           print '<p>The page doesn\'t exists'.'</p>'.PHP_EOL;
    
        }
      }
    
    function api( $path, $method = NULL, $params = NULL) {
    
        $apiCall = 'https://graph.facebook.com/v2.0' . $path;
                 // Initiate the cURL extension
                 $ch = curl_init();
                           // Check if the method is GET or POST
                           if ( is_null($method) ) {
    
                                 curl_setopt( $ch, CURLOPT_POST, false );
                                 if ( is_null($params) ) {
                                 // NO PARAMS
                                 } else {
                                 $apiCall .= '?' . http_build_query($params);
                                 // echo 'NULL:'.$apiCall;
                                 }
    
                            } else if ( $method === 'GET' ) {
    
                                 curl_setopt( $ch, CURLOPT_POST, false );
                                 if ( is_null($params) ) {
                                 // NO PARAMS
                                 } else {
                                 $apiCall .= '?' . http_build_query($params);
                                 // echo 'GET: '.$apiCall;
                                 }
    
                           } else if ( $method === 'POST' ) {
    
                                 curl_setopt( $ch, CURLOPT_POST, true );
                                 // Check if any params are set
                                 if ( is_null($params) ) {
                                 // NO PARAMS
                                 } else {
                                 $params = http_build_query($params);
                                 curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
                                 }
    
                           }
    
                           curl_setopt($ch, CURLOPT_URL, $apiCall);
                           curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                           curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
                           curl_setopt($ch, CURLOPT_HEADER, 0);
                           curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,0);
                           curl_setopt($ch, CURLOPT_TIMEOUT, 500);
                 // Execute cURL         
                 $apiResults = curl_exec($ch);
                 // Close cURL
                 curl_close($ch);
                 // return an JSON *ARRAY* not an object
                 return json_decode($apiResults, true);
    
    }
    

    php-ref

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器