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上獲取許可證的副本。
除非適用法律要求或以書面形式同意,否則根據許可證分配的軟件是按照“原樣”分發的,沒有任何明示或暗示的任何形式的保證或條件。請參閱許可證,以獲取執行許可條款和限制的特定語言。