4

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);
        }
    }
}
Mihai Bujanca
  • 4,089
  • 10
  • 43
  • 84
  • possible duplicate of [PHP function goes into an infinite loop under certain conditions](http://stackoverflow.com/questions/16225700/php-function-goes-into-an-infinite-loop-under-certain-conditions) – pilsetnieks May 02 '13 at 17:56
  • @pilsetnieks As you can see, that is also my question, without a suitable answer. The difference here is that if I called getLikes after writeJSON, it worked. Here, it doesn't work. – Mihai Bujanca May 02 '13 at 18:01
  • i didn't know var_dump can be used liked that, i though var_dump wa mainly used on debugging. – Viscocent May 03 '13 at 13:37
  • @Viscocent well, I am using it for debugging. I am trying to make sure that the content of my array changes as needed. – Mihai Bujanca May 03 '13 at 17:42
  • haven't you tried if the var_dump() itself is causing the trouble? sometimes var_dump cause problems, like for me my app wont work if i use var_dump on a json return. btw could you not post the code for youre utils? – Viscocent May 04 '13 at 03:14
  • Without complete code we can't really help you as the loop may very well be somewhere else entirely. Post the complete solution (once I can run on my machine) and we probably will be able to help. – Tymoteusz Paul May 04 '13 at 07:22
  • @Viscocent I can post the code for my utils, if needed, but it is irrelevant, I just define the variables I need for all my app. For example the `user_id`. Somehow like a library. I am quite new to php, and I am still thinking a bit the OOP way, i might be wrong.. – Mihai Bujanca May 04 '13 at 08:56
  • from what i think, if your arrayForJson is returning null then i want to know how you construct that array in your arrayForJson variable. – Viscocent May 04 '13 at 09:02
  • @Puciek if you refer to utils.php, the same answer as the upper one. The rest of the code would be irrelevant, probably, as the only code I run is what I wrote in my question to obtain the loop :( – Mihai Bujanca May 04 '13 at 09:07
  • Most likely it is relevant as I do not see anything in this code that would cause an infinite loop. And it's more likely that you've made a bug somewhere else, than it is a php bug. – Tymoteusz Paul May 04 '13 at 09:09
  • @Puciek the only code I am running right now is php. Steps: 1. log in to facebook; 2.run getLikes; 3. run learning_globals – Mihai Bujanca May 04 '13 at 10:52
  • Can you please provide the code for utils.php? also you can try to use print_r() instead of var_dump and see if anything changes. – Amir May 07 '13 at 04:49
  • @Amir I edited my question. I will also try print_r, thanks – Mihai Bujanca May 08 '13 at 06:25

1 Answers1

1

I tend to stay away from var_dump(); and rather use PRE tags to dump and object to the screen. I also find that its a slower method for printing out an object. Try the following code snippet instead, hopefully it will resolve your issue with the timeout.

echo('<pre>');
print_r($arrayForJSON);
echo('</pre>');

I hope that helps!

Richard Niemand
  • 161
  • 1
  • 7