Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

REST API response hedaer

Posted on 2013-06-04
9
Medium Priority
?
414 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 75

Expert Comment

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

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 84

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 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
 
LVL 84

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 84

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 111

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 111

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

916 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