我的php文件是:
<?php
session_start();
if (!class_exists('FacebookApiException')) {
require_once('facebook.php' );
}
$facebook = new Facebook(array(
'appId' => $appId,
'secret' => $appSecret,
));
$fbuser = $facebook->getUser();
if ($fbuser) {
try {
$user_interest = $facebook->api('/me/movies');
echo '<pre>';
//print_r($user_interest);
}
catch (FacebookApiException $e) {
echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
$user = null;
}}
?>
我想在php文件中使用$ user_interest和html代码中的其他var: 我试着按照以下方式给出错误$ user_interest not defined。
<?php
session_start();
$appId = '619';
$appSecret = 'myverybeatuifuldaisygiveusmilk'; // Facebook App Secret
$return_url = 'http://yoursite.com/connect_script/'; //path to script folder
$fbPermissions = 'publish_stream,email'; //more permissions : https://developers.facebook.com/docs/authentication/permissions/
require_once('facebook.php' );
$facebook = new Facebook(array(
'appId' => $appId,
'secret' => $appSecret,
));
$fbuser = $facebook->getUser();
$user_interest = $facebook->api('/me/movies');
?>
<!DOCTYPE html>
<html xmlns:fb="http://www.facebook.com/2008/fbml" xml:lang="en-gb" lang="en-gb" ><head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<title>JS/Ajax Facebook Connect</title>
<script>
function AjaxResponse()
{
var myData = 'connect=1';
$.ajax({
type: "POST",
url: "process_facebook.php",
data: myData
}).done(function(result) {
$("#fb-root").html(result);
});
}
</script></head><body>
<div id="fb-root"></div>
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({
appId: '<?php echo $appId; ?>',
cookie: true,
xfbml: true,
// channelUrl: '<?php echo $return_url; ?>channel.php',
oauth: true});};
document.write($user_interest);
(function() {
var e = document.createElement('script');
e.async = true;
e.src = document.location.protocol +'//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);}());
function CallAfterLogin(){
FB.login(function(response) {
if (response.status === "connected")
{
LodingAnimate(); //Animate login
FB.api('/me', function(data) {
console.log($user_interest);
});
} }); }
</script>
</body>
</html>
答案 0 :(得分:2)
尝试更改行
console.log($user_interest);
为:
console.log('<?php echo $user_interest; ?>');
答案 1 :(得分:2)
您正在使用php上下文之外的变量$ user_interest。 它应该放在php-Tags中,如此:
<?php echo $user_interest; ?>