Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using version 2 of PECL HTTP extension (pecl_http)
<?php
// Doc Home - http://devel-m6w6.rhcloud.com/mdref/http
$url = 'http://php.net';
// ## Simple single Get
$req = new \http\Client\Request('GET', $url);
$client = (new \http\Client())
->enqueue($req)
->send();
$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
})->send();
// ## Adding QueryString
// @see : http://devel-m6w6.rhcloud.com/mdref/http/Client/Request/setQuery
$q = new \http\QueryString($params);
$req->setQuery($q);
// ## 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 - http://devel-m6w6.rhcloud.com/mdref/http/Client/Curl
$req->setOptions(array(
'connecttimeout' => 30,
'timeout' => 120,
));
// ## Send and handle parallal requests
$client = new http\Client;
$client->enqueue(new http\Client\Request("GET", "http://php.net"));
$client->enqueue(new http\Client\Request("GET", "http://pecl.php.net"));
$client->enqueue(new http\Client\Request("GET", "http://pear.php.net"));
$client->send();
while ($res = $client->getResponse()) {
printf("%s returned %d\n", $res->getTransferInfo("effective_url"),
$res->getResponseCode());
}
// ## Info in Response object
$res->getBody();
$res->getHeader();
$res->getHeaders();
$res->getRequestUrl();
$res->getResponseCode();
$res->isMultipart();
$res->splitMultipartBody();
$res->getInfo();
// Many others
// Check with `print_r(get_class_methods('http\Client\Response'));`
@MichelBartz

This comment has been minimized.

Copy link

MichelBartz commented Aug 2, 2014

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).

@m6w6

This comment has been minimized.

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 https://bugs.php.net/bug.php?id=67316

@m6w6

This comment has been minimized.

Copy link

m6w6 commented Aug 5, 2014

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

@fpischedda

This comment has been minimized.

Copy link

fpischedda commented Jun 3, 2015

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

@marcosanson

This comment has been minimized.

Copy link

marcosanson commented Apr 13, 2017

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

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