HTTP GET

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
ukconceptsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
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
Olaf DoschkeSoftware DeveloperCommented:
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
ukconceptsAuthor Commented:
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
Olaf DoschkeSoftware DeveloperCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Content Management

From novice to tech pro — start learning today.