Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php facebook graph api在2.2到2.3之间不起作用_Php_Facebook_Facebook Graph Api - Fatal编程技术网

Php facebook graph api在2.2到2.3之间不起作用

Php facebook graph api在2.2到2.3之间不起作用,php,facebook,facebook-graph-api,Php,Facebook,Facebook Graph Api,因为它是graph api 2.2的到期日,所以我正在尝试使用v2.3修复我的graph api 但是当我使用2.3时,我发现大多数api请求-响应-什么都没有,但是我在升级文档中找不到任何更新。例如: https://graph.facebook.com/v2.3/{$user_id}?date_format=U&fields=albums.order(reverse_chronological).limit(100).offset(0){id,count,name,created_t

因为它是graph api 2.2的到期日,所以我正在尝试使用v2.3修复我的graph api 但是当我使用2.3时,我发现大多数api请求-响应-什么都没有,但是我在升级文档中找不到任何更新。例如:

https://graph.facebook.com/v2.3/{$user_id}?date_format=U&fields=albums.order(reverse_chronological).limit(100).offset(0){id,count,name,created_time}
如果我使用2.3,将不返回任何内容。 我打电话时无法获取用户的生日:

https://graph.facebook.com/v2.3/{$user_id}
这是唯一的返回名称和现场位置。 但在v2.2中,它包括生日简介

我使用facebook SDK 3.2.2,因为我的php版本是5.3。
有什么我不知道的更新吗?谢谢。

我自己也发现了问题。这是因为SDK 3.2.2。对于facebook更新(来自API 2.3版):

[Oauth Access Token]Format—当您将代码交换为Access_Token时返回的响应格式现在返回有效的JSON,而不是URL编码。这个响应的新格式是{“access_token”:{token},“token_type”:{type},“expires_in”:{TIME}。我们进行此更新是为了符合RFC 6749第5.1节的要求

但SDK无法将响应识别为数组(在getAccessTokenFromCode函数中):

这将无法正确获取用户访问令牌,并且您无法获取用户数据。因此,您应该更新此函数以将数据解析为json:

$response = json_decode($access_token_response);
if (!isset($response->access_token)) {
  return false;
}
return $response->access_token;
然后所有功能都将正常工作


此外,您必须对
setExtendedAccessToken()
进行类似的更改。否则,您的应用程序将无法扩展访问令牌。下面的代码演示了如何升级该函数

  /**
   * Extend an access token, while removing the short-lived token that might
   * have been generated via client-side flow. Thanks to http://bit.ly/ b0Pt0H
   * for the workaround.
   */
  public function setExtendedAccessToken() {
    try {
      // need to circumvent json_decode by calling _oauthRequest
      // directly, since response isn't JSON format.
      $access_token_response = $this->_oauthRequest(
        $this->getUrl('graph', '/oauth/access_token'),
        $params = array(
          'client_id' => $this->getAppId(),
          'client_secret' => $this->getAppSecret(),
          'grant_type' => 'fb_exchange_token',
          'fb_exchange_token' => $this->getAccessToken(),
        )
      );
    }
    catch (FacebookApiException $e) {
      // most likely that user very recently revoked authorization.
      // In any event, we don't have an access token, so say so.
      return false;
    }

    if (empty($access_token_response)) {
      return false;
    }

    //Version 2.2 and down (Deprecated).  For more info, see http://stackoverflow.com/a/43016312/114558
    // $response_params = array();
    // parse_str($access_token_response, $response_params);
    //
    // if (!isset($response_params['access_token'])) {
    //   return false;
    // }
    //
    // $this->destroySession();
    //
    // $this->setPersistentData(
    //   'access_token', $response_params['access_token']
    // );

    //Version 2.3 and up.
    $response = json_decode($access_token_response);
    if (!isset($response->access_token)) {
      return false;
    }

    $this->destroySession();

    $this->setPersistentData(
      'access_token', $response->access_token
    );
  }

使用v2.3,您仍然应该获得所有字段。但是,从v2.4开始,您必须明确要求输入所需的字段,请参阅@CBroe谢谢,我稍后会注意到:)为了使此答案更易于搜索,我收到的错误消息是:“必须使用活动访问令牌查询有关当前用户的信息。”@SerhatAkay-你能提供更多的信息吗?2.8会发生什么?你收到错误信息了吗?上帝保佑你,布鲁斯。帮我省了很多麻烦:)
  /**
   * Extend an access token, while removing the short-lived token that might
   * have been generated via client-side flow. Thanks to http://bit.ly/ b0Pt0H
   * for the workaround.
   */
  public function setExtendedAccessToken() {
    try {
      // need to circumvent json_decode by calling _oauthRequest
      // directly, since response isn't JSON format.
      $access_token_response = $this->_oauthRequest(
        $this->getUrl('graph', '/oauth/access_token'),
        $params = array(
          'client_id' => $this->getAppId(),
          'client_secret' => $this->getAppSecret(),
          'grant_type' => 'fb_exchange_token',
          'fb_exchange_token' => $this->getAccessToken(),
        )
      );
    }
    catch (FacebookApiException $e) {
      // most likely that user very recently revoked authorization.
      // In any event, we don't have an access token, so say so.
      return false;
    }

    if (empty($access_token_response)) {
      return false;
    }

    //Version 2.2 and down (Deprecated).  For more info, see http://stackoverflow.com/a/43016312/114558
    // $response_params = array();
    // parse_str($access_token_response, $response_params);
    //
    // if (!isset($response_params['access_token'])) {
    //   return false;
    // }
    //
    // $this->destroySession();
    //
    // $this->setPersistentData(
    //   'access_token', $response_params['access_token']
    // );

    //Version 2.3 and up.
    $response = json_decode($access_token_response);
    if (!isset($response->access_token)) {
      return false;
    }

    $this->destroySession();

    $this->setPersistentData(
      'access_token', $response->access_token
    );
  }