chomedey Posted April 28, 2010 Share Posted April 28, 2010 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> Quote Link to comment Share on other sites More sharing options...
chomedey Posted April 28, 2010 Author Share Posted April 28, 2010 btw, how do I format my code so it looks all nice and codey? I thought I had to put <code> tags around it, but it clearly didn't work. J Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted April 28, 2010 Share Posted April 28, 2010 , not <code>. Quote Link to comment Share on other sites More sharing options...
Baez Posted April 28, 2010 Share Posted April 28, 2010 or [ php ] for nice coloured syntax Quote Link to comment Share on other sites More sharing options...
chomedey Posted April 28, 2010 Author Share Posted April 28, 2010 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] Quote Link to comment Share on other sites More sharing options...
chomedey Posted April 28, 2010 Author Share Posted April 28, 2010 Still not working! Quote Link to comment Share on other sites More sharing options...
Mchl Posted April 28, 2010 Share Posted April 28, 2010 json_decode needs second argument to be true to return an array. Otherwise it returns StdClass object. Oh... and it's not [ php ] (Baez put those spaces so that you could see the tag) Quote Link to comment Share on other sites More sharing options...
chomedey Posted April 28, 2010 Author Share Posted April 28, 2010 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')"; Quote Link to comment Share on other sites More sharing options...
chomedey Posted April 28, 2010 Author Share Posted April 28, 2010 It has a second argument, no? Isn't that what file_get_contents is? Quote Link to comment Share on other sites More sharing options...
Mchl Posted April 28, 2010 Share Posted April 28, 2010 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')"; Quote Link to comment Share on other sites More sharing options...
chomedey Posted April 28, 2010 Author Share Posted April 28, 2010 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 ... Quote Link to comment Share on other sites More sharing options...
chomedey Posted April 28, 2010 Author Share Posted April 28, 2010 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? Quote Link to comment Share on other sites More sharing options...
Mchl Posted April 28, 2010 Share Posted April 28, 2010 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')"; Quote Link to comment Share on other sites More sharing options...
chomedey Posted April 28, 2010 Author Share Posted April 28, 2010 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" } Quote Link to comment Share on other sites More sharing options...
Mchl Posted April 28, 2010 Share Posted April 28, 2010 See my updated post above Quote Link to comment Share on other sites More sharing options...
chomedey Posted April 28, 2010 Author Share Posted April 28, 2010 Great! It's working. I'm not sure exactly what you changed, but will have a closer look. Thanks so much for your time. All the best, Julian Quote Link to comment Share on other sites More sharing options...
Mchl Posted April 28, 2010 Share Posted April 28, 2010 The code you posted first suggested, that all user details are inside 'me' node of JSON. However, they're not. They're on top level. Quote Link to comment Share on other sites More sharing options...
chomedey Posted April 28, 2010 Author Share Posted April 28, 2010 Great. Thanks again. Julian Quote Link to comment Share on other sites More sharing options...
tabacitu Posted May 12, 2010 Share Posted May 12, 2010 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. Quote Link to comment Share on other sites More sharing options...
chomedey Posted May 12, 2010 Author Share Posted May 12, 2010 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 Quote Link to comment Share on other sites More sharing options...
tabacitu Posted May 12, 2010 Share Posted May 12, 2010 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. Quote Link to comment Share on other sites More sharing options...
chomedey Posted May 12, 2010 Author Share Posted May 12, 2010 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.? Quote Link to comment Share on other sites More sharing options...
tabacitu Posted May 12, 2010 Share Posted May 12, 2010 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. Quote Link to comment Share on other sites More sharing options...
tabacitu Posted May 19, 2010 Share Posted May 19, 2010 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.