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

时间:2014-06-28 18:00:02

标签: php facebook api

我在一个请求中请求来自几个不同的Facebook Like Pages的数据。如果一个页面的ID不正确,由于某种原因,请求失败:

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

有没有办法请求那些“正确”的ID。我可以请求每个页面(cron作业)查看ID是否正确,但如果有一种方法可以请求数据,即使一个ID不存在也是不必要的。

提前致谢

1 个答案:

答案 0 :(得分:0)

Batch个请求可以帮助您POST GET/?batch个端点(编码为JSON),响应将返回为:

r($batch)

每个数组都包含一个GET查询,您请求使用$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