PHP使用Facebook API随机结束或中断

时间:2012-07-18 05:44:13

标签: php javascript html facebook facebook-opengraph

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

<?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'=&gt;”从那时起将其解释为html。所以我在浏览器中看到了这个:

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

对于奇怪的裁剪图片感到抱歉,但我无法透露业务的名称或意图。

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

谢谢!

1 个答案:

答案 0 :(得分:-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;
            }
        }