medianet-dev/p-connector 问题修复 & 功能扩展

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

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

medianet-dev/p-connector

Composer 安装命令:

composer require medianet-dev/p-connector

包简介

Link projects together with restful apis

README 文档

README


StyleCI

PConnector is a package that makes linking projects together much easier than performing a manual Guzzle or Curl requests. You only need to set some basic settings of your gateway(s) and that's it.

Installation

You can install the package via composer:

composer require medianet-dev/p-connector

Then migrate the package table by running:

php artisan migrate

Usage

Here we will pass through some examples of doing some basic and advanced Api calls .

Configuring your gateway(s)

Before start playing with the package you need to setup a profile for your gateway, and to do so, you need to publish the configuration file by running:

php artisan vendor:publish --provider="MedianetDev\PConnector\PConnectorServiceProvider"

After publishing the configuration, go to p-connector.php config file and setup you first profile setting:

# file: p-connector.php

/*
|--------------------------------------------------------------------------
| List of the available profiles with it's configurations
|--------------------------------------------------------------------------
*/
'profiles' => [
    'demo' => [
        'protocol' => 'https',
        'host' => 'my-json-server.typicode.com',
        'port' => 443,
        'prefix' => 'typicode/demo',
    ],
],

Note that you can set more than one profile.

And that's it for basic configuration, other available settings are described in the configuration file itself.

Some usage examples

// Import the PConnector facade to get available methods hints 
use MedianetDev\PConnector\Facade\PConnector;

// Or use tha alias without available methods hints
# use PConnector;

// Method 1:
$demo = PConnector::get('posts/1');
echo '<h1>'.$demo->title.'</h1>';

// Method 2:
# $demo = PConnector::send('posts/1', [], 'GET');
# echo '<h1>'.$demo->title.'</h1>';

We don't have to tell the PConnector witch profile to use if we set the default_profile parameter, but if we have more than on profile and we want to switch between them, we can use the profile() function like so:

use MedianetDev\PConnector\Facade\PConnector;

$demo = PConnector::profile('demo')->get('posts/1');
$demo2 = PConnector::profile('demo_2')->get('users/1');

IMPORTANT: The profile function will apply a specific profile configuration and erase any other setting, so if you want to switch the profile call the profile() function before any other function.

Configuration:

Some setting can be set as a profile setting, you will find those setting with [AAPS] annotation to distinguish them (AAPS: Also A Profile Setting). To set settings for a profile, you have to put them inside the appropriate profile array. Also, note if a setting is defined inside a profile and as global in the same time, the profile one will be used.

Http Methods:

send(string $path = '', array $data = [], string $method = 'GET')
post(string $path = '', array $data = [])
get(string $path = '', array $data = [])
put(string $path = '', array $data = [])
patch(string $path = '', array $data = [])
delete(string $path = '', array $data = [])

Authentication:

PConnector supports sending request with authentication, and to do so you need to configuration the authentication settings for the desired profile or for all the profiles.

# file: p-connector.php

'auth' => [ // [AAPS]
    // Whether to use authentication by default or not (you can make an exception with withAuth() and withoutAuth() methods)
    'authenticate_by_default' => false,
    // How to authenticate (Accepted values are: api_key, basic, bearer)
    'auth_method' => 'bearer',
    // If the api_key method is use then you SHOULD provide an api_key key
    // 'api_key' => 'X-AUTH-TOKEN',
    // The path of the login
    'login_path' => 'login',
    // The http method used to login
    'login_http_method' => 'POST',
    'credentials' => [
        'username' => 'username',
        'password' => 'password',
    ],
    // The expected http code response when login in
    'success_login_code' => [200],
    // When should we re-authenticate
    're_auth_on_codes' => [401],
    // Where to find the token in the response (you can youse the dot syntax EX: 'data.user.auth.token')
    'token_path' => 'token',
],

You can configure the authentication settings for the desired profile by adding the auth array in the profile config.

'profiles' => [
    'demo' => [
        ...,
        'auth' => [ // Add this section for custom auth settings
            // Authentication settings specific to this profile
        ],
    ],
],

If for some reason you don't want to include authentication while sending some request, you can tell the PConnector like so:

use MedianetDev\PConnector\Facade\PConnector;

