Jump to content

New facebook functionality


chomedey

Recommended Posts

Hi all,

 

I'm experimenting with the new facebook functionality, and I was wondering if anyone can tell me what is wrong with this?  I have never used JSON_decode before.  Does the resulting array function just like a normal array?  Very confused, as I seem to be just using a variant of the example code provided by fb.

 

Cheers.

 

J

 

<code>

 

dbc();

 

$cookie = get_facebook_cookie('118230918196091', 'e9a4fa6385c792cc4bf1e01bfa0eacbb');

 

$deepshare = $_POST['deepshare'];

 

$user = json_decode(file_get_contents(

    'https://graph.facebook.com/me?access_token=' .

    $cookie['access_token']))->me;

   

$query = "INSERT into data (deepshareID, userID, userName, deepshare, date_entered, deleted)

VALUES (0, '$user->id', '$user->name', '$deepshare', NOW(), 'N')";

 

</code>

 

 

 

Link to comment
Share on other sites

Thanks.

 

[ php ]

dbc();

 

$cookie = get_facebook_cookie('118230918196091', 'e9a4fa6385c792cc4bf1e01bfa0eacbb');

 

$deepshare = $_POST['deepshare'];

 

$user = json_decode(file_get_contents(

    'https://graph.facebook.com/me?access_token=' .

    $cookie['access_token']))->me;

   

$query = "INSERT into data (deepshareID, userID, userName, deepshare, date_entered, deleted)

VALUES (0, '$user->id', '$user->name', '$deepshare', NOW(), 'N')";

 

[/code]

Link to comment
Share on other sites

dbc();

$cookie = get_facebook_cookie('118230918196091', 'e9a4fa6385c792cc4bf1e01bfa0eacbb');

$deepshare = $_POST['deepshare'];

$user = json_decode(file_get_contents(
    'https://graph.facebook.com/me?access_token=' .
    $cookie['access_token']))->me;
    
$query = "INSERT into data (deepshareID, userID, userName, deepshare, date_entered, deleted)
VALUES (0, '$user->id', '$user->name', '$deepshare', NOW(), 'N')";

Link to comment
Share on other sites

If you prefer working on arrays, then this code would look like this:

 

dbc();

$cookie = get_facebook_cookie('118230918196091', 'e9a4fa6385c792cc4bf1e01bfa0eacbb');

$deepshare = $_POST['deepshare'];

$jsonDecoded = json_decode(file_get_contents(
    'https://graph.facebook.com/me?access_token=' .
    $cookie['access_token']), true);

$user = $jsonDecoded['me'];
  
$query = "INSERT into data (deepshareID, userID, userName, deepshare, date_entered, deleted)
VALUES (0, '{$user['id']}', '{$user['name']}', '$deepshare', NOW(), 'N')";

Link to comment
Share on other sites

That's helpful because I do prefer working with arrays.  But it is still not returning any values for {$user['id']}' and {$user['name']}'.

 

That would lead me to suspect there is something not quite right with the way I am implementing the facebook interaction, but as far as I can tell I am doing / have done exactly what they say I should do i.e. login (this is confirmed as I have the userID showing at the top of the page etc.)

 

Hmm ...

 

 

Link to comment
Share on other sites

When I paste the file_get_contents link (with access key) in to the browser I get:

 

{

  "id": "568305844",

  "name": "Julian Humphreys",

  "first_name": "Julian",

  "last_name": "Humphreys",

  "link": "http://www.facebook.com/julian.humphreys",

  "timezone": -4,

  "verified": true,

  "updated_time": "2010-01-25T23:23:06+0000"

}

 

So that means my code looks like this:

 


$jsonDecoded = json_decode(file_get_contents({
   "id": "568305844",
   "name": "Julian Humphreys",
   "first_name": "Julian",
   "last_name": "Humphreys",
   "link": "http://www.facebook.com/julian.humphreys",
   "timezone": -4,
   "verified": true,
   "updated_time": "2010-01-25T23:23:06+0000"
}), true);

$user = $jsonDecoded['me'];

echo $user['id'];
echo $user['name'];

 

Any ideas?

 

 

 

 

Link to comment
Share on other sites

do

var_dump($jsonDecoded)

to see if there's anything in this array.

If it's empty, there's probably something wrong with how you fetch data using file_get_contents()

 

[added]

 

Duh! There's no 'me' node in this JSON. FB example is wrong.

 

$user = json_decode(file_get_contents(
    'https://graph.facebook.com/me?access_token=' .
    $cookie['access_token']), true);

$query = "INSERT into data (deepshareID, userID, userName, deepshare, date_entered, deleted)
VALUES (0, '{$user['id']}', '{$user['name']}', '$deepshare', NOW(), 'N')";

Link to comment
Share on other sites

No - not empty.

 

I get this:

 

