Solved

HTTP GET

Posted on 2012-03-25
5
406 Views
Last Modified: 2012-04-02
Dear Experts,

I am writing an api for MO messages with http.

Someone should please show me sample codes that can return my message in text/plain?

Thank you.

sample-mo-api.txt
0
Comment
Question by:ukconcepts
  • 2
  • 2
5 Comments
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 37762451
What you are describing is a RESTful API.  The whole WWW works this way, and your script will be easier than you may think.  Just send the response in plain text with the PHP echo statement.  Don't worry about any headers or any of the other stuff.  You can learn more than you ever wanted to know about REST here:
http://en.wikipedia.org/wiki/Representational_state_transfer

The code snippet is a teaching example of a RESTful API.  HTH, ~Ray
<?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'
, 'Richard' => 'Quadling'
, 'Dave'    => 'Baldwin'
)
;


// RESPONSE CAN BE PLAIN TEXT OR XML FORMAT
$alpha = NULL;
$omega = NULL;
if ( (isset($_GET["resp"])) && ($_GET["resp"] == 'XML') )
{
    // PREPARE THE XML WRAPPER
    $alpha = '<response>';
    $omega = '</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();
}



// LOOK UP 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
    echo $alpha . $dataModel[$name] . $omega;
    die();
}

// RETURNS THE UNKNOWN NAME INDICATOR
else
{
    echo $alpha . 'UNKNOWN' . $omega;
    die();
}

Open in new window

0
 
LVL 29

Expert Comment

by:Olaf Doschke
ID: 37762963
It's true what Ray says. On top of that, typically a Web server like Apache or IIS is configured to answer a request to a PHP script with a standard header for mime type text/html. This is okay for the normal case PHP is used to create HTML output.

In a PHP script you can override that, unless headers already have been sent. You can check that via headers_sent() and headers_list() will list headers already sent or at least prepared. In case headers have not yet been sent you can use header_remove() to remove them.

And then one of the first function calls made of your php script should be
header('Content-type: text/plain');

Open in new window


and afterwards echo is enough to send text.

If not doing that and staying with the web server default for PHP extension, a strict client might be picky about it, while actually text/html does not differ from text/plain in most cases, a typically UTF-8 encoded html output also contains text in the same single byte encoding as text/plain, the lower 128 chars are identical and include many Latin chars, digits and such.

Bye, Olaf.
0
 

Author Comment

by:ukconcepts
ID: 37763348
Dear experts,

Forgot to mention am a newbie.

HTTP response from Client’s application should look like this:

===========================================
HTTP/1.1 200 OK ??
Date: Mon, 07 Jan 2007 04:55:47 GMT ??
Server: Apache/1.3.19 (Unix) ??
Connection: close ??
Content-Type: text/plain ??
=========================================

Please provide me with php code that could be included in my code above to get this response.

Thank you.
0
 
LVL 29

Accepted Solution

by:
Olaf Doschke earned 250 total points
ID: 37763363
This HTTP header is generated automatically, you don't need to do anything for an HTTP GET to return a header.

The only thing you need to take care of is the content-type, the php extension typically is set to return a content-type of text/html, and the header() code I gave you will change that to the needed text/plain.

HTTP1.1 200 OK is sent by the webserver you run PHP on, no code needed for that
Date is sent by the webserver, no code needed
Server is sent by the server, no code needed
Connection: close is normal, also no code needed.

All you need additional to the text itself to send back that header is the Content-type header.

You can eg make a HTTP GET request to your php script and test the headers you get via using a tool like http://web-sniffer.net/

Start with this as your test.php:
<?php 
header('Content-type: text/plain');
echo 'test-output'; 
?>

Open in new window

that's it.

Now make a request to http://yoursite/test.php via http://web-sniffer.net/ and look at the header you get back.

Bye, Olaf.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 37797789
Good grief - what was so wrong with the answers that you marked this down a whole letter grade?  You got exactly what you asked for, including detailed responses with tested and working code examples.  What did you expect?  Please explain the marked-down grade, and going forward, please abide by the grading guidelines, thanks.
http://www.experts-exchange.com/help/viewHelpPage.jsp?helpPageID=26
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

#Citrix #Internet Explorer #Enterprise Mode #IE 11 #IE 8
This article offers some helpful and general tips for safe browsing and online shopping. It offers simple and manageable procedures that help to ensure the safety of one's personal information and the security of any devices.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
How to create a custom search shortcut to site-search Experts Exchange using Google in the Firefox browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch your Bookmark Menu: Press 'Ctrl +…

707 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

15 Experts available now in Live!

Get 1:1 Help Now