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
Solved

HTTP GET

Posted on 2012-03-25
5
409 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 109

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 109

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
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.
This Micro Tutorial will demonstrate how nuggets on the Web are formatted by using Chrome Developer Tools. These tools would not only view the site's CSS but it can also modify it and save the CSS to use on your own site.

828 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