Skip to content

Instantly share code, notes, and snippets.

@phpfour
Created April 7, 2012 16:55
Show Gist options
  • Save phpfour/2330376 to your computer and use it in GitHub Desktop.
Save phpfour/2330376 to your computer and use it in GitHub Desktop.
Generic use of pecl_http
<?php
// GET
$req = new HttpRequest('http://www.example.com');
$req->send();
echo "Response code: " . $req->getResponseCode(), PHP_EOL;
echo "Response body: ", PHP_EOL, $req->getResponseBody();
// POST / PUT
$req = new HttpRequest('http://www.example.com/post');
$req->addPostFields($someData);
$req->setMethod(HttpRequest::METH_POST); // OR HttpRequest::METH_PUT
$req->send();
echo "Response code: " . $req->getResponseCode(), PHP_EOL;
echo "Response body: ", PHP_EOL, $req->getResponseBody();
// DELETE
$req = new HttpRequest('http://www.example.com/post/1');
$req->setMethod(\HttpRequest::METH_DELETE);
$req->send();
echo "Response code: " . $req->getResponseCode(), PHP_EOL;
echo "Response body: ", PHP_EOL, $req->getResponseBody();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment