Send HTTP_Request2 Request and Form Response
HTTP_Request2 is addition to first version of PEAR HTTP request package which fixes some important bugs. To send HTTP_Request2 request you need to set three parts of a request:
- 1. create new object of HTTP_Request2 class
- 2. set get or post parameters
- 3. retrieve body of the request
To build new HTTP_Request2 object we’d use it this way:
$request = new HTTP_Request2('http://somesite.com/request.php');
Of course, your script needs to return some response, whether in HTML or XML format (most often).
Next, you can set POST or GET method to send request by:
If you need some parameters, you can define it like:
$request->setMethod(HTTP_Request2::METHOD_POST) ->addPostParameter('parm', 'value1') ->addPostParameter(array( 'parmType' => 'val11', 'parm2' => 'val12' ));
As you can see, parameters can be set directly as key->value pairs or as an array. This is absolutely up to you.
Finally, result is picked with getBody method:
$response = $request->send()->getBody();
If you know what type of body is returned (html or xml) e.g. XML, then it’s not a bad practice to parse it with SimpleXML PHP’s class:
$myxml = simplexml_load_string($response);
Pretty cool, huh?
Share this story:
One response to “Send HTTP_Request2 Request and Form Response”
Leave a Reply