Welcome!

Join our community of MMO enthusiasts and game developers! By registering, you'll gain access to discussions on the latest developments in MMO server files and collaborate with like-minded individuals. Join us today and unlock the potential of MMO server development!

Join Today!

How do I? JSON -> PHP/JS for new data entry.

Revolution-Entrepreneur
Joined
Apr 2, 2009
Messages
451
Reaction score
19
Hi guys! It's been a long time since I've touched programming lol.. but I've been keeping on track with the latest web development trends, just not in practice.

well, currently I have a mini project for fun,
but I'm stuck on Google search with fruitless results, and really need some help!

im trying to "spoof" a data from an android app, retrieving data on new events , it will show latest events on the app, ive reversed the app and traced the source, its pushing data from Google's fire base servers. I've gotten the link and am able to copy those data and show it on a php site.

but, in order to be notified when there is a new event, I need the php site(whatever it may be, php, HTML or angular) to run a script to send a notification to my gmail account. I need it to ONLY send when there is new updates from json. How would I do that??

thank you in advance!!!
 
Put Community First
Member
Joined
Oct 2, 2014
Messages
1,112
Reaction score
832
Are you just wanting an email saying 'a new event has been added' or are you wanting an email containing event details? That'd influence what answers you get.

JS cannot send emails on its own, so you'd need PHP for that.

If you're pulling data via JSON, on load of that URL you could count the events and check if it's more than an event counter, if so, send an email via AJAX request to a PHP mailer page.
 
Revolution-Entrepreneur
Joined
Apr 2, 2009
Messages
451
Reaction score
19
Sorry for the ambiguity, yes, I'm trying to send the entire event details. only the latest one to be exact.

As per AJAX request, I would still need a cron job task to be run, so that the script refreshes every 60-90 minutes?
Is it possible to set a trigger to execute the PHP script WHEN there's new data from the JSON ?
I could basically, run a script to create a text file and save the event details on it, and run a php script to compare the previous files based on its size/SHA1 encoding. TBH, the event isnt really updated that frequently (it's actually case files for my personal notification). It would be the WORST possible way in terms of efficacy, but I just need the job done.

I'm not quite familiar with AJAX, mind showing some light? :p
 
Put Community First
Member
Joined
Oct 2, 2014
Messages
1,112
Reaction score
832
Is it possible to set a trigger to execute the PHP script WHEN there's new data from the JSON?

Yes, that should be possible, just keep in mind that it'd be on the client-side since that's where JS lives :). For that to work you'd need a baseline count of the JSON data so you have the total of events, then x seconds run the PHP script to scrape the data, count that array, compare the two and have the script send the email. No need for a cron :).

Logic-wise, this is how I can see this being done:

1 - On page load, load JSON data array, count records and store result in a global variable.
2 - setInterval() every x seconds/minutes to load a function that reads JSON data array (reason for a function becomes clear below).
3 - In whichever function you have within setInterval() to load the array, store new count in a private variable, then compare the two.
4 - If the private variable is more than global, then there's a new event. AJAX-load the PHP and pass your new event data (at the end of the JSON array) into the URL of the PHP page.
5 - In the PHP mailer page, retrieve that last JSON array data via $_GET request (keep in mind JSON arrays are strings when read in PHP, and need conversion).
6 - Form your email code.

There are another perhaps two ways I can think of this being done, though if you can't tell by the above, how my brain works is a little 'different', lol.
 
◝(⁰▿⁰)◜Smile◝ (⁰▿⁰)◜
Developer
Joined
May 29, 2007
Messages
2,167
Reaction score
898
Re: How do I? JSON -> PHP/JS for new data entry.

Is it possible to set a trigger to execute the PHP script WHEN there's new data from the JSON ?
Long-polling would do the trick or you could keep looking up date from the server at a fixed rate. Like @fallenfate mentioned.

Btw you don't need angular since it's a front-end framework. You need a back-end.
Just use json_encode & json_decode for turning arrays & objects into valid Json input/output.

An OOP approach for handeling requests would help you with keeping stuff updated. It takes a little more time to develop something decent but this will payoff in the end.

If you don't want to write everything from scratch, then I recommend looking at Laravel or it's shrink-down brother Lummen for building an API.



You will need to cache the data somewhere. I recommend using an MD5 hash, then you don't need to store all the data. You just throw the content of the Json array trough the MD5 hash generator and if they differ, then something has changed.

These are just a hand full steps you have to do to get your system working. There are many solutions to this problem, it's up to you on how you implement this system.



A Github project I found that has PHP long-polling.
 
Last edited:
  • Like
Reactions: pel
Revolution-Entrepreneur
Joined
Apr 2, 2009
Messages
451
Reaction score
19
That's so awesome! Thank you so much for your help. I'll be working on this over the weekend. Will update the thread on further progress!
 
Put Community First
Member
Joined
Oct 2, 2014
Messages
1,112
Reaction score
832
Definitely let us know how you go. Will be good if you do require more assistance the source will be good so we can correct you where it needs fixing (if there ARE any errors, hehe).
 
Back
Top