shabby
shabby

Reputation: 3222

Doing an HTTP Post from PHP to an HTTP Handler

So the title is self descriptive. Here is my php code

function do_post_request($url, $data, $optional_headers = null)
{
    $url = 'http://localhost:1181/WebSite1/PostHandler.ashx';
    $data = array( 'fprm' => 1, 'sprm'=> 2, 'tprm'=>3
                  );

  $params = array('http' => array(
              'method' => 'POST',
              'content' => $data
            ));

 //$params = array('method'=>'POST', 'content'=>$data);
 /* if ($optional_headers !== null) {
    $params['http']['header'] = $optional_headers;
  }*/
  $ctx = stream_context_create($params);
  //stream_context_set_option()

  //debug($params);
  //die();
  $fp = @fopen($url, 'rb', false, $ctx);
  if (!$fp) {
    throw new Exception("Problem with $url, $php_errormsg");
  }
  $response = @stream_get_contents($fp);
  if ($response === false) {
    throw new Exception("Problem reading data from $url, $php_errormsg");
  }
  return $response;
}

and here is my handler code:

 public void ProcessRequest(HttpContext context)
{
    context.Response.ContentType = "text/plain";
    string responseStr = "Hello World, this reply is from .net";
    string fprm = context.Request["fprm"];
    string sprm = context.Request["sprm"];
    string tprm = context.Request["tprm"];
    context.Response.Write(responseStr + " " + fprm + " " + sprm + " " + tprm);

}

and this is the reply i am getting:

'Hello World, this reply is from .net   '

i.e. without the parameters value, I read a similar post and the idea I got was that maybe you need to set a different context type to pass content parameters. But looking throught the php documentation, I dont find any option, http://php.net/manual/en/context.http.php, for setting the context type

Any help would be great, thanks

Upvotes: 0

Views: 2135

Answers (1)

Delta
Delta

Reputation: 4328

You're just forgetting to set the content-type header on your stream context. Set it to application/x-www-form-urlencoded

And you can't pass an array as the content directly to the stream context, it has to be the urlencoded form string, so use http_build_query for that

 $params = array('http' => array(
              'method' => 'POST',
              'header'=>'Content-Type: application/x-www-form-urlencoded',
              'content' => http_build_query($data)
            ));

The reason you didn't find how to change the content type on php's stream context documentation is because they don't provide a wrapper for that, but they do provide a way for you to add any HTTP header you want.

This content-type is necessary because otherwise the requested server-side application would just end up with a string that it doesn't know how to handle, since teorically you could send any kind of data over the content of a http request. application/x-www-form-urlencoded is telling the server that the string sent as the content is just a regular html form serialized and urlencoded. http_build_query takes a relational array and serializes it like a html form.

Upvotes: 1

Related Questions