定制 sylouuu/php-curl 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sylouuu/php-curl

Composer 安装命令:

composer require sylouuu/php-curl

包简介

Lightweight PHP cURL wrapper

关键字:

README 文档

README

Build Status Version CodeClimate

Requirements

  • PHP >= 5.4
  • cURL library enabled

Install

Composer

{
    "require": {
        "sylouuu/php-curl": "0.8.*"
    }
}
require_once './vendor/autoload.php';

Usage

// Namespace shortcut
use sylouuu\Curl\Method as Curl;

// Template
$request = new Curl\<METHOD_NAME>( string $url [, array $options ] );

Methods are:

  • Get()
  • Head()
  • Options()
  • Post()
  • Put()
  • Patch()
  • Delete()

Constructor $options

[
    'data' => [             // Data to send, available for `Post`, `Put` and `Patch`
        'foo' => 'bar'
    ],
    'headers' => [          // Additional headers (optional)
        'Authorization: foobar'
    ],
    'ssl' => '/cacert.pem', // Use it for SSL (optional)
    'is_payload' => true,   // `true` for sending a payload (JSON-encoded data, optional)
    'autoclose' => true     // Is the request must be automatically closed (optional)
]

Public methods

// Send a request
$request->send();

// HTTP status code
$request->getStatus();

// HTTP header
$request->getHeader();

// HTTP body response
$request->getResponse();

// Used cURL options
$request->getCurlOptions();

// Set a cURL option
$request->setCurlOption(CURLOPT_SOMETHING, $value);

// Manually close the handle (necessary when `autoclose => false` is used)
$request->close();

Examples

Basic:

// Namespace shortcut
use sylouuu\Curl\Method as Curl;

// Standard GET request
$request = new Curl\Get('http://domain.com');

// Send this request
$request->send();

echo $request->getResponse(); // body response
echo $request->getStatus(); // HTTP status code

Send a payload:

// Namespace shortcut
use sylouuu\Curl\Method as Curl;

// JSON-encoded POST request
$request = new Curl\Post($this->endpoint, [
    'data' => [
        'name' => 'foo',
        'email' => 'foo@domain.com'
    ],
    // With 'is_payload' => true
    // You don't have to json_encode() your array of data
    // Moreover, the appropriate headers will be set for you
    'is_payload' => true
]);

// Send this request
$request->send();

echo $request->getResponse(); // body response
echo $request->getStatus(); // HTTP status code

Manual closing:

// Namespace shortcut
use sylouuu\Curl\Method as Curl;

// Set `autoclose` option to `false`
$request = new Curl\Get('http://domain.com', [
    'autoclose' => false
]);

// Send this request
$request->send();

// Now you can retrieve a cURL info as the handle is still open
$request->getCurlInfo(CURLINFO_SOMETHING);

echo $request->getResponse();

// Manually close the handle
$request->close();

Tests

On project directory:

  • composer install to retrieve phpunit
  • phpunit to run tests

Changelog

2015-08-25 - 0.8.1

  • fixed invalid content-length in case of no data provided

2015-07-03 - 0.8.0

  • added is_payload option to perform a request with JSON-encoded data
  • fixed ssl option

2014-10-23 - 0.7.1

  • fixed Post() which didn't send data in some cases

2014-08-01 - 0.7.0 (BC break)

  • moved from psr-0 autoload to psr-4
  • added Method directory, then methods are now in \sylouuu\Curl\Method\

2014-05-30 - 0.6.1

  • removed exception when data option is not specified for Post Put and Patch

2014-05-22 - 0.6.0 (BC break)

  • moved url option to the first constructor parameter

2014-05-20 - 0.5.0 (BC break)

  • renamed repository from php-rest-client to php-curl
  • refactored all code
  • added autoclose option
  • added the way to get/set cURL options
  • added the way to get cURL info
  • sources are now psr-2 compliant

2014-05-13 - 0.4.0 (BC break)

  • renamed RESTClient.class.php to RESTClient.php
  • moved RESTClient.php to /src
  • moved RESTClientTest.php to /tests
  • added HEAD, OPTIONS and PATCH support
  • added getHeader method
  • renamed getJSON to getResponse
  • removed JSON validation
  • added sylouuu namespace
  • removed gulp

2014-05-09 - 0.3.0

  • added ssl option

2014-04-06 - 0.2.1

  • added exception if invalid JSON format returned

2014-04-04 - 0.2.0

  • new way to retrieve result
  • added HTTP status code

2014-04-01 - 0.1.0

  • refactored class
  • removed constructor parameter
  • added unit tests

2014-03-24 - 0.0.2

  • added $api_url as a constructor parameter

2014-02-05 - 0.0.1

  • Initial release

sylouuu/php-curl 适用场景与选型建议

sylouuu/php-curl 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19.84k 次下载、GitHub Stars 达 27, 最近一次更新时间为 2014 年 05 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「curl」 「http」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 sylouuu/php-curl 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 sylouuu/php-curl 我们能提供哪些服务?
定制开发 / 二次开发

基于 sylouuu/php-curl 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 19.84k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 28
  • 点击次数: 14
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 27
  • Watchers: 2
  • Forks: 11
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-05-20