$demo = PConnector::profile('demo')->withoutAuth()->get('posts/1');
# $demo = PConnector::withoutAuth()->get('users/1');

Remember that the profile function will apply a specific profile configuration and erase any other setting (in our case the withoutAuth() function), that why we use the profile() before withoutAuth().

Request

# file: p-connector.php

// Request settings
'request' => [ // [AAPS]
    // The default request headers
    'headers' => ['Accept' => 'application/json'],
    // Whether to send the language through the header by default or not
    'enable_localization' => true,
    // Handle http errors or not
    'http_errors' => false,
    // Http connect timeout value
    'connect_timeout' => 3,
    // Http timeout value
    'timeout' => 3,
    // The data type; json, form-data, ...
    'post_data' => 'json',

    // Verify the SSL certificate or not
    'verify' => true,
],
Localization

Send Accept-Language header when needed:

use MedianetDev\PConnector\Facade\PConnector;

$demo = PConnector::lang('ar')->get('posts/1');

Or configure the PConnector to send it automatically via the configuration file.

Quickly change the current url
PConnector::url('https://jsonplaceholder.typicode.com')->get('todos')

Or you can use tha alias setUrl() method

This is the list of getters available for the request:
  • getRequestUrl()
  • getRequestMethod()
  • getRequestData()

Response

You can configure how do you want to save the response body:

  • As a string
  • As an object
  • As an array
# file: p-connector.php

'decode_response' => 'object', // [AAPS]

And if you want to change that for a specific response:

use MedianetDev\PConnector\Facade\PConnector;

$demo = PConnector::objectResponse()->get('posts/1');
$demo = PConnector::htmlResponse()->get('posts/1');
$demo = PConnector::arrayResponse()->get('posts/1');

The received response body can be retrieved using getResponseBody() after sending the request.

use MedianetDev\PConnector\Facade\PConnector;

$demo = PConnector::get('posts/1')->getResponseBody();

This is the list of getters available for the response:

  • getResponseBody()
  • getResponseStatusCode()
  • getResponseHeaders()

If you decode the response body as an object, you can use getAttribute('attribute_name') or ->attribute_name to retrieve a specific attribute, or even you can go more deep and do getAttribute('object.object.attribute_name', 'fallback value'), as an example:

use MedianetDev\PConnector\Facade\PConnector;

$demo = PConnector::get('posts/1')->getAttribute('title');
$demo = PConnector::get('posts/1')->title;
$demo = PConnector::get('posts/1')->getAttribute('author.name', 'Unknown author');

Response status code checks:

  • function responseCodeIs(int $code): bool
  • function responseCodeNot(int $code): bool
  • function responseCodeIn(array $codes): bool
  • function responseCodeNotIn(array $codes): bool
  • function responseOK(): bool
  • function responseNOK(): bool

Example:

use MedianetDev\PConnector\Facade\PConnector;

$demo = PConnector::get('posts/1');
if ($demo->responseCodeNot(200)) {
    echo 'This is not what I was expecting from Demo!';
} else {
    echo '<h1>'.$demo->title.'</h1>';
}

$demo = PConnector::post('posts', ['title' => 'My title']);
 if ($demo->responseCodeNotIn([200, 201])) {
    echo 'This is not what I was expecting from Demo!';
} else {
    echo 'Post created, Hola!';
}

Logging

# file: p-connector.php

// Log requests & responses to log files or not (you can make an exception with withLog() and withoutLog() methods)
'log' => false, // [AAPS]

Example 1:

use MedianetDev\PConnector\Facade\PConnector;

$demo = PConnector::get('posts/1')->dump();

if ($demo->responseCodeNot(200)) {
    $demo->log();
} else {
    echo '<h1>'.$demo->title.'</h1>';
}

Example 2:

use MedianetDev\PConnector\Facade\PConnector;

$demo = PConnector::withLog()->get('posts/1');

Debugging

You can debug the PConnector object using the dump() or the dd() method.

use MedianetDev\PConnector\Facade\PConnector;

$demo = PConnector::get('posts/1')->dump();
$demo = PConnector::get('posts/1')->dd();

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email soufiene.slimi@medianet.com.tn instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

medianet-dev/p-connector 适用场景与选型建议

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

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

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

围绕 medianet-dev/p-connector 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

与 medianet-dev/p-connector 相关的其它包

同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-10-25