amsify42/php-curl-http
Composer 安装命令:
composer require amsify42/php-curl-http
包简介
This is a PHP package for making curl http request and getting the response
README 文档
README
This is the PHP package for making http request through cURL and getting response.
Installation
$ composer require amsify42/php-curl-http
Table of Contents
1. Initialization
For creating cURL http request, we can do like this by passing url to it.
$curlHttp = new Amsify42\CurlHttp\CurlHttp('http://www.sample.com/users');
or with helper method
$curlHttp = get_curl_http('http://www.sample.com/users');
2. Request
If the request method is not of type GET, we can pass this with CurlHtpp instance
$curlHttp = new Amsify42\CurlHttp\CurlHttp('http://www.sample.com/user/create'); $curlHttp->setRequestMethod('POST'); $curlHttp->setRequestData(['name' => 'test', 'salary' => 123]);
or with Amsify42\CurlHtpp\Request instance
$curlRequest = new Amsify42\CurlHtpp\Request(); $curlRequest->setMethod('POST'); /** * Set one of the header as Content-Type: application/json if you want to pass bodyData as json */ $curlRequest->setHeaders(['Content-Type: application/json']); $curlRequest->setData(['name' => 'test', 'salary' => 123]); $curlHttp = new Amsify42\CurlHttp\CurlHttp('http://www.sample.com/user/create', $curlRequest);
With Amsify42\CurlHtpp\Request we can also call these methods.
$curlRequest->setMethod('PUT'); $curlRequest->setHeaders(['Authorization: Bearer sf23rsdf23fds']); /** * Set request ContentType as json if you want to pass bodyData as json */ $curlRequest->setContenType('json'); $curlRequest->setData(['name' => 'test', 'salary' => 123]);
You can simply pass json encoded request data also if you don't want to pass header or contentType for json
$curlRequest->setData(json_encode(['name' => 'test', 'salary' => 123]));
For headers, you can also pass array items as key values
$curlRequest->setHeaders(['Authorization' => 'Bearer sf23rsdf23fds']);
3. Response
After executing the cURL through CurlHttp, we will get response of type Amsify42\CurlHttp\Response
$curlHttp = get_curl_http('http://www.sample.com/users'); $response = $curlHttp->execute();
We can call these methods with the response
/* To get the response code */ $response->getCode(); /* To get the response body data */ $response->getBodyData(); /* To get the response headers as array */ $response->getHeaders(); /* To get the json decoded data of response */ $response->json();
For executing different methods like GET, POST, PUT and DELETE, you can also call these methods to get response with a single call.
$response = CurlHttp::get('https://www.somsite.com/users'); $response = CurlHttp::post('https://www.somsite.com/user/create', ['name' => 'dummy'], ['Authorization: Bearer sdf23rsdf23'], 'json'); $response = CurlHttp::put('https://www.somsite.com/user/2', ['name' => 'dummy...edited'], ['Authorization: Bearer sdf23rsdf23'], 'json'); $response = CurlHttp::delete('https://www.somsite.com/user/2', ['Authorization: Basic sdf23rsdf23']);
Methods post(), put() and delete() expects three more optional parameters
data - expects data array or defaults to NULL headers - expects headers array or defaults to NULL contentType - expects either string 'json' or defaults to NULL
For json request data, instead of passing contentType parameter as json to these methods you can also pass one of the header as Content-Type: application/json
$response = CurlHttp::post('https://www.somsite.com/user/create', ['name' => 'dummy'], ['Authorization: Bearer sdf23rsdf23', 'Content-Type: application/json']);
or you can simply pass json encoded request data
$response = CurlHttp::post('https://www.somsite.com/user/create', json_encode(['name' => 'dummy']), ['Authorization: Bearer sdf23rsdf23']);
amsify42/php-curl-http 适用场景与选型建议
amsify42/php-curl-http 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.86k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2020 年 03 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「curl」 「cUrl Http」 「php curl http」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 amsify42/php-curl-http 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 amsify42/php-curl-http 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 amsify42/php-curl-http 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
cURL and cURL based Soap-Client for PHP
repository php library
Simple cURL wrapper
A fluent PHP CURL wrapper
http客户端
Easily add Excepct-CT header to your project
统计信息
- 总下载量: 1.86k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 14
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-03-22