cURL POST Alternative
Failed with cURL? Or you just want to look for alternatives? This Tut will show you how to accomplish this with PEAR package (HTTP_Request2, yes again this, but it’s very cool, not everyone is using it, dunno why).
First, you need to import PEAR and some of it’s functions. Instead to search for it, I have it, so – download it. Long story short, here’s what you need to do, unpack zip to your site, and import it (depending where you have it – I have it in root):
require_once($_SERVER['DOCUMENT_ROOT']."/PEAR.php"); require_once($_SERVER['DOCUMENT_ROOT'].'/Net/URL2.php'); require_once($_SERVER['DOCUMENT_ROOT']."/HTTP/Request2.php"); ?>
Next, put the code:
$request = new HTTP_Request2('http://url/to/some/service'); $request->setMethod(HTTP_Request2::METHOD_POST) ->addPostParameter(array( 'some_parameter1' => $val1, 'some_parameter2'=>$val2 )); $response = $request->send()->getBody();
Constructor is url to a service or a site, next set method to POST, and request parameter. Easy! If response is e.g. XML, parse it, or if it’s HTML display it or whatever (scrape?) .. That’s it!
Share this story:
Leave a Reply