Php oAuth,使用WordPress原生方法获取Facebook使用的数据?

Php oAuth,使用WordPress原生方法获取Facebook使用的数据?,php,facebook-graph-api,wordpress,Php,Facebook Graph Api,Wordpress,我正在尝试使用wordpress中的oauth从facebook获取用户数据。我在插件文件中有以下代码 class fb{ function authenticate($req){ define('FACEBOOK_APPID','xxxxx'); // replace 123 with your app id define('FACEBOOK_APPSECRET','xxxxxx'); // replace abc with your app secret define('REDIRE

我正在尝试使用wordpress中的oauth从facebook获取用户数据。我在插件文件中有以下代码

class fb{
    function authenticate($req){
define('FACEBOOK_APPID','xxxxx'); // replace 123 with your app id
define('FACEBOOK_APPSECRET','xxxxxx'); // replace abc with your app secret
define('REDIRECTURI','http://test.name.com/index.php?request=facebookdata_Action'); 
   if ($_REQUEST['code'] != '') {
    if ($_REQUEST['state'] != '' && wp_verify_nonce($_REQUEST['state'], 'my-nonce')) {

        $api_url = sprintf("https://graph.facebook.com/oauth/access_token?client_id=%s&redirect_uri=%s&client_secret=%s&code=%s",
            urlencode(FACEBOOK_APPID),
            urlencode(REDIRECTURI),
            urlencode(FACEBOOK_APPSECRET),
            urlencode($_REQUEST['code'])
        );

        $response = wp_remote_request($api_url, array(
            'timeout' => 60,
            'sslverify' => false,
            'method' => 'GET'
        ));

        if( is_wp_error( $response ) ) {
          $messages['error'] = "There was an error.";
        } else {
            $args = wp_parse_args( wp_remote_retrieve_body($response), array() );
            //echo $args['access_token'];
            $messages['userdata'] = $args;
        }
    }
} else {
     $facebook_dialog_url = sprintf("https://www.facebook.com/dialog/oauth?client_id=%s&redirect_uri=%s&state=%s&scope=user_photos,email,user_birthday,user_online_presence",
        FACEBOOK_APPID,
        urlencode(REDIRECTURI),
        wp_create_nonce ('my-nonce')
     );
  $messages['url'] = trim($facebook_dialog_url);

}
     echo json_encode($messages);
       exit();
    }
}
//用于处理AJAX请求的函数

function Request_handler() {
if (isset($_REQUEST['request']) && ($_REQUEST['request'] == 'facebook_Action')) {
        // Otherwise display an error and exit the call
        $obj = new fb();
        echo $obj->authenticate($_REQUEST);
        exit();
    }   
}
//将处理程序添加到init()中

当我点击按钮时,我使用ajax调用这个
facebook\u操作
,ajax将调用这个
authenticate
函数,最后我得到了
facebook url
。我使用window.location重定向到facebook登录页面

登录后,我被重定向到我的页面。在那里我得到了作为响应的访问令牌。我的要求是获得用户数据,即
电子邮件、性别、电话号码作为响应

请给我一些建议我参考了这个

使用access\u令牌找到了解决方案

// Attempt to query the graph:
  $graph_url = "https://graph.facebook.com/me?"
    . "access_token=" . $access_token;
  $response = curl_get_file_contents($graph_url);
  $decoded_response = json_decode($response);

function curl_get_file_contents($URL) {
    $c = curl_init();
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($c, CURLOPT_URL, $URL);
    $contents = curl_exec($c);
    $err  = curl_getinfo($c,CURLINFO_HTTP_CODE);
    curl_close($c);
    if ($contents) return $contents;
    else return FALSE;
  }

你到底有什么问题?只是获取用户数据(可以使用)还是其他什么?@Robbie谢谢,我在获取用户数据作为响应时遇到了麻烦。
// Attempt to query the graph:
  $graph_url = "https://graph.facebook.com/me?"
    . "access_token=" . $access_token;
  $response = curl_get_file_contents($graph_url);
  $decoded_response = json_decode($response);

function curl_get_file_contents($URL) {
    $c = curl_init();
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($c, CURLOPT_URL, $URL);
    $contents = curl_exec($c);
    $err  = curl_getinfo($c,CURLINFO_HTTP_CODE);
    curl_close($c);
    if ($contents) return $contents;
    else return FALSE;
  }