I have the following files:
utils.php where arrayForJSON
is defined
getLikes.php:
include '../utils.php';
if($user_id) {
try {
if(is_null($likes))
$likes = idx($facebook->api('/me/likes'), 'data', array());
if ($likes) {
$arrayForJSON['likes']=$likes;
}
}
catch(FacebookApiException $e){
echo error_log($e);
}
var_dump($arrayForJSON);
}
else
echo "User not logged in";
Which results in showing the content of $arrayForJSON
.
I now have another file with the following content:
learning_globals.php
<?php
include 'utils.php';
var_dump($GLOBALS['arrayForJSON']);
?>
Now, if I run this file, AFTER running getLikes
, it runs until timeout. If I run it BEFORE, it returns null. Same result if I run the files in reverse order.
What should I do..? I am learning php currently and I got a little bit stuck.
Edit: not a dupicate-
The suggested answer is not suitable, and was not a good answer to that question- the problem does not seem to have anything to do with facebook's api. On the other hand, the difference here is that even if I call first learning_globals.php it shows null and calling getLikes.php results again in trying until timeout.
I hope now everything is clear
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.
*/
function idx(array $array, $key, $default = null) {
return array_key_exists($key, $array) ? $array[$key] : $default;
}
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
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();
}
}
}
$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);
}
}
}