Solved

REST API response hedaer

Posted on 2013-06-04
9
360 Views
Last Modified: 2013-10-28
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.
0
Comment
Question by:drupal_100
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 39219260
Perhaps the header function?
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39219296
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

0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39219501
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.
0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39219523
@DaveBaldwin
I'd like to see what client program is going to read the response headers.
That's kind of an ignorant statement. An application (or service) can define what it wants to pass in HTTP headers--the same way it can define what it passes in the response body. As long as the client understands what the header means, what is the problem? Not every application accessing a RESTful service is a web browser  ; )

The bigger concern, IMO, is whether or not an intermediate node along the path from host to client strips out or rejects the custom header.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39219603
@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.
0
 

Author Comment

by:drupal_100
ID: 39219898
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?
0
 
LVL 82

Expert Comment

by:Dave Baldwin
ID: 39220403
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
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39220488
What's the deep background motivation here?  Why is there any need for a header at all?
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39314973
You may want to read this article before you spend too much time or money on the project.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_12239-Introduction-to-Application-Programming-Interfaces.html

~Ray
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now