Sort Of Fixed: Not receiving data from hosted PHP/JSON script.

KevinCrossKevinCross London, UKMember Posts: 1,894
edited August 2016 in Working with GS (Mac)

I've set up a PHP script that generates JSON data to save the date and time into a GameSalad table.

The output can be seen here: http://www.kevincross.co.uk/dateandtime.php

From what I can tell the output looks like it has everything that's mentioned in the cookbook.

In my project I've got one actor that has a "Network Get Table From URL" behaviour on it that points to that page and writes to a table called "DateTime". I've checked the id of the table by looking at the XML files (something that's not really mentioned in the cookbook). The id is id493612. This is the id I'm referencing in the output JSON code. On this actor is also two displays. One to display the 6 columns of the table (the date and time) and another to show the callback value.

When I run the attached project the table and callback values are still 0 so nothing is happening.

One thing I've noticed in the cookbook is that it says make sure you have a Pro account. Is this just something that should have been removed from the page a while ago? I didn't think you needed a Pro account to use certain behaviours anymore.

If you don't need a Pro account does anyone know what I might be doing wrong please?

I've attached the project that contains this one actor. I've also attached the PHP file that generates the JSON code. There's not much in the PHP file as I only need to access the time on the server.

EDIT: I attached an old version which had an old URL. New version attached now.

EDIT: I noticed that the callback variable was an index type and not integer so it would never have been set as -1. I've attached a new version again. Still doesn't work though. However I can see that it's trying to read the file because if I change the URL to http://www.kevincross.co.uk/dateandtime2.php (added a 2 to it) it will set callback as -1. When it references the correct URL nothing happens and it stays as 0.

Comments

  • KevinCrossKevinCross London, UKMember Posts: 1,894

    Is it something to do with the way I'm outputting the json code? I see in one of the examples (think the cookbook) that it just used echo which is what I'm doing but then I see examples (not on GameSalad) which uses PHP functions like json_encode and var_dump.

    I've dabbled quite a lot with PHP in the past creating database driven sites, even dabbled a little with json for a project, so wouldn't call myself a noob, but this ones stumped me as I can't see why GameSalad is unhappy with it.

    I'm like 95% finished with a project which isn't something I do often and the project won't work if I'm relying on the device clock so really want to understand this hurdle and overcome it.

  • zweg25zweg25 Member Posts: 738

    Seems like a problem with Gamesalad. Because if you request from any site it never has a successful callback. This may be a bug. I'd contact @stevej

  • KevinCrossKevinCross London, UKMember Posts: 1,894

    Fixed it! There was an extra comma in the string. Didn't see it before, but after taking a break and coming back I instantly spotted it. Working version attached if anyone wants to connect to their own server to get the time. I'm going to move the file on my server soon so if it doesn't work in the attached files you'll need to move the attached php to your own server.

  • KevinCrossKevinCross London, UKMember Posts: 1,894

    Noticed one more problem. It reads the date and time but it seems to store it in cache in GameSalad Creator and GameSalad Viewer because when you reload/refresh the app it shows the date and time it first loaded. It's not pulling through the latest time each time, even though the date and time is updated in the PHP page when loading it in a browser. I've closed both the Viewer and Creator fully but it doesn't make a difference.

    Is there a time delay between each time it will clear the cache and pull through new data from the server?

  • KevinCrossKevinCross London, UKMember Posts: 1,894

    @stevej do you know how long the data from a JSON feed stays in the cache in GameSalad Creator and Viewer, and a real published app? I closed GameSalad properly for an hour or two and when I started it up it pulled through the latest date and time. But now when I refresh it it shows the date and time that was first loaded into it like 10 minutes ago. I'm not saving data in the table and it is giving me a callback value of 1 so it is working.

    Was it planned to do this? Is it a bug? Is there a way to get it to update each time the URL is called?

  • KevinCrossKevinCross London, UKMember Posts: 1,894

    It seems to be an hour between each update of JSON data in GameSalad which would suggest it was probably programmed intentionally to do this instead of it being a bug.

    You'll have to keep this in mind if you create any games/apps which needs updated JSON data a lot more regularly than hourly. Maybe someone from GameSalad can confirm or correct me if I'm wrong.

    I can probably come up with a work around for my project using the device time as well as the server time by checking that there's no more than an hour or so difference between the two. It's when there's days differences between the two that it would seem like they're either cheating, or not connected to the internet. Both can be treated the same by not doing anything until the two are close together.

  • zweg25zweg25 Member Posts: 738

    @KevinCross said:
    It seems to be an hour between each update of JSON data in GameSalad which would suggest it was probably programmed intentionally to do this instead of it being a bug.

    You'll have to keep this in mind if you create any games/apps which needs updated JSON data a lot more regularly than hourly. Maybe someone from GameSalad can confirm or correct me if I'm wrong.

    I can probably come up with a work around for my project using the device time as well as the server time by checking that there's no more than an hour or so difference between the two. It's when there's days differences between the two that it would seem like they're either cheating, or not connected to the internet. Both can be treated the same by not doing anything until the two are close together.

    Probably smart to implement this anyway, because a user could always have his device on airplane mode or something

  • MentalDonkeyGamesMentalDonkeyGames Member Posts: 1,276
    edited August 2016

    FYI, @stevej is no longer working at GS.

    @jonmulcahy might now something about your issue. Based on what i've seen here on the forums, he's really good with this kind of stuff.

    Mental Donkey Games
    Website - Facebook - Twitter

  • jonmulcahyjonmulcahy Member, Sous Chef Posts: 10,408

    Try saving the json to a table and reading from that. I've never had any sort of delay when downloading and displaying data (which is really all it looks like your doing). If you make a change in my sample project and hit the download button, it's instantly changed.

    What rules do you have wrapped around your download button in GS?

    Some sample code on the GS side would really help

  • KevinCrossKevinCross London, UKMember Posts: 1,894
    edited August 2016

    @>; @jonmulcahy said:

    Try saving the json to a table and reading from that. I've never had any sort of delay when downloading and displaying data (which is really all it looks like your doing). If you make a change in my sample project and hit the download button, it's instantly changed.

    What rules do you have wrapped around your download button in GS?

    Some sample code on the GS side would really help

    I do write it to the table. And I don't have any rule around it just a timer that happens after 0.2 seconds after the scene is loaded. I have tried it with a touch is pressed rule but it made no difference. Can try again later though. What I'm finding is that when I view it in Creator or Viewer and refresh it or close it down and replay the project it reads the URL ok (callback = 1) but it doesn't update the table not until an hour later. I can refresh hundreds of times in that hour and it won't pull through the latest time until an hour has passed. The table is blank each time the project is run and before the URL is read so it's not saving old table data

    I get the impression that the Get JSON behaviour (whatever it's called) saves the JSON data in cache/a hidden temp file that has a life span of an hour (like web cookies) and while it accesses the URL if that file exists it won't overwrite it so when it does come to populating the table it's taking an old file and using that data.

    I've literally put together the smallest/simplest project (attached a couple of posts above) so I can't really see why it would act differently from other users experience with it all. It does the same with GameSalad Viewer too. I haven't tried a proper published app yet as I don't have a Pro account and haven't tried a GameSalad Arcade export yet either.

    I did refer to your tutorial and PHP files as well as the other page in the cookbook and various forum threads but didn't follow it fully as I didn't want to go through the hassle of setting up a local server again and I don't need access to MySQL tables. What I want is to just read the time on my GoDaddy hosted account and save it in a table to convert to a usable timestamp. I don't need to send or receive any other data to and from the server

    I want to stop the user from doing certain actions in the app for 24 hours between each time (the whole point of the app is to take that control away from the user) but where it's currently set up with the device time the user can just cheat when they're impatient.

    I can definitely work with an hour delay between each server time update using the device time too but it would be good to understand how it all works and know if the problem is just something I'm experiencing or not, so I know whether or not it could ever be used in a game or app where more regular data updates are needed.

  • zweg25zweg25 Member Posts: 738

    @MentalDonkeyGames said:
    FYI, @stevej is no longer working at GS.

    My apologies

    @KevinCross I used your gamesalad file and php file on my server and it updates instantly. Could it be possible that the your server is actually caching the script?

  • zweg25zweg25 Member Posts: 738

    Ill leave the script on there for today if you want to try: http://www.appitt.com/dateandtime2.php

  • KevinCrossKevinCross London, UKMember Posts: 1,894
    edited August 2016

    @zweg25 said:

    @MentalDonkeyGames said:
    FYI, @stevej is no longer working at GS.

    My apologies

    @KevinCross I used your gamesalad file and php file on my server and it updates instantly. Could it be possible that the your server is actually caching the script?

    @zweg25 said:

    @MentalDonkeyGames said:
    FYI, @stevej is no longer working at GS.

    My apologies

    @KevinCross I used your gamesalad file and php file on my server and it updates instantly. Could it be possible that the your server is actually caching the script?

    Interesting! Thanks for testing on your own. At least I know now where the problem lies. It does update when I refresh the page in the browser but I'll do a Google search for cached JSON data to see if I can amend it. It's probably a setting in a .htaccess file or something

    Thank you

  • KevinCrossKevinCross London, UKMember Posts: 1,894

    @zweg25 said:
    Ill leave the script on there for today if you want to try: http://www.appitt.com/dateandtime2.php

    @zweg25 said:
    Ill leave the script on there for today if you want to try: http://www.appitt.com/dateandtime2.php

    Thanks that'll be great. I can at least see then if it's a problem with my server settings if it works in my installation of GameSalad Creator and Viewer. I'll let you know once I've had a chance to check it so that you can remove it sooner if you want

  • KevinCrossKevinCross London, UKMember Posts: 1,894

    @zweg25 I've tested the script on your server and you're right it's working fine. Thanks for putting it up there. You can remove it now if you want.

    Once I find the fix that needs to be made to my site I'll share it here

  • KevinCrossKevinCross London, UKMember Posts: 1,894

    Found the solution to clearing the cached PHP/JSON data.

    Add these two lines to the top of the PHP page:

    header("Cache-Control: no-cache, must-revalidate");
    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

    New version of the files attached. The URL in the GameSalad file will need replacing before it will work.

  • zweg25zweg25 Member Posts: 738

    Nice! What does the expire header do?

  • KevinCrossKevinCross London, UKMember Posts: 1,894

    Works the same way as a web cookie. If you set it with a date and time in the past it will destroy once it's used. Might not have needed it but left it in just incase

Sign In or Register to comment.