piggly/php-api-client 问题修复 & 功能扩展

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

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

piggly/php-api-client

Composer 安装命令:

composer require piggly/php-api-client

包简介

A client starter-kit used to interact with any REST API.

README 文档

README

Latest Version on Packagist Software License

This open-source library is provided for cases where you need to do APIs calls with large flexibility that officials SDK may not provide out-of-the-box. When using this Starter-Kit, you will be able to make better cURL requests with a smart managing of requests and responses.

Installation

Composer

  1. At you console, in your project folder, type composer require piggly/php-api-client;
  2. Don't forget to add Composer's autoload file at your code base require_once('vendor/autoload.php);.

Manual install

  1. Download or clone with repository with git clone https://github.com/piggly-dev/php-api-client.git;
  2. After, goes to cd /path/to/piggly/php-api-client;
  3. Install all Composer's dependencies with composer install;
  4. Add project's autoload file at your code base require_once('/path/to/piggly/php-api-client/vendor/autoload.php);.

Dependencies

The library has the following external dependencies:

How to?

Configuration

This library provides, first, a Configuration object. You must create it before send any request. You may create many Configuration objects to provide different environments or you could use .env files to manages it.

See below all configurations:

Configuration Method Description
Api Keys apiKey() and getApiKey() Add a new api key. You may have many.
Access Token accessToken() and getAccessToken() Add an access token.
Client Username username() and getUsername() HTTP Client Username to Basic Authentication.
Client Password password() and getPassword() HTTP Client Secret to Basic Authentication.
HTTP Headers headers() and cloneHeaders() Manage the defaults HTTP Headers.
Host host() and getHost() Host URL to send Requests, without paths.
UserAgent userAgent() and getUserAgent() UserAgent which is making Requestes.
Timeout timeout() and getTimeout() The maximum number of seconds to allow cURL functions to execute.
Connection Timeout connectionTimeout() and getConnectionTimeout() The maximum amount of time in seconds that is allowed to make the connection to the server
Proxy Host proxyHost() and getProxyHost() Host for proxying.
Proxy Port proxyPort() and getProxyPort() Port for proxying.
Proxy Type proxyType() and getProxyType() cURL proxy type, e.g. CURLPROXY_HTTP or CURLPROXY_SOCKS5.
Proxy User proxyUser() and getProxyUser() User for proxying.
Proxy Password proxyPassword() and getProxyPassword() Password for proxying.
Custom Configuration custom() and getCustom() Add any custom configuration.
SSL Validation verifySSL() and shouldVerifySSL() If should or not do a SSL verification.
Default Configuration default and getDefault() A static default configuration objects. It will be available to all Configuration instances.

Development configurations:

With debug() method you will be able to enable requests debugging. You also may set a Logger to log messages across code lifetime. The logger() method is expecting a Logger from monolog/monolog package. Only when a Logger is set the log() method will output messages into Logger.

NOTE: Debug messages will only be output to Logger when debug() mode is active.

Requests

After configuration is done, you are ready to use Request object. You may set a Configuration object to Request constructor or it will get the default configuration.

Then, you must start with request's methods: delete(), head(), get(), options(), patch(),post() and put(). After you will be able to manipulate request with methods below:

Method Description
basicAuth() Apply basic authorization header.
authorization() Apply authorization header with an api key.
getHeaders() Manages all request headers.
headers() Applies headers to request headers.
data() Set the post data.
params() Set parameters replaces to URI. At URI, /posts/{id} you may set params(['id', 1]), then it will be replaced /posts/1.
query() Set query string parameters.
responseType() Set the response type as \SplFileObject, string or array.

When your request is done, you need to use call() method. The call() method will:

  • Throw an ApiRequestException, if response fails;
  • Throw an ApiResponseException, if response has a non 2xx response;
  • Return a Response object with all response data and request reference.

Headers

To manage headers, there is a HeaderBag object that will have all needed methods to better handle with headers.

Samples

You can see very lightweight samples at /samples folder.

Changelog

See the CHANGELOG file for information about all code changes.

Testing the code

This library uses the PHPUnit. We carry out tests of all the main classes of this application.

vendor/bin/phpunit

You must always run tests with PHP 7.2 and greater.

Contributions

See the CONTRIBUTING file for information before submitting your contribution.

Credits

Support the project

Piggly Studio is an agency located in Rio de Janeiro, Brazil. If you like this library and want to support this job, be free to donate any value to BTC wallet 3DNssbspq7dURaVQH6yBoYwW3PhsNs8dnK ❤.

License

MIT License (MIT). See LICENSE.

piggly/php-api-client 适用场景与选型建议

piggly/php-api-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 956 次下载、GitHub Stars 达 6, 最近一次更新时间为 2021 年 05 月 17 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 956
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 11
  • 依赖项目数: 0
  • 推荐数: 2

GitHub 信息

  • Stars: 6
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-05-17