0

I am working on a facebook app and I am trying to write in a file that was opened in another php, but doesn't seem to work. My code looks like this:

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

Any idea why does this happen and how should I deal with it?


Complete 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);
        }
    }
}
Mihai Bujanca
  • 4,089
  • 10
  • 43
  • 84

1 Answers1

0

It's hard to tell which particular typo-like mistake causes this error, but the code apparently have to be

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";
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345