array(9) { ["id"]=>  string(9) "568305844" ["name"]=>  string(16) "Julian Humphreys" ["first_name"]=>  string(6) "Julian" ["last_name"]=>  string(9) "Humphreys" ["link"]=>  string(40) "http://www.facebook.com/julian.humphreys" ["birthday"]=>  string(5) "02/28" ["timezone"]=>  int(-4) ["verified"]=>  bool(true) ["updated_time"]=>  string(24) "2010-01-25T23:23:06+0000" }

Link to comment
Share on other sites

  • 2 weeks later...

hello there.

 

just wondering if anyone is having new problems with this method.

i was in quite a pickle before i found this forum and your info helped me out.

it worked for a few days, but last night the whole thing just crashed for no reason.

 

after manual check of the url+authtoken i got "You must use https:// when passing an access token"

 

so i tried changing it to https... doesn't work.

 [function.file-get-contents]: failed to open stream: No such file or directory in ...

 

 

anybody else having this problem?

 

i'm just using plain old json_decode and file_get_contents - which worked well before last night (without https though)

$user = json_decode(file_get_contents('https://graph.facebook.com/me?access_token='.$cookie['access_token']), true);

 

p.s. tried manual url with https - it works.

Link to comment
Share on other sites

I found the fb:login script to be unreliable so I added some code around it to avoid the error message you're getting.  My opening code now reads:

 


define('FACEBOOK_APP_ID', '[APP NUMBER HERE]');
define('FACEBOOK_SECRET', '[sECRET CODE HERE]');

$cookie = get_facebook_cookie(FACEBOOK_APP_ID, FACEBOOK_SECRET);

if (@file('https://graph.facebook.com/me?access_token=' .
    $cookie['access_token']) == false) {
$cookie='';
} else {

$user = json_decode(file_get_contents(
    'https://graph.facebook.com/me?access_token=' .
    $cookie['access_token']), true);
    
$logged_in_user = $user['id'];
$logged_in_user_name = $user['name'];

}

// Get logged_in_user's friends

if(@file('https://graph.facebook.com/me/friends?fields=id&access_token=' .
    $cookie['access_token']) == false) {
$cookie='';
} else {
    
$jsonDecoded = json_decode(file_get_contents(
    'https://graph.facebook.com/me/friends?fields=id&access_token=' .
    $cookie['access_token']), true);

$friends = $jsonDecoded['data'];

$formatted_friends=implodeIds($friends);

}

 

So if the file is not there for any reason, it sets the cookie to NULL, asking the user to login.  And if the file is there everything works great.

 

Solved the problem for me.

 

Let me know if it works for you.

 

Julian

Link to comment
Share on other sites

thank you for the reply.

 

unfortunately, i've tried it your way and it still doesn't work.

 

well basicly what you've done is see whether or not the graph file exists, thus concluding if it's worth getting contents.

well that's the whole problem: mine doesn't see the file, so it can't fetch the data. but the file IS there (manually checked the url and it's ok).

 

what bugs me is that it was working perfectly last night, when i was accessing "HTTP://graph.facebook.com/me?access_token=".

now http won't work, but neither does https for some reason.

 

i understand why http doesn't - facebook no longer supports it. https though...

 

i've searched the internet high and low and all i've come up with is that for some reason PHP does not accept https as a Registered PHP Stream. if you're saying that yours works, could you please phpinfo() and tell me if you do have https as a registered stream? my hosting service has not responded to my ticket yet.

 

other than that, i don't know what the problem is. i don't understand why it doesn't work.

 

basicly, your method doesn't work for me because it practicly eliminates the one thing that does work - the facebook cookie.

every time.

 

i really don't know what to try next.

thanks again for the reply.

Link to comment
Share on other sites

Hi,

 

Sorry it's taken so long for me to get back to you.  Was building bookshelves all day.

 

Yes, I'm on justhost and it does have https as a registered stream.

 

Hope that helps.

 

J

 

P.S. You're sure it's not something to do with login, access token etc.?

 

 

Link to comment
Share on other sites

Hello

 

It does help, thank you.

I've checked my phpinfo and several other hosting services i use for other websites.

 

My conclusion is this:

In order for this way of working with facebook to actually work you need:

 

https - as a Registered PHP Stream

allow_url_fopen - On

Curl

Json

 

----

 

Now the problem i'm facing is that out of my 3 hosts, none offers all of them.

the closest i got was allow_url_fopen, curl and json but no HTTPS.

 

Of course that none of the tech departments were any help, but one did say this (and this is from the host i was working from all along):

 

"Unfortunately, you can not edit php.ini

But you can change most of PHP settings yourself by using php_flag values in .htaccess file and ini_set() ini_alter() PHP functions."

 

If only i knew how to do that...

 

Does anybody have any idea how i can make HTTPS a Registered PHP Stream using .htaccess and ini_set, ini_alter?

 

Thanks again, Julian, you've been very helpful.

Link to comment
Share on other sites

Ok, so I was right.

 

What I needed was another host. Found one with HTTPS as Registered PHP Stream and it works just fine.

Anybody else having this problem, just check to see if your host has everything I posted above.

 

Thanks for your help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.