Grasshopper is a yet another cURL PHP library wchich makes you easy to send HTTP request. This library can process multiple requests at once.
- supports process multiple requests in one call
- easy to use: simple interface
- variety of error handling: both supported procedural or callback
use Grasshopper\Grasshopper;
use \Grasshopper\event\SuccessEvent;
use \Grasshopper\event\ErrorEvent;
$hopper = new Grasshopper();
$url = 'http://example.com';
$hopper->addRequest($url);
$result = $hopper->waitForAll();
$res = $result[$url];
if ( $res instanceof SuccessEvent ){
// success
$status = $res->getResponse()->getStatusCode();
$body = $res->getResponse()->getBody();
echo "success: status=$status" . PHP_EOL;
echo $body . PHP_EOL;
}
elseif ( $res instanceof ErrorEvent ){
// error
echo "error: " . $res->getError()->getMessage() . PHP_EOL;
}
- create grashopper object.
- add HttpGet/HttpPostRequest to grasshopper object.
- execute Grasshopper#waitforAll() method.
- get response from returned array.the key is requested URL.
- check response object whether SuccessEvent or ErrorEvent.SuccessEvent means request was succeeded, ErrorEvent means failure.
- you can get response object from SuccessEvent. it provides status code and response body.
- you can get error information from ErrorEvent. it provides error code and message.
PHP 5.5 or later
The recommended way to install Grasshopper is through Composer.
composer require stk2k/grasshopper
After installing, you need to require Composer's autoloader:
require 'vendor/autoload.php';
This software is no warranty.
We are not responsible for any results caused by the use of this software.
Please use the responsibility of the your self.