Discussion Forums  >  Self Hosted Control Panels

Replies: 4    Views: 152

SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
06/25/13 08:25 PM (12 years ago)

Announcing the Smug API Saver for self hosted control panels...

Basically it boils down to this: I'm a cheapskate ;) That said, if you 'subscribe' to some services (in my instance, I'm talking about weather) you are allowed a certain number of API calls per hour, per day, per something. If you want more you have to subscribe to some 'premium' membership. Let's not, ok? If our wildest dreams come true, millions upon millions will be using our app... therefore our API calls will soon be depleted, leaving a less than optimal user experience for all facets of our app. Enter the Smug 'API Saver' for self hosted control panels. So simple, yet so effective. You would substitute your API url (that would connect in your app) with your 'Smug API Saver PHP URL' on your server... "http://www.yourserver.com/yourdirectory/smugapi.php" (or whatever you name it) The server script will check for a recent (1 hour or less) copy of your API return file. If present, it will feed it to the client transparently, alleviating an API call. If not present, it will download the file, save it to your server, and then feed it to the client transparently. This ensures that the clients get the latest (1 hour or less old) data without running up your API count. After one hour, the 'filename' changes, and if present, is fed, if not will be downloaded, saved, and fed. You won't believe how simple it is. <?php // if this script does not work for you // check your php.ini file for 'allow fopen'. // "www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen" $swFileName = "weather" . date('YmdH') . ".txt"; $swOldFileName = "weather" . (date('YmdH') - 1) . ".txt"; $swURLString = "http://api.longwindeddomainname.com/this/that/weather.ashx?q=13.500000,144.800000&format=json&num_of_days=5&key=ABCDEFGHIJKLMNOPQRSTUVWXYZ"; if (file_exists($swFileName)) { ob_clean(); flush(); readfile($swFileName); } else { $success = file_put_contents($swFileName, file_get_contents($swURLString)); ob_clean(); flush(); readfile($swFileName); } if (file_exists($swOldFileName)) { unlink($swOldFileName); } ?> Cheers! -- Smug Edit: In retrospect, the method of deleting old files is not perfect; if a call is not made within an hour, then the 'hour minus one' scheme will not work. I also suspect issues around midnight/1am... so you'll end up with a few files... but rather small ones, and nothing that will damage anyone/anything. I'll look into this a little more, but it's already down on my priority list... Edit of the Edit: This kind of solution is best for 'redundant' API calls... Weather for a particular city, Currency Exchange rates, and APIs that have the same 'call URL' time after time. With my '50 request per hour' API, I could conceivably have 25+ apps distributed among thousands of users using the same plugin, and still have plenty API calls for further testing/development. API Calls that basically change a component of the URL with each API call (different cities, geo coordinates) would not be solved with this solution. caveat emptor :)
 
nadthevlad
Code is Art
Profile
Posts: 1025
Reg: Jun 07, 2012
Denver
21,850
like
06/25/13 09:25 PM (12 years ago)
So basically your talking about polling the API server with your server. Saving the result. Then using your own server to broadcast the results to your app users. Thus saving you money on the subscription API service.
 
SmugWimp
Smugger than thou...
Profile
Posts: 6316
Reg: Nov 07, 2012
Tamuning, GU
81,410
like
06/25/13 09:30 PM (12 years ago)
Exactly. A request to the page would initiate the sequence. 1) Look for the file. Is it there? 2) If there, feed it to the requesting device. 3) If not, download it and feed it to the requesting device.. 4) Delete last hour's file. That I could do that in under 20 lines of code really cracks me up. Cheers! -- Smug
 
chris1
Code is Art
Profile
Posts: 3862
Reg: Aug 10, 2012
Austin, TX
50,120
like
06/25/13 09:43 PM (12 years ago)
Good work Smug.
 
nadthevlad
Code is Art
Profile
Posts: 1025
Reg: Jun 07, 2012
Denver
21,850
like
06/26/13 05:23 PM (12 years ago)
I like it. Good job. <-- Needs to get hot on learning PHP.
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.