Online Test Banks
Score higher
See Online Test Banks
eLearning
Learning anything is easy
Browse Online Courses
Mobile Apps
Learning on the go
Explore Mobile Apps
Dummies Store
Shop for books and more
Start Shopping

How to Return JSON and XML

You need to choose whether you will return JSON data or XML data at programming time, and that can never change unless you reprogram the output. The world would be a better place if you could return both XML and JSON, depending on what the calling program wants.

Accomplishing this feat is a matter of accepting input for the web service and then providing appropriate output. The following listing provides the code for this web service.

<?php
 
if (isset($_GET['format'])) {
    $format = $_GET['format'];
    if (!preg_match('/json|xml/',$format)) {
        print "Please choose a format: json or xml";
        exit;
    }
} else {
    print "Please choose a format: json or xml";
    exit;
}
 
$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
);
 
if ($format == "xml") {
    $xml = new DOMDocument();
    $dateInfoElement = $xml->createElement("dateInformation");
    foreach ($returnData as $key => $value) {
        $xmlNode = $xml->createElement($key,$value);
        $dateInfoElement->appendChild($xmlNode);
    }
    $xml->appendChild($dateInfoElement);
    $output = $xml->saveXML();
    $header = "Content-Type:text/xml";
} else if ($format == "json") {
    $output = json_encode($returnData);
    $header = "Content-Type:application/json";
}
header($header);
print $output;
 ?>

It may be helpful to break this code down. The first part of the code looks for the format to be sent back:

if (isset($_GET['format'])) {
    $format = $_GET['format'];
    if (!preg_match('/^(json|xml)$/',$format)) {
        print "Please choose a format: json or xml";
        exit;
    }
} else {
    print "Please choose a format: json or xml";
    exit;
}

If a GET parameter of format is available, it's set to the $format variable. This variable is tested using the preg_match() function. This function uses a regular expression to check that the format parameter is set to json or xml (lowercase). If it isn't, an error is displayed, as is the case if the format parameter is not set at all.

From there, the code performs the same functions that you've seen already, obtaining the date in various formats and placing them into an array. Finally, the code sets up a conditional based on the requested format. If it's XML, then the XML-related code is executed; if the requested format is JSON, then the JSON-related code is executed. Finally, the output is sent to the browser.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win $500. Easy.