Skip to content

Instantly share code, notes, and snippets.

@ajaxray
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)
<?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'));`
@m6w6
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
Copy link

m6w6 commented Aug 5, 2014

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

@fpischedda
Copy link

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
Copy link

@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