How to Return JSON and XML
How to Add JavaScript Validation to a Web Form
How to Validate Numbers in Web Form Fields

How to Return the Date from a Web Service

A simple way to get your feet wet with web services is to return a date in JSON format. Here's how you can do that:

  1. Open your text editor or programming IDE and create a new empty file.

  2. Place the following code within the file:

    <?php
    $header = "Content-Type: application/json";
    header($header);
     
    $date = date("M d, Y");
    print json_encode($date);
    ?>
  3. Save the file as date.php in your document root.

  4. View the page in your web browser at http://localhost/date.php.

    image0.jpg

The format for this web service just returns the date as a quoted string. It's more common to return an array of data with each element labeled. The labels make it easier to find and use individual elements. For example, consider the following listing:

<?php
$header = "Content-Type: application/json";
header($header);
$date = date("M d, Y");
$returnData = array("friendlyDate" => $date);
print json_encode($returnData);
 ?>

When viewed in a browser, the JSON-encoded data looks like this:

image1.jpg

As you can see, there's now more to the returned data. This means that you can return all sorts of data with the same web service and the consumers of the web service can choose which pieces they'll use. For example, the upcoming listing shows an enhanced date web service that returns the friendly date, the Unix time, the month, the day of the week, and the year in various formats.

This example uses the PHP header() function to send a Content-Type header to the browser. The Content-Type header tells the browser what type of information is to be expected as output. It's important for browsers so that they can parse the information properly.

<?php
$header = "Content-Type: application/json";
header($header);
 
$friendlyDate = date("M d, Y");
$unixTime = time();
$month = date("M");
$dayOfWeek = date("l");
$year = date("Y");
$returnData = array(
        "friendlyDate" => $friendlyDate,
        "unixTime" => $unixTime,
        "monthNum" => $month,
        "dayOfWeek" => $dayOfWeek,
        "yearNum" => $year
);
print json_encode($returnData);
 ?>

When viewed in a browser, the code returns data like that shown.

image2.jpg

With that arraylike output, it's easy to access individual elements. Say you have an application that needs to know the day of the week. You can call your web service and use the built-in json_decode() PHP function to get access to the dayOfWeek element. The following listing shows code to consume a web service.

<?php
$curlHandle = curl_init("http://localhost/date.php");
curl_setopt($curlHandle, CURLOPT_HEADER, 0);
curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curlHandle);
$decoded = json_decode($output,TRUE);
print $decoded['dayOfWeek'];
 ?>

When this page is viewed in a browser, the output is simply the day of the week. The code uses the cURL library, which connects into PHP through a set of powerful functions to interact with web pages and sites, including submitting forms. In this case, the code initializes the cURL object (through curl_init()), sets some options, and then loads the URL.

The output is saved into a variable called $output, which is then decoded using the json_decode() function. The Boolean TRUE that you see within the json_decode() function sets the output as an array, which is what you want. Finally, the dayOfWeek is retrieved from the decoded output and displayed to the screen.

This pattern is pretty typical of web service consumption. In fact, it's common to set up a shared function or a class for cURL so that you can call cURL web services without having to include this same code in all your files.

So what's the advantage of calling a date web service instead of just simply calling the date() function? That depends. On one hand, you could argue that setting up a common date function that returns all sorts of formats is easier than trying to remember the exact formatting for the date() function everywhere you need it.

On the other hand, you could say that calling a web service might slow down the overall response time. Both are true and valid.

The date() function is used in this chapter primarily because it provides an easy way to demonstrate returning data from a web service, without your humble book authors having to explain too much about what the date() function is doing.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
How to Refine Your Web Form Validation
How to Validate Required Fields in a Form-Process Page
Use FTP Functions to Ensure Safe File Uploads
How to Perform Basic JavaScript Validation
Using Layered Architectures in ASP.NET
Advertisement

Inside Dummies.com