?
Solved

REST API response hedaer

Posted on 2013-06-04
9
Medium Priority
?
399 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?
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
WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

765 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