0

嗨,我正在尝试使用 Facebook sdk for php,但遇到了一些问题。下面的代码直接从他们提供的示例中复制而来,但我什至无法显示要显示的脚本的 html 元素……这让我觉得某处存在语法错误。当我检查嵌入在 html 部分中的 php 时,我相信我看到了 if - else 语句的一些问题。例如,似乎有一个 : 当下面的行中应该有一个 { 时:

<?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>
4

1 回答 1

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

于 2013-07-12T20:00:02.267 回答