phpcurl/curlwrapper

curl, curl_multi, curl_share 函数最简单的 OOP 包装器。使用 CURL 作为依赖,而不是硬编码!

3.0.0 2018-11-02 00:34 UTC

This package is auto-updated.

Last update: 2024-08-29 03:26:10 UTC


README

Total Downloads Latest Stable Version Travis Build SensioLabs Insight

这是标准 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"

基本用法示例。经典与 OOP 风格

Curl

CurlMulti

CurlShare