2013-07-12 150 views
0

嗨我試圖與Facebook的SDK的PHP工作,我有一些問題。下面的代碼直接從它們提供的示例中複製,但我甚至無法獲取腳本的html元素以顯示......這使我認爲某處存在語法錯誤。當我檢查嵌入在html部分中的php時,我相信我會在if-else語句中看到一些問題。舉例來說,似乎是一個:當應該有一個{以下行:問題與Facebook的Facebook的PHP登錄

<?php if ($user): ?> 

我是在說這句話的正確或不使用Facebook的語法我不熟悉?

<?php 

require 'facebook.php'; 

// Create our Application instance (replace this with your appId and secret). 
$facebook = new Facebook(array(
    'appId' => 'my_app', 
    'secret' => 'my_secret', 
)); 

// Get User ID 
$user = $facebook->getUser(); 



if ($user) { 
try { 
// Proceed knowing you have a logged in user who's authenticated. 
$user_profile = $facebook->api('/me'); 
} catch (FacebookApiException $e) { 
error_log($e); 
$user = null; 
} 
} 

// Login or logout url will be needed depending on current user state. 
if ($user) { 
$logoutUrl = $facebook->getLogoutUrl(); 
} else { 
$loginUrl = $facebook->getLoginUrl(); 
} 

// This call will always work since we are fetching public data. 
$naitik = $facebook->api('/naitik'); 

?> 
<!doctype html> 
<html xmlns:fb="http://www.facebook.com/2008/fbml"> 
<head> 
<title>php-sdk</title> 
<style> 
    body { 
    font-family: 'Lucida Grande', Verdana, Arial, sans-serif; 
    } 
    h1 a { 
    text-decoration: none; 
    color: #3b5998; 
    } 
    h1 a:hover { 
    text-decoration: underline; 
    } 
</style> 
</head> 
<body> 
<h1>php-sdk</h1> 

<?php if ($user): ?> 
    <a href="<?php echo $logoutUrl; ?>">Logout</a> 
<?php else: ?> 
    <div> 
    Login using OAuth 2.0 handled by the PHP SDK: 
    <a href="<?php echo $loginUrl; ?>">Login with Facebook</a> 
    </div> 
<?php endif ?> 

<h3>PHP Session</h3> 
<pre><?php print_r($_SESSION); ?></pre> 

<?php if ($user): ?> 
    <h3>You</h3> 
    <img src="https://graph.facebook.com/<?php echo $user; ?>/picture"> 

    <h3>Your User Object (/me)</h3> 
    <pre><?php print_r($user_profile); ?></pre> 
<?php else: ?> 
    <strong><em>You are not Connected.</em></strong> 
<?php endif ?> 

<h3>Public profile of Naitik</h3> 
<img src="https://graph.facebook.com/naitik/picture"> 
<?php echo $naitik['name']; ?> 
</body> 
</html> 

回答

0

如果在某處出現語法錯誤,應檢查您的error_log。至於PHP條件,這只是用於編寫它們的替代語法。

PHP爲其控制結構提供了一種替代語法;即if,while,for,foreach和switch。在每種情況下,替代語法的基本形式是分別將大括號分別更改爲冒號(:)和結束大括號,以分別結束endif,endwhile,endfor ;, endforeach ;,或endswitch。

http://php.net/manual/en/control-structures.alternative-syntax.php