Sending and Receiving data using your own SQL Server - PHP file and TestProject included

2456715

Comments

  • jonmulcahyjonmulcahy Member, Sous Chef, PRO Posts: 10,406
    @StormyStudio

    no problem. when working with the JSON data, i find converting it to an array first really helps.

    Do you have a sample format of the GS table you are trying to store it in? That will also help with the conversion.

    I loaded the file up into my test machine and converted it to an array, you can see it below. If you can send over a sample JSON coming out of GS I can take a look.

    Array ( [id] => 8345679096 [name] => Steve Paper [first_name] => Steve [last_name] => Paper [link] => https://www.facebook.com/paper4000 [birthday] => 03/15/1920 [hometown] => Array ( [id] => 198675897641883604 [name] => Kent, Kent, United Kingdom ) [location] => Array ( [id] => 124533723732263369 [name] => Maidstone ) [quotes] => life is like a box of chocolates [education] => Array ( [0] => Array ( [school] => Array ( [id] => 10854349232221 [name] => Grovewood School & Sixth Form College ) [type] => High School ) [1] => Array ( [school] => Array ( [id] => 118788887563124 [name] => Grovewood School & Sixth Form Centre ) [type] => High School ) [2] => Array ( [school] => Array ( [id] => 1490222939342 [name] => Bournemouth ) [concentration] => Array ( [0] => Array ( [id] => 17098899702935 [name] => Pre-Animation and Illustration ) ) [type] => College ) ) [gender] => male [email] => name@yahoo.co.uk [timezone] => 0 [locale] => en_GB [verified] => 1 [updated_time] => 2013-11-01T15:37:45+0000 [username] => Paper4000 )
  • StormyStudioStormyStudio United KingdomMember Posts: 3,989
    wow… that was quick..

    I don't actually want all this information as this is the user who would already have the game (although still useful for allowing a player to post to their own wall form the game).

    That's just the test data I'm setup to receive at the moment.

    Things that I think would be of use in a game or game menu are.

    [gender]
    [name]
    [first name]
    [second name]
    [link] as I'm pretty sure with this and the access code we can invite people to play games.
    [email]
    [id] - for when we get the chance to check this against the Facebook id's on Togethers network.

    I need to tweak what I'm asking gamesalad for as I should be able to request the above but for all of a users friends as well as their own. So lots of very useful info.

    Once we've got it we should be able to.

    a) send them an invite to install the game and play (already found the rough code for this)

    b) at some point compare the [id] with the Facebook [ids] on Playstogether to see which friends already play the game. (not sure if we could do this now or not)

    c) Post scores and info to the players wall



    I'll work out how to only get the info I want (all friends) then send that over and see what you make of it. I'm assuming there isn't a tidy 5 lines of code I can put under the data variable to tell it to go into an array and get repackaged as required…

    Thanks Jon

  • RiffelRiffel Member Posts: 1,272
    ~X(
    can't see the json.txt file...

    "After that, hit the send button. When you see the send status turn green, your data has reached the server. At this point if you browse to the location of asyncText.php you will see a json.txt file"



    http://localhost:8888/MAMP/asyncTest.php

    return this

    {"Properties":[],"Name":"","Children":[{"Properties":[{"Name":"rowCount","Value":0},{"Name":"columnCount","Value":3},{"Name":"0-1-name","Value":"playerID"},{"Name":"0-1-type","Value":1},{"Name":"0-2-name","Value":"playerName"},{"Name":"0-2-type","Value":1},{"Name":"0-3-name","Value":"playerstats"},{"Name":"0-3-type","Value":2}],"Name":"id911451_headers","Children":[]},{"Properties":[],"Name":"id911451","Children":[]}]}
  • StormyStudioStormyStudio United KingdomMember Posts: 3,989
    Are you now using jonmulcahy's updated script file?

    Once you've sent the table there should be a json.text file in the same folder as the asyncTest.php file.
  • StormyStudioStormyStudio United KingdomMember Posts: 3,989
    Well after spending a few hours tinkering with the facebook access codes and php that I don't really understand I'm more confused than before. Simply don't understand enough scripting myself, nor can I find an explanation that makes sense to get it done. Will try again tomorrow...
  • jonmulcahyjonmulcahy Member, Sous Chef, PRO Posts: 10,406
    Well after spending a few hours tinkering with the facebook access codes and php that I don't really understand I'm more confused than before. Simply don't understand enough scripting myself, nor can I find an explanation that makes sense to get it done. Will try again tomorrow...
    it'll come to you. I can't tell you how many times i looked at the JSON data before it 'clicked'

    what are you having trouble with? the facebook side of things or the GS side of things?


  • StormyStudioStormyStudio United KingdomMember Posts: 3,989
    It's the Facebook side… like you say it will just click at some point… some has for me to get this far…

    ...but I think a late stint last night going through your MAMP tut then looking into the Facebook stuff, then a 9 month old crying for the rest of the night (it was my turn to be up with him) followed by a days corporate animation have all come together to severely brake my noggin. (noggin… never used that word before).

    Here's a snippet of the code I've been staring at for hours, changing, testing, changing back….


    ……………
    function getAccessTokenDetails($app_id,$app_secret,$redirect_url,$code) { $token_url = "https://graph.facebook.com/oauth/access_token?" . "client_id=" . $app_id . "&redirect_uri=" . urlencode($redirect_url) . "&client_secret=" . $app_secret . "&code=" . $code; $response = file_get_contents($token_url); $params = null; parse_str($response, $params); return $params; } function getUserDetails($access_token) { $graph_url = "https://graph.facebook.com/me?access_token=". $access_token; $user = json_decode(file_get_contents($graph_url,TRUE)); return $user; return null; } $user = getUserDetails($access_token_details['access_token']); if($user) { $user_json=json_encode($user); //print $user_json; print "id: "+ $user['id']; $myFile = "stormys_json.txt"; $fh = fopen($myFile, 'w') or die("can't open file"); //$stringData = json_encode($_POST); $stringData = $user_json; fwrite($fh, $stringData); fclose($fh); //save $user_json to database. }


    The main problem is I can't work out why its giving me so much data…in the login page where I declare what I want access to, I've simply asked for id and email and friends_list.

    But I think you get a lot more of the own users stuff either way but that seems wrong.

    I'm not sure if part of the problem is that I'm doing it on a local server and only certain info is allowed that way (i.e. not a friends list). Or the code is simply wrong… thinking the 'me?' in:

    $graph_url = "https://graph.facebook.com/me?access_token=". $access_token;

    could be changed as well.

    Also I've added 'TRUE' in the above code at
    $user = json_decode(file_get_contents($graph_url,TRUE));
    thinking that might make it into an array…

    Really I should probably just spend a few nights brushing up on basic PHP…
  • App_MakerApp_Maker Member, PRO Posts: 64
    Does this guide mean when Gamesalad updates the nightly builds to allow us to input our own server we can create a multiplayer game with all the same features as Playstogether.com without actually using them at all. Will there be certain things we won't be able to do with our own server?
  • jonmulcahyjonmulcahy Member, Sous Chef, PRO Posts: 10,406
    Does this guide mean when Gamesalad updates the nightly builds to allow us to input our own server we can create a multiplayer game with all the same features as Playstogether.com without actually using them at all. Will there be certain things we won't be able to do with our own server?
    right now you can really do anything you want using tables. if you can dream it up it can be done with the right server side code.

    it will be even easier when they have the few new behaviors to only send certain rows.
  • jonmulcahyjonmulcahy Member, Sous Chef, PRO Posts: 10,406
    @stormystudio, yea it's a pain, and what you're trying to do with Facebook is all above my head :)
    I would think that this would work though:

    function getUserDetails($access_token) { $graph_url = "https://graph.facebook.com/me?access_token=". $access_token; // get facebook JSON $fb_data = file_get_contents($graph_url,TRUE); // convert JSON to an array $fb_array = json_decode($fb_data, TRUE); // format array to view it $fb_clean_array = print_r($fb_array,true); // save array to file to very contents file_put_contents($fb_clean_array,$results); return null; }

    I just converted some of my code, and I was able to get every individual data piece from that original JSON, skipping over education. That wouldn't be too difficult to get, but would require a loop I didn't want to code:

    <?php // needed if you enable the debugging secton below $sqlErrorLog = "sqlErrors.txt"; $jsonFile = "sjson.txt"; $fullArray = 'fullArray.txt'; $jsondata = file_get_contents($jsonFile); // convert JSON to an array $array = json_decode($jsondata, TRUE); // formats the array to view it easier $results = print_r($array,true); file_put_contents($fullArray,$results); // get array values $id = $array['id']; $name = $array['name']; $first_name = $array['first_name']; $last_name = $array['last_name']; $link = $array['link']; $birthday = $array['birthday']; $hometown_id =$array['hometown']['id']; $hometown_name = $array['hometown']['name']; $location_id = $array['location']['id']; $location_name = $array['location']['name']; $quotes = $array['quotes']; $gender = $array['gender']; $email = $array['email']; $timezone = $array['timezone']; $locale = $array['locale']; $verified = $array['verified']; $updated_time = $array['updated_time']; $username = $array['username']; // create string with values $data_return = $id ." " . $name ." " . $first_name ." " . $last_name ." " . $link ." " . $birthday ." " . $hometown_id ." " . $hometown_name ." " . $location_id ." " . $location_name ." " . $quotes ." " . $gender ." " . $email ." " . $email ." " . $timezone ." " . $locale ." " . $verified ." " . $updated_time ." " . $username; // display string on screen echo $data_return; // dump string to file file_put_contents("data.txt",$data_return); ?>

    that returns this:

    8345679096 Steve Paper Steve Paper https://www.facebook.com/paper4000 03/15/1920 198675897641883604 Kent, Kent, United Kingdom 124533723732263369 Maidstone life is like a box of chocolates male name@yahoo.co.uk name@yahoo.co.uk 0 en_GB 1 2013-11-01T15:37:45+0000 Paper4000
  • jonmulcahyjonmulcahy Member, Sous Chef, PRO Posts: 10,406
    new version of the test file, link above has also been updated.

    this is built on top of .220, so it no longer requires plays together to work.
  • StormyStudioStormyStudio United KingdomMember Posts: 3,989
    edited January 2014
    Thanks Jon… I'll take a look at what you did there in a little more detail before work starts today. (corporate video about Oreo biscuits/cookies… fingers crossed for some free samples)

  • EireStudiosEireStudios Member Posts: 451
    Did someone say free Oreos? I knew I should have studied animation in college :(

    Thanks for your help yesterday mate, you can delete those things now I don't think I'll need them anymore :)
  • StormyStudioStormyStudio United KingdomMember Posts: 3,989
    @EireStudios Are you sure? have you got your own Playstogether account up n running?
  • EireStudiosEireStudios Member Posts: 451
    No but all I wanted it for was to study the SendTable and GetTable but now that playstogether isn't needed for that anymore I don't really need it!

    Thanks again mate! :)
  • StormyStudioStormyStudio United KingdomMember Posts: 3,989
    edited January 2014
    @EireStudios … Well at least I helped for a few hours. :-) I'll leave it there for a bit incase you want to test the other playstogether stuff too.

    @jonmulcahy … I've just sent you a PM.. I managed to mix the two bits of code you gave me to be able to save out any preferred values from the incoming array into the .txt file. Then I figured out the needed wording to bring in a Facebook users friends list with IDs. into the array. But can't work out how to call the new values I'm now bringing in. This may be to do with the number of rows (Facebook users) in the array, rather than just the one user before.
  • The_Gamesalad_GuruThe_Gamesalad_Guru Member Posts: 9,919
    As you are all proving I said to people you could do Facebook with this stuff but it requires work and study like you guys are doing. Too many people want it to be easy but this stuff is only going to get harder and require more work. This is where the men are separated from the boys. Perfect example of life. Success is achiveved by the driven, period. There is no substitute for effort!
  • StormyStudioStormyStudio United KingdomMember Posts: 3,989
    @FryingBaconStudios … we only do it to prove you right :-)

    If anyone fancies learning some PHP and MySQL stuff that would be great.

    There's lots of goodness in this general mySQL PHP tutorial that I keep spinning through.

    http://zetcode.com/databases/mysqlphptutorial/
  • StormyStudioStormyStudio United KingdomMember Posts: 3,989
    edited January 2014
    They were indeed helpful… Can now write a lovely text file with the friends list and ids separated up clearly… now to get it into the database… and out to GameSalad where the real fun will begin… Wa ha ha ha!… absolutely loving the power and potential of this stuff. (will be a while before I get another big chunk done, day job calling again and pub quiz tonight which is always dangerous)

    thanks again for your stirling work.
  • EireStudiosEireStudios Member Posts: 451
    @jonmulcahy Is there anyway to overwrite a MySQL table value? i.e if I SendTable I would like it to overwrite the previous table value instead of creating a new row.
  • jonmulcahyjonmulcahy Member, Sous Chef, PRO Posts: 10,406
    @jonmulcahy Is there anyway to overwrite a MySQL table value? i.e if I SendTable I would like it to overwrite the previous table value instead of creating a new row.
    yes, there are a few ways you could do it. you could drop and recreate the table, or update the table based on a unique key for each row. you'll want to google mysql commands and take a look, i have not implemented anything like that yet.
  • StormyStudioStormyStudio United KingdomMember Posts: 3,989
    edited January 2014
    Latest update on my facebook progress.
    Can successfully write the facebook data to a MySQL table.

    I've also been trying to get it sent to GS but hit an intelligence road block.

    @jonmulcahy in your asyncTest.php script in the section where we reconstruct the JSON.


    // construct the JSON response

    // this is the header of the JSON return. It will have to be adjusted to match whatever your app is expecting. We have to define this here to get the row count above.
    $jsonHeadher='{"Properties":[],"Name":"","Children":[{"Properties":[{"Name":"rowCount","Value":'.$arrlength.'},{"Name":"columnCount","Value":3},{"Name":"0-1-name","Value":"playerID"},{"Name":"0-1-type","Value":1},{"Name":"0-2-name","Value":"playerName"},{"Name":"0-2-type","Value":1},{"Name":"0-3-name","Value":"playerstats"},{"Name":"0-3-type","Value":2}],"Name":"id911451_headers","Children":[]},{"Properties":[';

    // this is the footer of the JSON return. Again it will have to be adjusted to match whatever your app is expecting.
    $jsonFooter='],"Name":"id911451","Children":[]}]}';





    When I my empty target table form GameSalad get


    {"Properties":[],"Name":"","Children":[{"Properties":[{"Name":"rowCount","Value":1},{"Name":"columnCount","Value":3},{"Name":"0-1-name","Value":"Name"},{"Name":"0-1-type","Value":1},{"Name":"0-2-name","Value":"Facebook_ID"},{"Name":"0-2-type","Value":2},{"Name":"0-3-name","Value":"Playstogether_ID"},{"Name":"0-3-type","Value":2}],"Name":"id390467_headers","Children":[]},{"Properties":[{"Name":"1","Value":"||0|0|"}],"Name":"id390467","Children":[]}]}



    This is my attempt at putting the correct JSON Header and Footer in place.

    I had to delete some information from this to make it look the same as your original.

    Namely, the Value 1 became $arrlength.

    and at the end I removed [{"Name":"1","Value":"||0|0|"}].


    Here's what I came up with...



    // construct the JSON response

    // this is the header of the JSON return. It will have to be adjusted to match whatever your app is expecting. We have to define this here to get the row count above.

    //MINE
    $jsonHeadher='{"Properties":[],"Name":"","Children":[{"Properties":[{"Name":"rowCount","Value":'.$arrlength.'},{"Name":"columnCount","Value":3},{"Name":"0-1-name","Value":"Name"},{"Name":"0-1-type","Value":1},{"Name":"0-2-name","Value":"Facebook_ID"},{"Name":"0-2-type","Value":2},{"Name":"0-3-name","Value":"Playstogether_ID"},{"Name":"0-3-type","Value":2}],"Name":"id390467_headers","Children":[]},{"Properties":[';


    // this is the footer of the JSON return. Again it will have to be adjusted to match whatever your app is expecting.

    $jsonFooter='],"Name":"id390467","Children":[]}]}';




    GameSalad is definitely trying to GET the table, as if I put an intentional error in the target address I get a -1 error code back... but without errors it stays at 0.

    any help very much appreciated.

  • RiffelRiffel Member Posts: 1,272
    all ok in the backend, but the GS app don't reach the asyncTest.php
    Can I see what the GS app is calling?
  • jonmulcahyjonmulcahy Member, Sous Chef, PRO Posts: 10,406
    @riffell
    check the logs under applications\MAMP\logs and see if there is anything there

    also check to the URL attributes in the game match the URL
  • jonmulcahyjonmulcahy Member, Sous Chef, PRO Posts: 10,406
    @stormystudio are you writing the final response back to the page when complete?

    // removes an extra comma that the loop above leaves behind $jsonBody=rtrim($jsonBody, ","); // constructing the full JSON return $returnedJson=$jsonHeadher.$jsonBody.$jsonFooter; // write the JSON data so the app can read it. echo $returnedJson;

    that last echo line is what the app looks for
  • StormyStudioStormyStudio United KingdomMember Posts: 3,989
    edited January 2014
    yep... doing that bit. (did'nt include it above to try and keep it tidier)
  • StormyStudioStormyStudio United KingdomMember Posts: 3,989
    edited January 2014
    I can PM a link to my whole page of code, if your willing? :-)
  • jonmulcahyjonmulcahy Member, Sous Chef, PRO Posts: 10,406
    I can PM a link to my whole page of code, if your willing? :-)
    send it over!
  • StormyStudioStormyStudio United KingdomMember Posts: 3,989
    Done...
Sign In or Register to comment.