Compare commits
No commits in common. "1aedddc66e748267cb41a8992713c93652cc09cb" and "57335f98ce284d232c0cc942595c6baa408122b1" have entirely different histories.
1aedddc66e
...
57335f98ce
102
README.md
102
README.md
|
@ -1,102 +0,0 @@
|
|||
# Internal API Server/Client(PHP版)
|
||||
|
||||
|
||||
> 该项目使用 composer 来完成加载
|
||||
|
||||
执行
|
||||
```bash
|
||||
composer config repositories.php-internal-api-client vcs git@git.int.haowumc.com:arch/php-internal-api-client.git
|
||||
composer require arch/php-internal-api-client
|
||||
```
|
||||
|
||||
|
||||
### 如何使用
|
||||
|
||||
#### Server
|
||||
|
||||
* 注册中间件
|
||||
|
||||
```php
|
||||
$app->routeMiddleware([
|
||||
'internal' => PdInternalApi\Middleware\InternalApi::class,
|
||||
]);
|
||||
|
||||
```
|
||||
|
||||
* 增加配置文件:```config/internal_api.php``` , 在server数组中为调用方增加 secret。
|
||||
|
||||
```php
|
||||
<?php
|
||||
return [
|
||||
/**
|
||||
* 对内部其他系统提供api
|
||||
*
|
||||
* 格式为:
|
||||
* 调用方标识 => 调用方secret
|
||||
*/
|
||||
'server' => [
|
||||
// app id => app secret
|
||||
'{{app name}}' => env('INTERNAL_SERVER_{{app name}}_SECRET'),
|
||||
],
|
||||
];
|
||||
```
|
||||
|
||||
* 在项目 .env 文件中增加如下配置
|
||||
|
||||
```
|
||||
INTERNAL_SERVER_{{app name}}_SECRET=323232323
|
||||
```
|
||||
|
||||
* 在路由中启用中间件
|
||||
|
||||
```php
|
||||
$route->group(['middleware'=>'internal'],function()use($router){
|
||||
//这里添加对应路由
|
||||
});
|
||||
```
|
||||
|
||||
|
||||
#### Client
|
||||
|
||||
* 注册服务
|
||||
|
||||
```PHP
|
||||
$app->register(PdInternalApi\ServiceProvider::class);
|
||||
```
|
||||
* 增加配置文件:```config/internal_api.php``` , 在server数组中为调用方增加 secret。
|
||||
|
||||
```php
|
||||
<?php
|
||||
return [
|
||||
/**
|
||||
* 配置可以使用的内部系统
|
||||
*/
|
||||
'client' => [
|
||||
/**
|
||||
* key 为 api 项目的名称。
|
||||
* 数组为该系统的配置,由该系统的负责人提供
|
||||
*/
|
||||
'{app name}' => [
|
||||
'base_uri' => env('INTERNAL_CLIENT_{{app name}}_URI', ''),
|
||||
'appid' => env('INTERNAL_CLIENT_{{app name}}_APPID', ''),
|
||||
'secret' => env('INTERNAL_CLIENT_{{app name}}_SECRET', ''),
|
||||
'timeout' => 30,
|
||||
],
|
||||
],
|
||||
];
|
||||
```
|
||||
|
||||
* 在项目 .env 文件中增加如下配置
|
||||
|
||||
```
|
||||
INTERNAL_CLIENT_{app name}_URI=http://test.in.haowumc.com/
|
||||
INTERNAL_CLIENT_{app name}_APPID=test
|
||||
INTERNAL_CLIENT_{app name}_SECRET=sdfsdfsdf
|
||||
```
|
||||
|
||||
* 调用
|
||||
|
||||
```php
|
||||
$api = app('internal.api.{{app name}}')
|
||||
$resp = $api->call('{{URI}}',$params);
|
||||
```
|
|
@ -1,5 +1,5 @@
|
|||
{
|
||||
"name": "paidian/php-internal-api-client",
|
||||
"name": "pd_arch/internal_api",
|
||||
"type": "library",
|
||||
"require": {
|
||||
"guzzlehttp/guzzle": "^6.3",
|
||||
|
@ -7,7 +7,7 @@
|
|||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"PdInternalApi\\": "src/"
|
||||
"InternalApi\\": "src/"
|
||||
},
|
||||
"files": [
|
||||
"src/helpers.php"
|
||||
|
|
1194
composer.lock
generated
Normal file
1194
composer.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
|
@ -1,19 +1,29 @@
|
|||
<?php
|
||||
|
||||
namespace PdInternalApi;
|
||||
namespace InternalApi;
|
||||
|
||||
class Client
|
||||
{
|
||||
|
||||
protected $service_name;
|
||||
protected $currentApp;
|
||||
protected $config;
|
||||
|
||||
public function __construct($service_name, $config)
|
||||
public function __construct($config)
|
||||
{
|
||||
$this->service_name = $service_name;
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $app
|
||||
* @return $this
|
||||
*/
|
||||
public function app($app)
|
||||
{
|
||||
if (isset($this->config[$app]))
|
||||
$this->currentApp = $app;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用api,如果状态码不为200则抛出异常
|
||||
* @param $uri
|
||||
|
@ -23,7 +33,8 @@ class Client
|
|||
*/
|
||||
public function call($uri, $params)
|
||||
{
|
||||
$config = array_merge(['timeout' => 3], $this->config);
|
||||
$config = array_merge(['timeout' => 3],
|
||||
$this->config[$this->currentApp]);
|
||||
$secret = $config['secret'];
|
||||
unset($config['secret']);
|
||||
$client = new \GuzzleHttp\Client($config);
|
||||
|
@ -31,10 +42,10 @@ class Client
|
|||
$params['timestamp'] = time();
|
||||
$params['sign'] = sign($params, $secret);
|
||||
$resp = $client->post($uri, ['form_params' => $params]);
|
||||
if ($resp->getStatusCode() != 200) {
|
||||
return false;
|
||||
}
|
||||
if ($resp->getStatusCode() == 200) {
|
||||
return \GuzzleHttp\json_decode($resp->getBody(), true);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,11 +1,11 @@
|
|||
<?php
|
||||
|
||||
namespace PdInternalApi\Middleware;
|
||||
namespace InternalApi\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Str;
|
||||
use function PdInternalApi\sign;
|
||||
use function InternalApi\sign;
|
||||
|
||||
class InternalApi
|
||||
{
|
||||
|
@ -15,27 +15,6 @@ class InternalApi
|
|||
app()->configure('internal_api');
|
||||
}
|
||||
|
||||
private function isClientIPPermitted($ip)
|
||||
{
|
||||
if (!app()->environment('production', 'staging')) {
|
||||
return true;
|
||||
}
|
||||
if (Str::startsWith($ip, [
|
||||
'127.0.0.1',
|
||||
//局域网
|
||||
'192.168.',
|
||||
//vpc
|
||||
'10.0.',
|
||||
//pod network
|
||||
'172.20.',
|
||||
//北京办公区
|
||||
'172.16.'
|
||||
])) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
|
@ -46,8 +25,11 @@ class InternalApi
|
|||
public function handle($request, Closure $next)
|
||||
{
|
||||
$ip = $request->getClientIp();
|
||||
if (!$this->isClientIPPermitted($ip)) {
|
||||
return new JsonResponse("$ip is forbidden", 403);
|
||||
|
||||
if (!Str::startsWith($ip, [
|
||||
'127.0.0.', '192.168.', '10.0.'
|
||||
])) {
|
||||
return new JsonResponse('', 404);
|
||||
}
|
||||
|
||||
$params = $request->all();
|
||||
|
|
|
@ -1,40 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace PdInternalApi;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ServiceProvider extends \Illuminate\Support\ServiceProvider
|
||||
{
|
||||
|
||||
public function boot(){
|
||||
Request::setTrustedProxies([
|
||||
//pod network
|
||||
'172.20.0.0/16',
|
||||
//vpc
|
||||
'10.0.0.0/16',
|
||||
//local
|
||||
'127.0.0.1',
|
||||
//北京办公区
|
||||
'172.16.0.0/16',
|
||||
//aliyun slb
|
||||
'100.116.0.0/16',
|
||||
], Request::HEADER_X_FORWARDED_ALL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register any application services.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
$this->app->configure('internal_api');
|
||||
foreach (config('internal_api.client') as $service_name => $config) {
|
||||
$this->app->singleton('internal.api.' . $service_name, function () use ($service_name, $config) {
|
||||
return new Client($service_name, $config);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
}
|
28
src/ServiceProvider/InternalApiServiceProvider.php
Normal file
28
src/ServiceProvider/InternalApiServiceProvider.php
Normal file
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
namespace InternalApi\ServiceProvider;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use InternalApi\Client;
|
||||
|
||||
class InternalApiServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Register any application services.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
$this->app->configure('internal_api');
|
||||
$this->app->singleton('internal.api', function () {
|
||||
return new Client(config('internal_api.client'));
|
||||
});
|
||||
foreach (config('internal_api.client') as $key => $config) {
|
||||
$this->app->singleton('internal.api.' . $key, function () use ($key) {
|
||||
return $this->app['internal.api']->app($key);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -1,36 +0,0 @@
|
|||
<?php
|
||||
return [
|
||||
/**
|
||||
* 对内部其他系统提供api
|
||||
*
|
||||
* 格式为:
|
||||
* 调用方标识 => 调用方secret
|
||||
*/
|
||||
'server' => [
|
||||
// app id => app secret
|
||||
'finance' => 'a249f0e40e31877adedd76fac6c6c116',
|
||||
'mall' => '8b54fdc7b317906d93c83152be5f956b',
|
||||
'xiaoke' => '8b54fdc7b317906d93c83152be5f956b',
|
||||
],
|
||||
/**
|
||||
* 配置可以使用的内部系统
|
||||
*/
|
||||
'client' => [
|
||||
/**
|
||||
* key 为 api 项目的名称。
|
||||
* 数组为该系统的配置,由该系统的负责人提供
|
||||
*/
|
||||
'finance' => [
|
||||
'base_uri' => env('INTERNAL_CLIENT_FINANCE_URI', ''),
|
||||
'appid' => env('INTERNAL_CLIENT_FINANCE_APPID', ''),
|
||||
'secret' => env('INTERNAL_CLIENT_FINANCE_SECRET', ''),
|
||||
'timeout' => 30,
|
||||
],
|
||||
'mall' => [
|
||||
'base_uri' => env('INTERNAL_CLIENT_MALL_URI', ''),
|
||||
'appid' => env('INTERNAL_CLIENT_MALL_APPID', ''),
|
||||
'secret' => env('INTERNAL_CLIENT_MALL_SECRET', ''),
|
||||
'timeout' => 3,
|
||||
],
|
||||
],
|
||||
];
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
|
||||
namespace PdInternalApi;
|
||||
namespace InternalApi;
|
||||
|
||||
/**
|
||||
* 签名
|
||||
|
|
Loading…
Reference in New Issue
Block a user