NetDriver is a PHP library which provides sending HTTP requests.
- Pluggable. You can easily replace other HTTP access library if you want.
- Simple interfaces.
- Callback events. You can customize your request before it is exectuted. Also you can get detail logs in sending request.
- PSR-3 Logger acceptable.
- cURL/PHP(file_get_contents) net drivers are bundled.
- Create net driver object(CurlNetDriver or PhpNetDriver are available).
- Create handle from net driver object.
- Create request object(HttpGetRequst/HttpPostRequest/JsonPostRequest are available).
- Call sendRequest method of net driver object and receive response object.
use Stk2k\NetDriver\Drivers\Curl\CurlNetDriver;
$driver = new CurlNetDriver();
$request = new HttpGetRequest($driver, 'http://sazysoft.com/test/');
$handle = $driver->newHandle();
try{
$response = $driver->sendRequest($handle, $request);
echo $response->getBody();
}
catch(NetDriverException $e)
{
// error handling here
}
use Stk2k\NetDriver\Drivers\Php\PhpNetDriver;
$driver = new PhpNetDriver();
$request = new HttpGetRequest($driver, 'http://sazysoft.com/test/');
$handle = $driver->newHandle();
try{
$response = $driver->sendRequest($handle, $request);
echo $response->getBody();
}
catch(NetDriverExceptionInterface $e)
{
// error handling here
}
$driver = new CurlNetDriver();
$request = new HttpPostRequest($driver, 'http://sazysoft.com/test/', ['foo' => 'bar', 'baz' => 'qux']);
$handle = $driver->newHandle();
try{
$response = $driver->sendRequest($handle, $request);
echo $response->getBody();
}
catch(NetDriverExceptionInterface $e)
{
// error handling here
}
$driver = new CurlNetDriver();
$request = new HttpGetRequest($driver, 'http://sazysoft.com/test/');
$handle = $driver->newHandle();
try{
$driver->listen('request', function(HttpRequest $request){
$request->addHttpHeader('Content-Type', 'text/json'); // set content type to text/json
return $request; // replace old request to new one
});
$response = $driver->sendRequest($handle, $request);
echo $response->getBody();
}
catch(NetDriverExceptionInterface $e)
{
// error handling here
}
$driver = new CurlNetDriver();
$request = new HttpGetRequest($driver, 'http://sazysoft.com/test/');
$handle = $driver->newHandle();
try{
$driver->setVerbose(true); // curl netdriver can write debug info to output
$driver->listen('verbose', function($strerr, $header, $output){
echo '----[ strerr ]----' . PHP_EOL;
echo $strerr . PHP_EOL;
echo '----[ header ]----' . PHP_EOL;
echo $header . PHP_EOL;
echo '----[ output ]----' . PHP_EOL;
echo $output . PHP_EOL;
});
$response = $driver->sendRequest($handle, $request);
echo '----[ response ]----' . PHP_EOL;
echo $response->getBody();
}
catch(NetDriverExceptionInterface $e)
{
// error handling here
}
PHP 7.1 or later
The recommended way to install NetDriver is through Composer.
composer require stk2k/net-driver
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.