Facebook SDK V4错误

时间:2014-10-16 10:40:42

标签: php facebook facebook-graph-api

我正在尝试使用Facebook登录我的网站。

我的代码在Localhost上完美运行。但是当我将它上传到服务器。它给出了这样的错误:

Facebook\FacebookSDKException Object ( [message:protected] => couldn't connect to host [string:Exception:private] => [code:protected]

您可以尝试DEMO: http://dkclan.co/facebook_user_authentication.php

这是我的代码:

<?php

// include required files form Facebook SDK

require 'facebook-php-sdk/autoload.php';

use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\Entities\AccessToken;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\HttpClients\FacebookHttpable;



// start session
session_start();

// init app with app id and secret
FacebookSession::setDefaultApplication( 'abc','xyz' );

// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper( 'http://dkclan.co/facebook_user_authentication.php' );

// see if a existing session exists
if ( isset( $_SESSION ) && isset( $_SESSION['fb_token'] ) ) {
// create new session from saved access_token
$session = new FacebookSession( $_SESSION['fb_token'] );
// validate the access_token to make sure it's still valid
try {
if ( !$session->validate() ) {
$session = null;
}
} catch ( Exception $e ) {
// catch any exceptions
$session = null;
}
}

if ( !isset( $session ) || $session === null ) {
// no session exists
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
// handle this better in production code
print_r( $ex );
} catch( Exception $ex ) {
// When validation fails or other local issues
// handle this better in production code
print_r( $ex );
}
}

// see if we have a session
if ( isset( $session ) ) {
// save the session
$_SESSION['fb_token'] = $session->getToken();
// create a session using saved token or the new one we generated at login
$session = new FacebookSession( $session->getToken() );
// graph api request for user data
$request = new FacebookRequest( $session, 'GET', '/me' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject()->asArray();
// print profile data
echo '<pre>' . print_r( $graphObject, 1 ) . '</pre>';
// print logout url using session and redirect_uri (logout.php page should destroy the session)
echo '<a href="' . $helper->getLogoutUrl( $session, 'http://yourwebsite.com/app/logout.php' ) . '">Logout</a>';
} else {
// show login url
echo '<a href="' . $helper->getLoginUrl( array( 'email', 'user_friends' ) ) . '">Login</a>';
}

我知道为什么会收到此错误?

1 个答案:

答案 0 :(得分:1)

您的提供商很可能会阻止传出的CURL调用(Facebook SDK使用CURL来调用Facebook API)。

使用相同的错误查看此主题:https://stackoverflow.com/questions/9425406/facebook-api-exception-object-error-on-facebook-app

它有意义,因为它适用于您的本地服务器。您唯一能做的就是让您的提供商允许拨打Facebook API。