samuraee/easycurl
Composer 安装命令:
composer require samuraee/easycurl
包简介
Super easy and flexible wrapper class for PHP cURL extension
关键字:
README 文档
README
Super easy and flexible wrapper class for PHP 7.0+ cURL extension
See php.net/curl for more information about the libcurl extension for PHP.
It's a fairly simple library, so if you want something more powerful take a look at Guzzle.
Install
via Composer (recommended)
composer require samuraee/easycurl '~1.0'
via download
Just grab the latest release.
Usage
Initialization
try { $curl = new \Samuraee\EasyCurl(); } catch (Exception $e) { echo $e->getMessage(); }
Performing request
The EasyCurl object supports 5 types of requests: HEAD, GET, POST, PUT, and DELETE. You must specify an url to request and optionally specify an associative array or query string of variables to send along with it.
$response = $curl->head($url, $params); $response = $curl->get($url, $params); $response = $curl->post($url, $params); $response = $curl->put($url, $params); $response = $curl->delete($url, $params);
To use a custom request methods, you can call the request method:
$response = $curl->request($url, 'ANY_CUSTOM_REQUEST_TYPE', $params);
Examples:
$response = $curl->get('google.com?q=test'); $response = $curl->get('google.com?q=test', array('some_variable' => 'some_value')); // EasyCurl will append '&some_variable=some_value' to the url $response = $curl->post('test.com/posts', array('title' => 'Test', 'body' => 'This is a test'));
All requests return response body as is or throw a EasyCurlException if an error occurred.
Performing POST/PUT request with raw payload
Some times you need to send not encoded POST params, but a raw JSON or other raw data format.
$response = $curl->rawPost($url, $jsonData); $response = $curl->rawPut($url, 'raw random data');
Note that data is sending as as, without any URL-encoding manipulation. Keep that in mind.
You might also need to change the content type header for those types of request:
$curl->addHeader('Content-Type', 'text/plain'); // or $curl->addHeader('Content-Type', 'application/json'); // and then $response = $curl->rawPost($url, $jsonData);
It depends on API server-side you are working with.
Getting additional information about request sent
$info = $curl->getTransferInfo();
This will give you associative array with following keys:
url- Last effective URLcontent_type- Content-Type: of downloaded object, NULL indicates server did not send valid Content-Type: headerhttp_code- Last received HTTP codeheader_size- Total size of all headers receivedrequest_size- Total size of issued requests, currently only for HTTP requestsfiletime- Remote time of the retrieved document, if -1 is returned the time of the document is unknownssl_verify_result- Result of SSL certification verification requested by setting CURLOPT_SSL_VERIFYPEERredirect_count- Number of redirects it went through if CURLOPT_FOLLOWLOCATION was settotal_time- Total transaction time in seconds for last transfernamelookup_time- Time in seconds until name resolving was completeconnect_time- Time in seconds it took to establish the connectionpretransfer_time- Time in seconds from start until just before file transfer beginssize_upload- Total number of bytes uploadedsize_download- Total number of bytes downloadedspeed_download- Average download speedspeed_upload- Average upload speeddownload_content_length- content-length of download, read from Content-Length: fieldupload_content_length- Specified size of uploadstarttransfer_time- Time in seconds until the first byte is about to be transferredredirect_time- Time in seconds of all redirection steps before final transaction was startedcertinfo- There is official description for this field yetrequest_header- The request string sent. For this to work, add the CURLINFO_HEADER_OUT option
You can also easily fetch any single piece of this array:
$httpCode = $curl->getTransferInfo('http_code');
Cookie sessions
To maintain a session across requests and cookies support you must set file's name where cookies to store:
$curl->setCookieFile('some_file_name.txt');
This file must be writable or the EasyCurlException will be thrown.
Basic configuration options
You can easily set the referer, user-agent, timeout and whether or not follow redirects:
$curl->setReferer('http://google.com'); $curl->setUserAgent('some user agent string'); $curl->setTimeout(15); // seconds $curl->setFollowRedirects(true); // to follow redirects
HTTP Basic Authentication
You can set a username and password for use in HTTP basic auth:
$curl->setAuthType(); $curl->setAuthCredentials('username', 'password');
Setting custom headers
You can set custom headers to send with the request:
$curl->addHeader('Host', '98.52.78.243'); $curl->addHeader('Some-Custom-Header', 'Some Custom Value');
Or use a single array:
$curl->addHeader(array('Host'=>'98.52.78.243', 'Some-Custom-Header'=>'Some Custom Value'));
Setting custom cURL options
You can set/override any cURL option (see the curl_setopt documentation for a list of them):
$curl->addOption(CURLOPT_AUTOREFERER, true);
Contributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Added some feature') - Push to the branch (
git push origin my-new-feature) - Create new Pull Request
samuraee/easycurl 适用场景与选型建议
samuraee/easycurl 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.33k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2021 年 02 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「curl」 「http」 「util」 「helper」 「http-client」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 samuraee/easycurl 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 samuraee/easycurl 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 samuraee/easycurl 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Camilo3rd's PHP Utils Library
cURL and cURL based Soap-Client for PHP
repository php library
Simple cURL wrapper
Collection of exception class and utilities
PHP-ClamAV library - to scan the file for viruses
统计信息
- 总下载量: 9.33k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 9
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-02-24