tags:

views:

159

answers:

1

Hi Stackoverflowers.

I'm using the Facebook php-sdk to get the users name and friends, right now the loading friends part takes about +3 seconds so I wanted to do it via Ajax, e.g. so the document can load and jQuery then calls an external PHP script which loads the friends (their names and their profile pictures). So to do this I did:

    $(document).ready(function() {
      var loadUrl = "http://localhost/fb/getFriends.php" ;
      $("#friends")
       .html("Hold on, your friends are loading!")
       .load(loadUrl);
    });

But I get a PHP error:

Fatal error:  Call to a member function api() on a non-object

If I do this in the same PHP file (so I don't use Ajax at all to call it) it works fine.

Now I think I understand the reason this is happening, but I don't know how to fix it. In my main index.php file I have a bunch of init and session code e.g.

    FB.init({
      appId   : '<?php echo $facebook->getAppId(); ?>',
      session : <?php echo json_encode($session); ?>, // don't refetch the session when PHP already has it
      status  : true, // check login status
      cookie  : true, // enable cookies to allow the server to access the session
      xfbml   : true // parse XFBML
    });

So I'm just wondering what is the best way to treat my new separate PHP file getFriends.php in a way where it has access to all PHP/JavaScript session data/variables?

If you haven't used the Facebook php-sdk I'll quickly explain what I mean: Lets say I have index.php and getUsername.php, from index.php I want to retrieve the getUsername.php file via Ajax using .load. Now the problem is getUsername.php needs to access PHP session data/Javascript Init functions which were created in index.php, so I'm thinking of ways to solve this (I'm new to PHP so sorry if this sounds silly) but I'm thinking maybe I could do a POST in jQuery Ajax and post the session data? Or maybe I could create a PHP class, so something like:

class getUsername extends index{} /*Yes I'm a newbie*/

If you have a look at the php-sdk example.php link posted at the top maybe you'd better understand what variables exactly need to be accessed from a new file.

Also on a different note, I'm using PHP to work out page rendering times and it seems that fetching the users name alone :

// Session based API call.
if ($session) {
  try {
    $uid = $facebook->getUser();
    $me = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
  }
}

Can take a good 4 seconds, is this normal? Once I get the users details is it good to cache it or something? -Speed isn't as important right now, for now I'm just trying to figure out this Ajax-separating php files thing.

Woah this is a long post.

Thanks very much for your time.

A: 

First of all, sorry if I misunderstood you,

There is a way to send data trough the load function:

$("#target").load("test.php", { 'choices[]': ["Jon", "Susan"] } );

or

    $("#target").load("test.php", {limit: 25}, function(){
   alert("The last 25 entries in the feed have been loaded");
 });

Hope this helps...

PS: It's completely normal for the page to take a few seconds to respond... (I refer to the FB API only)

Lord Otori
Thanks, I'll play around with sending the session data that way and see if I can still access it from a separate file. Also the page itself loads in 0.0025 seconds without calling the api, but requesting the name of the current logged in user adds +3 seconds which I thought was strange.Thanks for your help!
Umar
that is perfectly normal, the good thing about it it's that the user can have something to look at while loading.
Lord Otori
Ok so I tried what you said with .load:.load(loadUrl, { 'fb[]': [<?php echo $session; ?>] }) When I check what PHP received it seems to be an empty array:Array( [0] => )Thanks for your input anyway, I'll keep trying.
Umar
{ 'fb[]': [<?php echo $session; ?>] }That is the way to POST Arrays, Use that one only if you've formatted $session before using it.Otherwise, if it's not an array, use {limit: 25} instead.Also check on the source (after executing it) what echoed with this: <?php echo $session; ?>If you're still having trouble with the .load function checkhttp://api.jquery.com/load/
Lord Otori
Oh, $session appears to be a PHP object, do you know how I could post that?
Umar
you'll need to check the structure of the object. You can do that with <pre><?php print_r($session); ?></pre>
Lord Otori
Hey you were right, I passed it as <?php echo $session['access_token']; and it worked! It seems for the Facebook API all you need is an access_token so that did the job. In the new PHP I accessed the POST data with: $_POST['token'] - I hope this way of doing things is efficient, thank you very much for your help!
Umar
YW, I'm glad it worked for you. :)
Lord Otori