Link to home
Start Free TrialLog in
Avatar of drupal_100
drupal_100Flag for United States of America

asked on

REST API response hedaer

Develop REST API using PHP in server side.
How to return something in the HTTP response header?
For example, A=100.
How to make this information into HTTP response header, so the client application program
can get this A value in the Response Headers.
Avatar of kaufmed
kaufmed
Flag of United States of America image

Perhaps the header function?
if you do it right, the client does not need to consider the header at all.  The advantage of not having to deal with the header is that you do not need to understand all of this stuff.
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

Here is an example of a RESTful API implementation.  Please read it over and post back with any questions.

<?php // RAY_REST_get_last_name.php
error_reporting(E_ALL);


// DEMONSTRATE HOW A RESTFUL WEB SERVICE WORKS
// INPUT FIRST NAME, OUTPUT FAMILY NAME
// CALLING EXAMPLE:
// file_get_contents('http://laprbass.com/RAY_REST_get_last_name.php?key=ABC&resp=XML&name=Ray');


// OUR DATA MODEL CONTAINS ALL THE ANSWERS - THIS COULD BE A DATA BASE - AS SIMPLE OR COMPLEX AS NEEDED
$dataModel
= array
( 'Brian'   => 'Portlock'
, 'Ray'     => 'Paseur'
, 'COBOL'   => 'Dinosaur'
, 'Dave'    => 'Baldwin'
)
;


// RESPONSE CAN BE PLAIN TEXT OR XML FORMAT
$alpha = NULL;
$omega = NULL;

// NORMALIZE AND TEST THE "resp=" ARGUMENT
if ( (isset($_GET["resp"])) && (strtoupper(trim($_GET["resp"])) == 'XML') )
{
    // PREPARE THE XML WRAPPER
    $alpha = '<?xml version="1.0" encoding="utf-8" ?>' . PHP_EOL . '<response>' . PHP_EOL;
    $omega = PHP_EOL . '</response>';
}


// TEST THE 'API KEY' - THIS COULD BE A DATA BASE VALIDATION LOOKUP - AS SIMPLE OR COMPLEX AS NEEDED
$key = (!empty($_GET["key"])) ? $_GET["key"] : FALSE;
if ($key !== 'ABC')
{
    echo $alpha . 'BOGUS API KEY' . $omega;
    die();
}


// LOOKUP THE FAMILY NAME
$name = (!empty($_GET["name"])) ? $_GET["name"] : 'UNKNOWN';

// IF THE NAME FROM THE URL IS FOUND IN THE DATA MODEL
if (array_key_exists($name, $dataModel))
{
    // RETURNS THE APPROPRIATE FAMILY NAME FROM THE DATA MODEL
    die( $alpha . $dataModel[$name] . $omega );
}


// RETURNS THE UNKNOWN NAME INDICATOR
else
{
    die( $alpha . "UNKNOWN: $name" . $omega );
}

Open in new window

I'd like to see what client program is going to read the response headers.  While it is true that web browsers and emulators do that, I don't see that they pass any of it along to the client side in the HTML or javascript.
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@kaufmed, you may think it's an ignorant question.  I do know how to put things in the response headers using PHP.  But I'm wondering what the asker thinks is going to happen and how they plan on accessing the header info.  The easiest place to put the info is in a cookie.  But that requires that their app can read cookies which is not a big deal either.

Any answer will make more sense if we know how they are going to access the info in the header.
Avatar of drupal_100

ASKER

Use header function to set a=100.
header('a: 100');

Use browser to call API, and see the response headers in browser developer tool (chrome):
HTTP/1.1 200 OK
Server: Apache
X-Powered-By: PHP/5.3.21
Last-Modified: Tue, 04 Jun 2013 15:32:28 +0000
Cache-Control: no-cache, must-revalidate, post-check=0, pre-check=0
ETag: "1370359948"
a: 100
Content-Type: text/html; charset=utf-8
Content-Length: 28852
Accept-Ranges: bytes
......

For a PHP client program, how to get the value of 'a' in response header?
None of the PHP functions returns the response header so you can see it.  I checked and not even curl does that.  But if you just use the method Ray showed above, all you need is something like 'file_get_contents' to get a simple value.

http://us2.php.net/manual/en/function.file-get-contents.php
What's the deep background motivation here?  Why is there any need for a header at all?