fightbulc/php_curl 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

fightbulc/php_curl

Composer 安装命令:

composer require fightbulc/php_curl

包简介

PHP CURL as object-oriented implementation.

README 文档

README

PHP_CURL is an object-oriented implementation of the PHP's functional CURL interface.

Basic Usage

Since PHP_CURL allows for method-chaining, the static init method is used to generate an instance of a CURL object (similar to curl_init).

<?php

$curl = CURL::init("http://reddit.com/r/php/.json");

?>

We can then set some CURL options:

<?php

$curl = CURL::init("http://www.reddit.com/r/php/.json")
            ->setReturnTransfer(TRUE);

?>

All set... functions return an instance of the current CURL object, so you are free to set as many of the options in a row as necessary before executing.

<?php

$curl = CURL::init("http://www.reddit.com/r/php/.json")
        ->setReturnTransfer(TRUE);

$response = $curl->execute():

?>

All of the functions are close matches to the CURLOPT_* alternatives.

Comparison to functional cURL

The basic example given in the PHP manual for functional cURL is as follows:

<?php

$ch = curl_init("http://www.example.com/");
$fp = fopen("example_homepage.txt", "w");

curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);

curl_exec($ch);
curl_close($ch);
fclose($fp);
?>

We can perform the same operation with PHP_CURL like so:

<?php

$fp = fopen("example_homepage.txt", "w");

CURL::init("http://www.example.com/")
    ->setFile($fp)
    ->setHeader(0)
    ->execute()
    ->close();

fclose($fp);

?>

There isn't much of a difference in terms of code-size or performance, however, the PHP_CURL approach is arguably more readible, and less repeatitive.

One major benefit to using this approach is that it enables us to take advantage of PHP editors with auto-completion, so setting multiple cURL options should be somewhat easier.

Issues

At present, there is no support for curl_multi.... Support for curl_multi... is coming soon.

Why do this?

I found that writing for CURL in PHP was repeatitive and cumbersome, and having a quick, object-oriented and more readable way of performing quick CURL tasks was something I've wanted for a while.

I don't claim to have done anything special here, nor do I propose that this should be used in any sort of production environment. It's quick and dirty, but it's working for me.

I strongly encoruage any sort of feedback, patches or contributions. I'd be more than happy to open up this project to more developers if there is any demand.

FAQ

Q. Why CURL::init() instead of new CURL()?

A. If you want to take advantage of method-chaining, use CURL::init, since PHP's constructors don't allow for things like (new Curl())->foo().

However, if you prefer to instanciate an object and then use that object, support for $foo = new CURL() is allowed. Both will work.

Q. Couldn't you have just done this with a single __call() wrapper, or use __get() and __set()?

This is true, however, a design decision was made to expand all the possible functions to get... and set... functions individually. This means that it's easier for a newcomer to the code to see clearly what methods are available. Not to mention that editors with auto-completion ability aren't often clever enough to discover magic method handling.

At the end of the day, whether it's handled using PHPDoc paramters and magic methods, or all methods are out there in plain sight, it doesn't make a difference either way, and I feel that if all methods are in plain sight, then there is little to no confusion as to what methods are available.

fightbulc/php_curl 适用场景与选型建议

fightbulc/php_curl 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 74.48k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2012 年 10 月 19 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 fightbulc/php_curl 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 74.48k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 9
  • 依赖项目数: 8
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 0
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-3.0
  • 更新时间: 2012-10-19