PHP的Akamai Edgegrid身份验证
该库需要Php 8+,并在Guzzle上以插入式替换客户端和中间件为基础上实现Akamai EdgeGrid身份验证方案。
要安装,请使用composer :
$ composer require akamai-open/edgegrid-client从发布页面下载PHAR文件,并将其包含在您的代码中:
```php
include 'akamai-open-edgegrid-auth.phar';
// Library is ready to use
```
AkamaiOpenEdgeGridClient扩展了GuzzleHttpClient作为倒入替换。它可以透明地签署API请求,而无需更改您使用Guzzle的其他方式。
要使用客户端,请调用AkamaiOpenEdgeGridClient->setAuth()或在向API请求之前,向构造函数提供AkamaiOpenEdgeGridAuthentication的实例。
$ client = new Akamai Open EdgeGrid Client ([
' base_uri ' => ' https://akab-h05tnam3wl42son7nktnlnnx-kbob3i3v.luna.akamaiapis.net '
]);
$ client -> setAuth ( $ client_token , $ client_secret , $ access_token );
// use $client just as you would GuzzleHttpClient
$ response = $ client -> get ( ' /identity-management/v3/user-profile ' );要生成您的凭据,请参阅创建身份验证凭据。
我们建议使用本地.edgerc身份验证文件。将您的凭据放在[default]的标题下,在您的本地主目录或网络服务器用户的主目录。
[default]
client_secret = C113nt53KR3TN6N90yVuAgICxIRwsObLi0E67/N8eRN=
host = akab-h05tnam3wl42son7nktnlnnx-kbob3i3v.luna.akamaiapis.net
access_token = akab-acc35t0k3nodujqunph3w7hzp7-gtm6ij
client_token = akab-c113ntt0k3n4qtari252bfxxbsl-yvsdj
您可以调用您的.edgerc文件:两种方式之一:
使用工厂方法AkamaiOpenEdgeGridClient::createFromEdgeRcFile() 。
$ client = Akamai Open EdgeGrid Client:: createFromEdgeRcFile ();
// use $client just as you would GuzzleHttpClient
$ response = $ client -> get ( ' /identity-management/v3/user-profile ' );指定凭据部分和/或.edgerc位置:
$ client = Akamai Open EdgeGrid Client:: createFromEdgeRcFile ( ' example ' , ' ../config/.edgerc ' );
// use $client just as you would GuzzleHttpClient
$ response = $ client -> get ( ' /identity-management/v3/user-profile ' );该库提供了一个命令行接口(CLI),具有模仿HTTPIE的一组有限的功能。
使用vendor/bin/http安装CLI或执行PHAR文件。
# Composer installed
$ ./vendor/bin/http --help
# For Windows
> php ./vendor/bin/http --help
# PHAR download
php akamai-open-edgegrid-client.phar --help您可以设置身份验证并指定HTTP方法(情况不敏感),其标题和任何JSON身体字段。
注意:我们的CLI显示了所有HTTP和身体数据。 JSON被形成。
| 争论 | 描述 |
|---|---|
--auth-type={edgegrid,basic,digest} | 设置身份验证类型。默认值none 。 |
--auth user:或--a user: | 设置要使用的.edgerc部分。与httpie-edgegrid不同,结肠( :是可选的。 |
Header-Name:value | 标题和值是结肠( :分开的。 |
jsonKey=value | 发送{"jsonKey": "value"}在POST或PUT身体。这也将自动设置Content-Type并Accept application/json的标题。 |
jsonKey:=[1,2,3] | 允许您指定原始JSON数据,并在体内发送{"jsonKey": [1, 2, 3]} 。 |
multipart/mime (文件上传)数据。此软件包提供了三个不同的中间件处理程序,您可以在使用Client时透明地添加到标准GuzzleHttpClient或作为处理程序。
AkamaiOpenEdgeGridHandlerAuthentication transparent api请求签名。AkamaiOpenEdgeGridHandlerVerbose for uppution(或log)响应。AkamaiOpenEdgeGridHandlerDebug for输出(或日志)错误。| 处理程序 | 称呼 |
|---|---|
Authentication | Client->setAuthentication()或以AkamaiEdgeGridAuthentication的实例传递给Client->__construct() 。 |
Verbose | Client->setInstanceVerbose()或Client::setVerbose()传递`true |
Debug | Client->setInstanceDebug() , Client::setDebug() ,或用`true debug调试配置选项 |
| 处理程序 | 例子 |
|---|---|
| 验证 | // Create the Authentication Handler
$ auth = Akamai Open EdgeGrid Handler Authentication:: createFromEdgeRcFile ();
// or:
$ auth = new Akamai Open EdgeGrid Handler Authentication ;
$ auth -> setAuth ( $ client_token , $ client_secret , $ access_token );
// Create the handler stack
$ handlerStack = GuzzleHttp HandlerStack:: create ();
// Add the Auth handler to the stack
$ handlerStack -> push ( $ auth );
// Add the handler to a regular GuzzleHttpClient
$ guzzle = new GuzzleHttp Client ([
" handler " => $ handlerStack
]); |
| 冗长 | // Create the handler stack
$ handlerStack = HandlerStack:: create ();
// Add the Auth handler to the stack
$ handlerStack -> push ( new Akamai Open EdgeGrid Handler Verbose ());
// Add the handler to a regular GuzzleHttpClient
$ guzzle = new GuzzleHttp Client ([
" handler " => $ handlerStack
]); // Create the handler stack
$ handlerStack = HandlerStack:: create ();
// Add the Auth handler to the stack
$ handlerStack -> push ( new Akamai Open EdgeGrid Handler Debug ());
// Add the handler to a regular GuzzleHttpClient
$ guzzle = new GuzzleHttp Client ([
" handler " => $ handlerStack
]); |
版权所有©2022 Akamai Technologies,Inc。保留所有权利
根据Apache许可证(版本2.0(“许可”)获得许可;除了符合许可外,您不得使用此文件。您可以在http://www.apache.org/licenses/license-2.0上获取许可证的副本。
除非适用法律要求或以书面形式同意,否则根据许可证分配的软件是按照“原样”分发的,没有任何明示或暗示的任何形式的保证或条件。请参阅许可证,以获取执行许可条款和限制的特定语言。