0
我正在致力於一個Facebook應用程序,我試圖寫在另一個php中打開的文件,但似乎沒有工作。我的代碼如下所示:警告:fwrite()期望參數1是資源,null給出
myphp.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);
}
}
}
是'utils.php'是在同一個目錄中如果'myphp.php'是什麼? – Rikesh 2013-05-01 08:30:12
如果您必須使用'global',那麼哪些函數的代碼是 – 2013-05-01 08:31:26
,您的包含中會出現問題。作爲一個測試,你能否把utils.php中提到的兩行放在$ likes = NULL行後面。 – herrjeh42 2013-05-01 08:31:44