2014-10-16 89 views
0

我想實現用Facebook登錄到我的網站。Facebook的SDK V4錯誤

我的代碼在本地主機上工作得很好。但是,當我上傳到服務器。它給錯誤是這樣的:

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>'; 
} 

任何想法,爲什麼我收到此錯誤?

+0

[使用PHP facebook SDK 4.4.0時面臨錯誤]的可能重複(http://stackoverflow.com/questions/26389228/facing-error-while-using-php-facebook-sdk-4-4-0 ) – luschn 2014-10-16 10:46:00

+0

@luschn沒有提供解決方案。那麼你能幫助我嗎? – 2014-10-16 10:47:11

+0

這並不意味着你應該爲完全相同的問題打開另一個線程。從來沒有這樣做。 – luschn 2014-10-16 11:50:54

回答

1

您的提供商很可能會阻止傳出的CURL調用(Facebook SDK使用CURL調用Facebook API)。

見這個帖子同樣的錯誤:https://stackoverflow.com/questions/9425406/facebook-api-exception-object-error-on-facebook-app

這是有道理的,因爲它的工作原理在本地服務器上。你唯一能做的就是要求你的提供者允許調用Facebook API。

+0

非常感謝你的回答。我會聯繫我的服務器提供商 – 2014-10-16 16:58:13