Last active
April 13, 2017 10:59
-
-
Save ajaxray/11117042 to your computer and use it in GitHub Desktop.
Using version 2 of PECL HTTP extension (pecl_http)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'));` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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