Solved

REST API response hedaer

Posted on 2013-06-04
9
380 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 75

Expert Comment

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

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 83

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 75

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
 
LVL 83

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 83

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 110

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 110

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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…

726 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