2014-11-25 279 views
4

我一直在嘗試使用Facebook的PHP sdk登錄。facebook PHP sdk無法登錄

以下是我的其中一個代碼。

<?php 
session_start(); 
require_once('lib/Facebook/FacebookSession.php'); 
require_once('lib/Facebook/FacebookRedirectLoginHelper.php'); 
require_once('lib/Facebook/FacebookRequest.php'); 
require_once('lib/Facebook/FacebookResponse.php'); 
require_once('lib/Facebook/FacebookSDKException.php'); 
require_once('lib/Facebook/FacebookRequestException.php'); 
require_once('lib/Facebook/FacebookAuthorizationException.php'); 
require_once('lib/Facebook/GraphObject.php'); 
require_once('lib/Facebook/GraphUser.php'); 
require_once('lib/Facebook/GraphSessionInfo.php'); 
require_once('lib/Facebook/HttpClients/FacebookHttpable.php'); 
require_once('lib/Facebook/HttpClients/FacebookCurl.php'); 
require_once('lib/Facebook/HttpClients/FacebookCurlHttpClient.php'); 
require_once('lib/Facebook/Entities/AccessToken.php'); 
require_once('lib/Facebook/Entities/SignedRequest.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\GraphUser; 
use Facebook\GraphSessionInfo; 
use Facebook\HttpClients\FacebookHttpable; 
use Facebook\HttpClients\FacebookCurl; 
use Facebook\HttpClients\FacebookCurlHttpClient; 

// Initialize application by Application ID and Secret 
FacebookSession::setDefaultApplication('app_id','app_secret'); 

// Login Healper with reditect URI 
$helper = new FacebookRedirectLoginHelper('http://124.125.244.163/rtCamp/home.php'); 

try { 
    $session = $helper->getSessionFromRedirect(); 
} 
catch(FacebookRequestException $ex) { 
    // Exception 
} 
catch(Exception $ex) { 
    // When validation fails or other local issues 
} 

// Checking Session 
if(isset($session)) 
{ 
    // Request for user data 
    $request = new FacebookRequest($session, 'GET', '/me'); 
    $response = $request->execute(); 
    // Responce 
    $data = $response->getGraphObject(); 

    // Print data 
    echo print_r($data, 1); 
} 
else 
{ 
    // Login URL if session not found 
    echo '<a href="' . $helper->getLoginUrl() . '">Login</a>'; 
} 
?> 

現在我面臨的問題是,我可以得到一個會話的線路有問題。 即$session = $helper->getSessionFromRedirect()頁面從Facebook重定向後。

現在我甚至實現了這個傢伙的整個GitHub的目錄使用相同的登錄密碼here

的是,我的代碼將停止該行工作後,並沒有顯示一個錯誤。我嘗試了很多解決方案,但他們都不工作我下載了穩定的Facebook PHP SDK 4.0.0,並且運行了PHP 5.4的家庭LAMP服務器。我被卡住了。

UPDATE:錯誤出現在宇瞻登錄

PHP Fatal error: Class 'Facebook\HttpClients\FacebookStreamHttpClient' not found in /var/www/rtCamp/lib/Facebook/FacebookRequest.php on line 166, referer: http://124.125.244.163/rtCamp/home.php

+0

我有同樣的問題! – 2015-06-10 11:57:12

回答

-1
<?php 
require_once('lib/Facebook/FacebookSession.php'); 
require_once('lib/Facebook/FacebookRedirectLoginHelper.php'); 
require_once('lib/Facebook/FacebookRequest.php'); 
require_once('lib/Facebook/FacebookResponse.php'); 
require_once('lib/Facebook/FacebookSDKException.php'); 
require_once('lib/Facebook/FacebookRequestException.php'); 
require_once('lib/Facebook/FacebookAuthorizationException.php'); 
require_once('lib/Facebook/GraphObject.php'); 
require_once('lib/Facebook/GraphUser.php'); 
require_once('lib/Facebook/GraphSessionInfo.php'); 
require_once('lib/Facebook/HttpClients/FacebookHttpable.php'); 
require_once('lib/Facebook/HttpClients/FacebookCurl.php'); 
require_once('lib/Facebook/HttpClients/FacebookCurlHttpClient.php'); 
require_once('lib/Facebook/Entities/AccessToken.php'); 
require_once('lib/Facebook/Entities/SignedRequest.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\GraphUser; 
use Facebook\GraphSessionInfo; 
use Facebook\HttpClients\FacebookHttpable; 
use Facebook\HttpClients\FacebookCurl; 
use Facebook\HttpClients\FacebookCurlHttpClient; 


session_start(); 

$app_id=''; 
$app_secret=''; 
$redirect_url='http://124.125.244.163/rtCamp/home.php'; 
// Initialize application by Application ID and Secret 
FacebookSession::setDefaultApplication($app_id,$app_secret); 

// Login Healper with reditect URI 
$helper = new FacebookRedirectLoginHelper($redirect_url); 
$session = $helper->getSessionFromRedirect(); 


// Checking Session 
if(isset($session)) 
{ 
    // Request for user data 
    $request = new FacebookRequest($session, 'GET', '/me'); 
    $response = $request->execute(); 
    // Responce 
    $data = $response->getGraphObject(GraphUser::classname()); 
    $name=$graph->getName(); 
    echo "hi $name"; 
    // Print data 
    echo print_r($data, 1); 
} 
else 
{ 
    // Login URL if session not found 
    echo '<a href="' . $helper->getLoginUrl() . '">Login</a>'; 
} 

/* 
try { 
    $session = $helper->getSessionFromRedirect(); 
} 
catch(FacebookRequestException $ex) { 
    // Exception 
} 
catch(Exception $ex) { 
    // When validation fails or other local issues 
} 

// Checking Session 
if(isset($session)) 
{ 
    // Request for user data 
    $request = new FacebookRequest($session, 'GET', '/me'); 
    $response = $request->execute(); 
    // Responce 
    $data = $response->getGraphObject(); 

    // Print data 
    echo print_r($data, 1); 
} 
else 
{ 
    // Login URL if session not found 
    echo '<a href="' . $helper->getLoginUrl() . '">Login</a>'; 
} 
*/ 
?> 
+1

您是否介意在代碼中添加一些解釋? – 2015-03-15 11:00:11