net_bazzline / php_component_curl
免费且简单易用的面向对象的curl请求和响应组件(YACC - 另一个curl组件)
1.0.0
2017-04-22 22:23 UTC
Requires
- php: >=5.3.3
- net_bazzline/php_component_toolbox: 1.9.*
Requires (Dev)
- mockery/mockery: 0.9.*
- phpunit/phpunit: ~4.8||~5.7
README
该项目旨在提供一个易于使用且免费(自由)的面向对象的php curl命令组件。
请查看 openhub.net。
当前变更日志可在 此处 找到。
此组件的开发并非为了替换 guzzle。
示例
通过使用Builder
//it is always good to ship the component with a factory to easy up usage use Net\Bazzline\Component\Curl\BuilderFactory; use Net\Bazzline\Component\Curl\Option\Timeout; $factory = new BuilderFactory(); $builder = $factory->create(); $url = 'http://www.foo.bar'; $timeout = new Timeout(10); //set the timeout to 10 seconds /** * you can also use: * //assuming that $dispatcher is an instance of DispatcherInterface * //assuming that $requestFactory is an instance of RequestFactory * $builder->overwriteDispatcher($dispatcher); * $builder->overwriteRequestFactory($requestFactory); */ $response = $builder->usePost() ->onTheUrl($url) ->withTheData($data) ->withTheParameters(array('descendingOrderBy' => 'id')) ->withTheOption($timeout) ->andFetchTheResponse(); /** * you can also use: * $builder->withTheHeaderLine($headLine) //add the headline you want * $builder->withResponseModifier($modifier) //add the response modifier you want */ echo 'content: ' . $response->content() . PHP_EOL; echo 'content type: ' . $response->contentType() . PHP_EOL; echo 'error:' . $response->error() . PHP_EOL; echo 'error code:' . $response->errorCode() . PHP_EOL; echo 'head lines: ' . var_export($response->headerLines(), true) . PHP_EOL; echo 'status code: ' . $response->statusCode() . PHP_EOL;
通过使用Request
//it is always good to ship the component with a factory to easy up usage $factory = new Net\Bazzline\Component\Curl\RequestFactory(); $request = $factory->create(); $url = 'http://www.foo.bar'; $response = $request->get($url); echo 'content: ' . $response->content() . PHP_EOL; echo 'content type: ' . $response->contentType() . PHP_EOL; echo 'error:' . $response->error() . PHP_EOL; echo 'error code:' . $response->errorCode() . PHP_EOL; echo 'head lines: ' . var_export($response->headerLines(), true) . PHP_EOL; echo 'status code: ' . $response->statusCode() . PHP_EOL;
可执行示例
更多示例
带基本身份验证的POST请求
//begin of runtime environments
$factory = new BuilderFactory();
$builder = $factory->create();
$data = array(
'there' => 'is',
'no' => 'foo',
'without' => 'a bar'
);
$password = '<super secret password>';
$username = 'foo@bar.ru';
$url = 'https://foo.bar.ru/api/my/rest/endpoint/v1';
//end of runtime environments
//begin building the request
$builder->asJson();
$builder->onTheUrl($url);
$builder->withTheData($data);
$builder->withTheOption(new SetBasicAuthentication());
$builder->withTheOption(new SetUsernameAndPassword($username, $password));
$builder->usePost();
//end building the request
$request = $builder->andFetchTheResponse();
echo PHP_EOL . 'dumping the request' . PHP_EOL;
var_dump($request);
术语
- 分发器
- 执行curl请求
- 如果您想使用纯curl,请使用此类
- 请求
- 响应
- 面向对象的响应方法
- 响应行为
- 与响应交互的接口
- 修改响应(通过创建一个新的响应)
- 如果响应不符合您的需求,则通过抛出异常来更改流程(例如)
- 与响应交互的接口
- Builder
- 提供流畅的接口,以便轻松使用curl
- 它负责一切
不可用的Curl选项
通常,php版本限制了可用的curl选项。此外,一些选项由于在Response或Dispatcher中硬编码使用而没有实现(您可以设置它,但它们将被覆盖)。
这些选项包括
- 在请求中使用
- CURLOPT_CUSTOMREQUEST
- CURLOPT_HTTPHEADER
- CURLOPT_POSTFIELDS
- 在分发器中使用
- CURLINFO_HEADER_OUT
- CURLOPT_HEADERFUNCTION
- CURLOPT_RETURNTRANSFER
如果您想修改这些,您必须扩展现有的请求或分发器对象。
安装
手动安装
mkdir -p vendor/net_bazzline/php_component_curl
cd vendor/net_bazzline/php_component_curl
git clone https://github.com/bazzline/php_component_curl .
使用Packagist
composer require net_bazzline/php_component_curl:dev-master
链接
其他可用组件
- https://github.com/php-mod/curl
- https://github.com/anlutro/php-curl
- https://github.com/hamstar/curl
- https://github.com/jyggen/curl
- https://github.com/ixudra/Curl
- https://github.com/brodkinca/BCA-PHP-CURL
- https://github.com/miliqi/laravel-curl
- https://github.com/andrefigueira/Lib-Curl
结语
如果您喜欢它,请给它加星。如果您需要它,请添加问题。如果您喜欢它,请拉取补丁。如果您使用它,请写一篇博客。如果您爱它,请捐赠。