How to Create/Read HTTP POST request

This post is actually a follow-up to my last one called http post streaming where I tried to create a php class that provides a way to send a HTTP POST request. I recoded the class and added new features (e.g. a way to read the body of an HTTP request, a dynamic way of setting the headers fields…etc.).

The body of an HTTP request can be easily read from php by using php://input stream. This stream return the raw POST data.

The phpWebRequest class is still under developpment but I post it because it may help some of you. (Of course anytime I add a feature I will update this post)

TO DO:

  • Finish the DataReaderFactory so we can use either read from an url or file to create the body of the request.
  • Create an HTTPs support

 

phpWebRequest
 

<?php
/*
PhpWebRequest objects provide methods to send or read HTTP requests
Date: June-08
Author: Guillaume Nachury
Version: 0.1

*/

class PhpWebRequest{
//VERSION
var $version = "0.1";

//HTTP
var $boundary;
var $method;
var $header;
var $body;
var $protocol;
var $headerElements; 

//SERVER
var $address;
var $path;
var $port;

var $isOverSSL = false;

//FEED
var $xml;

/**
* InitServer()
* $a = server address
* $p = path to the ressource
* $prt = port (by default 80)
*/
function InitServer($a,$p,$prt=80){
$this->address = $a;
$this->path = $p;
$this->port = $prt;
}

/**
* InitRequest()
* $m = the HTTP method you wanna use
* $secured = in case we use HTTPS
* $bounds = a string to create a boundary (unused since v 0.1)
*/
function InitRequest($m, $secured = false, $bounds="PhpWebRequest"){
$this->method = $m;
$this->boundary = md5($bounds);
$this->isOverSSL = $secured;
if($this->isOverSSL === false){
$this->protocol = "HTTP/1.1";
}
else{
$this->protocol = "HTTPS";
}
}

/**
* DataReaderFactory()
* a factory that can read data from either a file, an URL or a String to be used as the content of the request
* $u = the path to the data ( for a file start with file://
* for an URL start with http://
* for string just enter chars)
*/
function DataReaderFactory($u){
//if we wanna read from a file
if(strpos($u,'file://' ) === true){

}
//or from a remote web page
elseif(strpos($u,'http://' ) === true){

}
//a string have been passed
else{
$this->xml=$u;
return strlen($this->xml);
}
}

/**
* CfgHeader()
* Create the HTTP header fields
* $attributesArray = an array of headers that MUST have keys
* keys = field name
* value = field calue
*/
function CfgHeader($attributesArray){
while(list($k,$v) = each($attributesArray)){
$this->headerElements .= $k.": ".$v."\n";
}
}

/**
* CraftRequest()
* Create a HTTP request
*/
function CraftRequest(){
$this->header = $this->method." ".$this->path." ".$this->protocol. "\n".$this->headerElements."\n";
$this->body = $this->xml."\n";
}

/**
* SendRequest()
* Send the request, return true if successfully sent
*/
function SendRequest(){
$this->CraftRequest();
$request = $this->header.$this->body;

$fs = fsockopen($this->address,$this->port);
if(!$fs){
return false;
}
else{
fputs($fs,$request);
fflush($fs);
fclose($fs);
return true;
}

}

/**
* readHTTPRequest()
* return the HTTP request BODY
*/
function readHTTPRequest(){
return @file_get_contents('php://input');
}

}
?>

Usage :

<?php
require_once('phpWebRequest.class.php');

$rq = new PhpWebRequest();
$rq->InitServer("127.0.0.1","/aPath/test.php");
$strSize = $rq->DataReaderFactory("the body of the request");
$rq->InitRequest("POST");

//header fields
$headerElements = array("Host" => "127.0.0.1",
"Connection" => "Close",
"Content-type" => "multipart/mixed",
"Content-length" => $strSize
);
//load the headers
$rq->CfgHeader($headerElements);

//send it
if($rq->SendRequest()){
echo "Request sent !";
}
else{
echo "ERROR";
}

?>

If you have any question feel free to send me an email.

Advertisements

7 Responses

  1. Hello,

    I have a question about the PhpWebRequest class usage : How can you read the response of the post request ? My problem is to send a HTTP POST XML request to a server and receive the acknowledgement of receipt (xml).

    Thanks for your response.

  2. Hi cyrielle,
    As you can see in the phpWebRequest Class, a fuction called readHTTPRequest allows you to retrieve the body of an incoming request. The only thing you have to make sure is (and it seems to be the case), the request you getting back is a post method.

    To display the raw POST data just do a simple:

    <?php
    require_once('phpWebRequest.class.php');
    
    //Create the PhpWebRequest Object
    $rq = new PhpWebRequest();
    
    //Call readHTTPRequest()
    echo "Response: ".$rq->readHTTPRequest();
    
    ?>
    

    Hope it helps.

  3. Hi Guillaume !

    Thanks for your response.
    I have try to use the readHTTPRequest() function but @file_get_contents(‘php://input’) return empty string (and the request I’m getting back is a post method). How does it works ?

  4. Hi Cyrielle,
    Actually

    @file_get_contents(’php://input’);
    

    return the POST data from the request that called the page.
    If you just call(#1) your php page that do a WebRequest and at the end of your code try to display the POST data, you won’t have anything, since you’ll display the POST data from your first call(#1). And this call should be a simple GET request.

    I don’t know the logic in your system but if I had to create the same thing I would go that way :
    1. Create a php page that can recieve a parameter like ‘?action=send’, and switch on this arg. If it exists then do a normal phpWebrequest to the remote server.
    If it doesn’t exist, it means that your the page is recieving a POST request from the remote server, and then store the POST data (in a db/ XML / or whatever…)
    2. Just Call your page http://www.myserver.org/mypage.php?action=send
    3. Wait for the remote server to POST back and read the response stored.

    As I said, I don’t know your logic so maybe it can’t be applied to your system, but I hope it shed a light on your problem.

  5. Hi Guillaume,

    I changed the method which send the request and now it’s OK ! I can see the content which send in the POST request.

    Thanks for your help !

  6. no problem 🙂

  7. I wants to have a form in which user posts a comment. And it shows on on the same page.
    It can be done through php. But i would not know anything about php.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: