我正在开发一个 facebook 应用程序,我正在尝试写入一个在另一个 php 中打开的文件,但似乎不起作用。我的代码如下所示:
我的php.php
include 'utils.php';
require_once('sdk/src/facebook.php');
require_once("AppInfo.php");
$likes=NULL;
global $fileout,$myfile; //If I don't request the global ones, I got undefined variable
if($user_id) {
try {
if(is_null($likes))
$likes = idx($facebook->api('/me/likes'), 'data', array());
if ($likes) {
$arrayForJSON['likes']=$likes;
fwrite($fileout,json_encode($arrayForJSON));
}
}
catch(FacebookApiException $e){
echo error_log($e);
}
echo "done";
var_dump($arrayForJSON);
}
else
echo "User not logged in";
知道为什么会发生这种情况以及我应该如何处理它?
完成utils.php
require_once('sdk/src/facebook.php');
require_once("AppInfo.php");
/**
* @return the value at $index in $array or $default if $index is not set.
*/
ini_set('display_errors',1);
error_reporting(E_ALL);
function idx(array $array, $key, $default = null) {
return array_key_exists($key, $array) ? $array[$key] : $default;
}
function he($str) {
return htmlentities($str, ENT_QUOTES, "UTF-8");
}
$facebook = new Facebook(array(
'appId' => AppInfo::appID(),
'secret' => AppInfo::appSecret(),
'sharedSession' => true,
'trustForwarded' => true,
'file_upload' =>true
));
$user_id = $facebook->getUser();
$app_info = $facebook->api('/'. AppInfo::appID());
$app_name = idx($app_info, 'name', '');
if($user_id)
{
$logoutUrl =$facebook->getLogoutUrl();
}
else
{
$loginUrl=$facebook->getLoginUrl();
}
if ($user_id) {
try {
$permissions = $facebook->api('/me/permissions');
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
// If the call fails we check if we still have a user. The user will be
// cleared if the error is because of an invalid accesstoken
if (!$facebook->getUser()) {
header('Location: '. AppInfo::getUrl($_SERVER['REQUEST_URI']));
exit();
}
}
}
$myfile="testson.json";
$fileout=fopen($myfile,'w') or die("Fatal: Can't open JSON file for writing");
$token=$facebook->getAccessToken();
$arrayForJSON = array();
function getUpdatedTime()
{
global $facebook,$user_id,$arrayForJSON;
if($user_id) {
try {
$updated_time= idx($facebook->api('me/updated_time'), 'data', array());
if($updated_time) {
$arrayForJSON['updated_time']=$updated_time;
}
}
catch(FacebookApiException $e){
error_log($e);
}
}
}