Skip to content

Instantly share code, notes, and snippets.

Last active April 13, 2017 10:59
Show Gist options
  • Save ajaxray/11117042 to your computer and use it in GitHub Desktop.
Save ajaxray/11117042 to your computer and use it in GitHub Desktop.
Using version 2 of PECL HTTP extension (pecl_http)
// Doc Home -
$url = '';
// ## Simple single Get
$req = new \http\Client\Request('GET', $url);
$client = (new \http\Client())
$resp = $client->getResponse();
$code = $resp->getResponseCode();
$content = $resp->getBody();
// ## Async handling
(new http\Client)->enqueue(new http\Client\Request("GET", $url),
function(http\Client\Response $res) {
printf("%s returned %d\n", $res->getTransferInfo("effective_url"), $res->getResponseCode());
return true; // dequeue
// ## Adding QueryString
// @see :
$q = new \http\QueryString($params);
// ## Setting Headers
$headers = array(
'X-Api-Key' => 'api-key',
'X-Api-Secret' => 'api-secret',
$req = new \http\Client\Request('GET', $url, $headers);
// ## Setting Options
// Available options -
'connecttimeout' => 30,
'timeout' => 120,
// ## Send and handle parallal requests
$client = new http\Client;
$client->enqueue(new http\Client\Request("GET", ""));
$client->enqueue(new http\Client\Request("GET", ""));
$client->enqueue(new http\Client\Request("GET", ""));
while ($res = $client->getResponse()) {
printf("%s returned %d\n", $res->getTransferInfo("effective_url"),
// ## Info in Response object
// Many others
// Check with `print_r(get_class_methods('http\Client\Response'));`
Copy link

PHP Fatal error: Uncaught exception 'http\Exception\UnexpectedValueException' with message 'Failed to locate "(null)" client request handler'

I always get that when trying to use any pecl_http 2 code... Same if I explicitly pass curl as handler.
Can't seem to find anything on that anywhere, really annoying as this has been made the default http package for PHP on Ubuntu (that I know off).

Copy link

m6w6 commented Aug 5, 2014

Install libcurlX-Y-dev where X is 3 or 4 and Y is openssl or gnutls.
See also

Copy link

m6w6 commented Aug 5, 2014

Y might be any SSL library, like e.g. NSS, too.

Copy link

I'm trying to disable SSL peer verification with
$r = new http\Client\Request("POST", XML_ENDPOINT, NULL, $body);
'verifyhost' => false,
'verifypeer' => false,
but I'm not having any success, can someone suggest how to proceed?

Copy link

2 years later... the method return client so you have to write "$r = $r->setSsl......."
Php is a very very good language... ironic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment