« Blog Home

§ Send POST request from PHP

A way to send a POST request from PHP.

The PostToHost function does the work.  The code above it shows examples of how to call it.

 


<?php

$post_data='a=b&c=d';
PostToHost('localhost','/php_post/post_receive.php',$post_data);

function add_to_newsletter($name,$email) {

    $news_name=htmlspecialchars(stripslashes($name));
    $news_email=htmlspecialchars(stripslashes($email));

  $post_data='u_name='.$news_name.'&u_email='.$news_email.'&r[group_id][]=1';
  PostToHost('localhost', '/mailing/subscribe/enter.php', $post_data);
}

function PostToHost($host, $path, $data_to_send) {
  $eol = "rn";
  $fp = fsockopen($host,80);
  fputs($fp, "POST $path HTTP/1.1".$eol );
  fputs($fp, "Host: $host".$eol );
  fputs($fp, "Content-type: application/x-www-form-urlencoded".$eol );
  fputs($fp, "Content-length: ".strlen($data_to_send).$eol );
  fputs($fp, "Connection: close".$eol.$eol );
  fputs($fp, $data_to_send);
  fclose($fp);
}

?>

 

last edited on January 29th, 2009 at 12:30 PM

Comments

No Comments Here. Add yours below!

Add your comment

Name:
Email: (Will not be displayed - Privacy policy)
Website:
Comments:
  random image I can't read it!
Give me a different one!
Verify Post: Input the text from the image above to verify this post (helps prevent spam)
 

« Blog Home


His idea managed to be both horribly unformed and far too complex - in itself quite an accomplishment, he supposed.
Martinez, The Sundering, Walter Jon Williams