Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
For 2023 - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Using web services to access you data - JSON and RESTful services

Rather than build database access directly into your application, you can write much more portable and maintainable code using a service / web service in which you embed the database access - or whatever else you're using for your backend data.

Web services are typically accessed through HTTP or HTTPS requests, very often using Representational State Transfer (RESTful) principles, and are commonly programmed in Java. The data is sometimes transferred in XML or even in HTML, but more commonly (and becoming more popular) in JSON (JavaScript Object Notation). I'm very much aware that I'm dropping in lots of buzzwords here and perhaps drowning my readers... so let's see an example that I've been writing today for new course material for a major client.

Here's a client (customer) program that uses our service:

  // Go get data (report of server accesses in the last minute)
  $stuff = file_get_contents("http://www.wellho.net/services/_.json");
  // Turn that data back into the structure that we've been served
  $info = json_decode($stuff);
  $reportat = date("r",$info->timestamp);
  $log = "";
  foreach ($info->records as $record) {
    $ago = $info->timestamp - $record->timedat;
    $log .= "$ago seconds ago loaded $record->what<br />\n";
  $nr = count($info->records);
  <head><title>Presentation Layer</title></head>
  <body><h1>What's been read in the last minute?</h1>
  Report timed at <?= $reportat ?> with <?= $nr ?> requests made<br /><br />
  <?= $log ?>

You can see it's REALLY easy to pick up data from a service. The source code is [here] and you can run it now via [here]. You'll get different results almost every time as this is a really fast-changing service. Here are results I got in testing:

The service layer code can be quite short too - my example is [here]. It works in stages:
a) Interpret the request (make sure a known format is asked for!)
b) Read the data in from the database into a memory structure of some sort
c) Format to the requested standard
d) Output the results

As I'll be using my server in a lot of different client demonstrations, I've allowed it to return a number of different formats:
• .html - formatted as a table [try it]
• .txt - formatted as a variable dump [try it]
• .json - formatted in JavaScript Object notation [try it] - looks a bit horrid!
• .csv - formatted as comma separated values [try it]
• .tsv - formatted as tab separated values [try it]
• .xml - formatted in Extensible Markup Language [try it] - your browser will make this look a bit ugly.

This demonstration service provider happens to be in PHP (it lets me explain the concept without having to use two different languages), but Java would be a more common choice.
(written 2013-03-29, updated 2013-04-06)

Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
H313 - PHP - Page Application and Service Layer
  [4053] Frameworks - learning through exploring and understanding data sources - (2013-03-27)
  [4059] Curl and curling from PHP - (2013-04-04)

H307 - PHP - Web2 and caching
  [1633] Changing a screen saver from a web page (PHP, Perl, OSX) - (2008-05-06)
  [1647] Exchange Rates - PHP with your prices in your users currency - (2008-05-19)
  [1733] memcached - overview, installation, example of use in PHP - (2008-08-02)
  [1812] Starting Ajax - easy example of browser calling up server data - (2008-09-27)
  [1813] Ajax - going Asyncronous and what it means - (2008-09-28)
  [1814] Javascript/HTML example, dynamic server monitor - (2008-09-28)
  [1926] Flash (client) to PHP (server) - example - (2008-12-06)
  [1995] Automated server heartbeat and health check - (2009-01-16)
  [2196] New Example - cacheing results in PHP for faster loading - (2009-05-24)
  [2321] Uploading and Downloading files - changing names (Perl and PHP) - (2009-08-04)
  [2545] Scraping content for your own page via PHP - (2009-12-21)
  [3029] PHP data sources - other web servers, large data flows, and the client (browser) - (2010-11-04)
  [3094] Setting your user_agent in PHP - telling back servers who you are - (2010-12-18)
  [3186] How to add a customised twitter feed to your site - (2011-02-27)
  [3458] On this day ... one PHP script with three uses - (2011-09-26)
  [3955] Building up from a small PHP setup to an enterprise one - (2012-12-16)
  [3999] Handling failures / absences of your backend server nicely - (2013-02-08)
  [4075] Further recent PHP examples - (2013-04-28)
  [4106] Web server efficiency - saving repetition through caches - (2013-05-30)
  [4136] How do I post automatically from a PHP script to my Twitter account? - (2013-07-10)
  [4627] Caching results in an object for efficiency - avoiding re-calculation - (2016-01-20)

Back to
On Salford Docks - mind over matter?
Previous and next
Horse's mouth home
Forward to
An overpractical test of our backup strategy!
Some other Articles
The highs and lows of customer service - Cheltenham
stdClass in PHP - using an object rather than an associative array
An overpractical test of our backup strategy!
Using web services to access you data - JSON and RESTful services
On Salford Docks - mind over matter?
The PHP course this week is in... Salford
On reading a new hotel review
A couple of new fast-start PHP examples
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page

This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2023: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/4055_.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb