2012-01-11 70 views
-3

我想让我的团队的Flash项目在Facebook上,但我无法获得任何PHP-SDK示例的工作。我已经关注了几个例子,比如github上的这个新例子(here)。然而,我一直得到一个解析错误:语法错误,当使用Facebook PHP-SDK时出现意外的'{'

Parse error: syntax error, unexpected '{' in /home/content/r/a/s/rashomon/html/projects/facebook/test/hellomyflash/index.php on line 36

我下载了最新的PHP-SDK。这里是我的文件结构:

.../hellomyflash/ 

    index.php 

    src/base_facebook.php 

    src/facebook.php 

    src/fb_ca_chain_bundle.crt 

我在index.php中改变的唯一两行是appId和secret。

的index.php:

<?php 
/** 
* Copyright 2011 Facebook, Inc. 
* 
* Licensed under the Apache License, Version 2.0 (the "License"); you may 
* not use this file except in compliance with the License. You may obtain 
* a copy of the License at 
* 
*  http://www.apache.org/licenses/LICENSE-2.0 
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
* License for the specific language governing permissions and limitations 
* under the License. 
*/ 

require '../src/facebook.php'; 

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

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

// We may or may not have this data based on whether the user is logged in. 
// 
// If we have a $user id here, it means we know the user is logged into 
// Facebook, but we don't know if the access token is valid. An access 
// token is invalid if the user logged out of Facebook. 

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> 

我下面this例如,当得到了同样的 “意外”{“ 错误。

我在做什么错?

+4

那么'36行'是什么? SO不会为我们做行编号 – Jakub 2012-01-11 15:00:17

回答

5

您的服务器很可能运行PHP4,所提供的代码是针对PHP5的。

PHP4没有try/catch块。

+0

准确地说,第36行是'try {'。 – 2012-01-11 15:25:13

+0

看起来这是问题所在。现在,我是另一个问题。身份验证请求未触发。然后,我得到他们灰色的iframe,说:“该网站无法显示页面”。我使用http://developers.facebook.com/blog/post/503/的官方FB代码。我应该为此开始一个新线程吗? – user359519 2012-01-11 16:54:53

+0

是的,请做,而不是很多人希望帮助已解决的问题。 – 2012-01-11 17:15:48

0

如果你进入“facebook.php”和“base_facebook.php”,PHP的关闭标记缺失,页面底部的“?>”。我听说这对一些人造成了问题,并且当他们添加了密切的标签时,它得到了修复。你可以尝试;)

但看到从我的家伙的答案,这可能是正确的答案! :p