phpcurl / curlwrapper
curl, curl_multi, curl_share 函数最简单的 OOP 包装器。使用 CURL 作为依赖,而不是硬编码!
3.0.0
2018-11-02 00:34 UTC
Requires
- php: >=7.1.0
- ext-curl: *
Requires (Dev)
- phpunit/phpunit: ^7.0
- squizlabs/php_codesniffer: ^2.0
README
这是标准 php curl 函数的最简单的 OOP 风格包装器,无外部依赖。主要目的是使使用 curl 调用的代码可测试。我们通过注入 Curl 对象作为依赖而不是直接调用 curl 函数来实现。
硬编码的依赖项。不可测试。
class MyApiClient { ... function call($url) { $ch = curl_init($url); curl_set_opt($ch, /*...*/) return curl_exec($ch); } }
可测试的代码。Curl 对象被注入,因此可以在 PHPUnit 中轻松模拟。
class MyApiClient { private $curl; function __construct(\PHPCurl\CurlWrapper\CurlInterface $curl) { $this->curl = $curl; } //... function call($url) { $this->curl->init($url); $this->curl->setOpt(/*...*/) return $this->curl->exec(); } }
安装
通过 composer: $ composer require "phpcurl/curlwrapper"