警告:fwrite()期望参数1为resource,给定null

时间:2013-05-01 08:26:06

标签: php file variables undefined fwrite

我正在开发一个Facebook应用程序,我正在尝试写一个在另一个php中打开的文件,但似乎不起作用。我的代码如下所示:

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);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

很难分辨哪个类似错误的错误会导致此错误,但代码显然必须是

utils.php

ini_set('display_errors',1);
error_reporting(E_ALL);

$myfile="testson.json";
//other variables

<强> myphp.php

require 'utils.php';
require_once('sdk/src/facebook.php');
require_once("AppInfo.php");
if($user_id) {
    $likes = idx($facebook->api('/me/likes'), 'data', array());
    if ($likes) {
        $arrayForJSON['likes']=$likes;
        file_put_contents($myfile,json_encode($arrayForJSON));
        var_dump($arrayForJSON);
    } else {
        echo "Wrong user id";
    }
}
else
    echo "User not logged in";