An-Najah National University

Razi Alsayyed Blog

This is a blog about my experience with programming, networking and systems

 
  • Bookmark and Share Email
     
  • Wednesday, January 21, 2009
  • Make a Post Request through PHP Script
  • Making a GET Request through PHP is very easy, you can treat the URL of the target page as a file, open it using fopen while include the parameters in the URL String:

     

    $result = file_get_contents("www.example.com?param1=value1&param2=value2");

     

    But some times GET does not work, specially when you need to communicate with other platforms "other servers" which already written and expect a POST request.

    Making a Post Request is Easy to, but it is a little tricky, because in POST requests you can not add the paramters to the end of the URL as in GET, instead you need to send them in the HTTP headers, and in order to do that, you need to open a socket using fsocketopen and send all the HTTP headers that the target server expect:

     

     

    function PostRequest($url, $referer, $_data) {

        // convert variables array to string:

        $data = array();    

        while(list($n,$v) = each($_data)){

            $data[] = urlencode($n)."=".urlencode($v);

        }    

        $data = implode('&', $data);

        

        // format --> test1=a&test2=b etc.

        // parse the given URL

        $url = parse_url($url);

        if ($url['scheme'] != 'http') { 

            die('Only HTTP request are supported !');

        }

        // extract host and path:

        $host = $url['host'];

        $path = $url['path'];

        // open a socket connection on port 80

        $fp = fsockopen($host, 80);

        // send the request headers:

        fputs($fp, "POST $path HTTP/1.1\r\n");

        fputs($fp, "Host: $host\r\n");

        fputs($fp, "Referer: $referer\r\n");

        fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");

        fputs($fp, "Content-length: ". strlen($data) ."\r\n");

        fputs($fp, "Connection: close\r\n\r\n");

        fputs($fp, $data);

        $result = ''; 

        while(!feof($fp)) {

            // receive the results of the request

            $result .= fgets($fp, 128);

        }

        // close the socket connection:

        fclose($fp);

        // split the result header from the content

        $result = explode("\r\n\r\n", $result, 2);

        $header = isset($result[0]) ? $result[0] : '';

        $content = isset($result[1]) ? $result[1] : '';

        // return as array:

        return array($header, $content);

    }

     

    using the above function is very easy, you just need to put all your parameters in an associative array and pass it to the function:

     

     

    $data = array(

        'test' => "foobar\ntest",

        'okay' => 'yes',

        'number' => 2

    );


     

    list($header, $content) = PostRequest(

        "http://www.example.com/post_to.php",    // the url to post to

        "http://www.example.com/post.php",         // the url of the post script "this file"

        $data

    );

     


    echo $content;

     

     

     
  • Bookmark and Share Email
     
  • idwaikat said...
  • Great job, it's very helpful in many many places
  • Monday, February 2, 2009
  • DK said...
  • Nice job
  • Monday, June 22, 2009
  • Toomas said...
  • You forgot to escape $data properly. Here's the fix (replace first while statement with this one): foreach($_data as $k => $v) { $data[] = urlencode($k).'='.urlencode($v); }
  • Tuesday, August 25, 2009
  • Razi said...
  • Thanks Toomas, I fixed it
  • Wednesday, August 26, 2009
  • Daniel Renfro said...
  • Thank you a million times. I was trying to do the same task using the PHP_CURL methods, but this is MUCH easier! (and pre-written!) It works like a charm...keep up the good work.
  • Saturday, January 2, 2010
  • Jan Verhulst said...
  • Tnx dude! very good solution!
  • Sunday, November 21, 2010
Leave a Comment

Attachments

PROFILE

Razi F. Alsayyed
Computer Engineer
I am a 26 years old computer enginner, graduated from An-Najah National University in 2005, and working in it since that.
 
Show Full Profile
 
 

GENERAL POSTS

 
Please do not email me if you do not know me
Please do not e-mail me if you do not know me