0

我正在为 PHP 使用 Facebook openGraph API。这是我的代码:

<?php
session_start();

if (!isset($_SESSION['username'])) {

header("Location: index.php");
break;

}

$fbconfig['appid' ]     = "APPID";
$fbconfig['secret']     = "SECRET";
$fbconfig['baseurl']    = "http://blah.com/"; 

$user = null; 
try{
    include_once "facebook.php";
}
catch(Exception $o){
    error_log($o);
}
    // Create our Application instance.
try {
$facebook = new Facebook(array(
  'appId'  => $fbconfig['appid'],
  'secret' => $fbconfig['secret'],
  'cookie' => true,
));
} catch (Exception $e) { echo "ERROR: ".$e; } 

$user = $facebook->getUser();

$loginUrl   = $facebook->getLoginUrl(
        array(
            'scope'         => 'email,publish_stream,read_stream,user_birthday,user_location,user_work_history,user_about_me,user_hometown',
            'redirect_uri'  => $fbconfig['baseurl'].'blah.php'
        )
);



$logoutUrl = $facebook->getLogoutUrl(array( 'next' => ($fbconfig['baseurl'].'blah.php') ));


//if user is logged in and session is valid.
if ($user ){

    header("Location;$loginUrl");
    break;

}


?>

现在,这适用于其他页面(Facebook 部分),但仅适用于此页面(与其他页面相同的目录) 浏览器认为 PHP 以“'appId' =>”结尾,然后将其解释为 html。所以我在浏览器中得到了这个:

http://cl.ly/image/1y2a2F0e3I35

很抱歉这张奇怪的裁剪图片,但我不能透露这家公司的名称或意图。

真实站点上的PHP后面有HTML,如果需要我可以给这个+ js。

谢谢!

4

1 回答 1

-1
     your header is wrong.......try following......
        header("Location:$loginUrl");
        and also if you don't get facebook user id then you have to redirect to login page but you did reverse.may do following
    //if user is not loggin
        if (empty($user)){
            header("Location:$loginUrl");
        }else{
           try {
                $userInfo = $facebook->api("/$user");
            } catch (FacebookApiException $e) {
                $userInfo=null;
            }
        }
于 2012-07-18T06:27:01.967 回答