2013-05-01 66 views
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); 
     } 
    } 
} 
+0

是'utils.php'是在同一个目录中如果'myphp.php'是什么? – Rikesh 2013-05-01 08:30:12

+1

如果您必须使用'global',那么哪些函数的代码是 – 2013-05-01 08:31:26

+0

,您的包含中会出现问题。作为一个测试,你能否把utils.php中提到的两行放在$ likes = NULL行后面。 – herrjeh42 2013-05-01 08:31:44

回答

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"; 
+0

没有错误utils.php中 – 2013-05-01 08:43:29

相关问题