carsso/ovhcloud

此包已被弃用,不再维护。未建议替代包。

OVHcloud API包装器

dev-main 2023-03-06 17:38 UTC

This package is auto-updated.

Last update: 2023-11-06 18:58:24 UTC


README

Source Code Build Status Codecov Code Coverage Total Downloads

这是一个轻量级的PHP包,用于包装OVHcloud API。

在PHP应用程序中使用OVHcloud API的最简单方法。

兼容PHP 7.4, 8.0, 8.1, 8.2。

这是原始OVHcloud项目https://github.com/ovh/php-ovh的分支。

安装

使用Composer安装此包装器并将其集成到您的PHP应用程序中

composer require carsso/ovhcloud

基本用法

<?php
require __DIR__ . '/vendor/autoload.php';
use \Ovh\Api;

// Api credentials can be retreived from the urls specified in the "Supported endpoints" section below.
$ovh = new Api($applicationKey,
                $applicationSecret,
                $endpoint,
                $consumerKey);
echo 'Welcome '.$ovh->get('/me')['firstname'];

高级用法

处理异常

在内部,php-ovhcloud默认使用Guzzle来发出API请求。

如果一切顺利,它将直接返回响应,如上例所示。

如果出现错误,如缺少端点或对象(404)、认证或授权错误(401或403)或参数错误,Guzzle将引发一个GuzzleHttp\Exception\ClientException异常。对于服务器端错误(5xx),它将引发一个GuzzleHttp\Exception\ServerException异常。

您可以使用类似以下代码获取错误详情

try {
    echo "Welcome " . $ovh->get('/me')['firstname'];
} catch (GuzzleHttp\Exception\ClientException $e) {
    $response = $e->getResponse();
    $responseBodyAsString = $response->getBody()->getContents();
    echo $responseBodyAsString;
}

自定义HTTP客户端配置

您可以注入具有特定配置的自定义HTTP客户端。例如,您可以编辑所有请求的用户代理和超时。

<?php
require __DIR__ . '/vendor/autoload.php';
use \Ovh\Api;
use GuzzleHttp\Client;

// Instantiate a custom Guzzle HTTP client and tweak it
$client = new Client();
$client->setDefaultOption('timeout', 1);
$client->setDefaultOption('headers', ['User-Agent' => 'api_client']);

// Api credentials can be retreived from the urls specified in the "Supported endpoints" section below.
// Inject the custom HTTP client as the 5th argument of the constructor
$ovh = new Api($applicationKey,
                $applicationSecret,
                $endpoint,
                $consumerKey,
                $client);

echo 'Welcome '.$ovh->get('/me')['firstname'];

授权流程

此流程将允许您从OVHcloud账户所有者请求consumerKey。在允许访问他的账户后,他将被重定向到您的应用程序。

有关授权流程的更多信息,请参阅下方的“OVHcloud API认证”部分。

use \Ovh\Api;
session_start();

// Api credentials can be retreived from the urls specified in the "Supported endpoints" section below.
$ovh = new Api($applicationKey,
                $applicationSecret,
                $endpoint);

// Specify the list of API routes you want to request
$rights = [
    [ 'method' => 'GET',  'path' => '/me*' ],
];

// After allowing your application access, the customer will be redirected to this URL.
$redirectUrl = 'https://your_application_redirect_url'

$credentials = $conn->requestCredentials($rights, $redirectUrl);

// Save consumer key and redirect to authentication page
$_SESSION['consumerKey'] = $credentials['consumerKey'];
header('location: '. $credentials['validationUrl']);
// After successful redirect, the consumerKey in the session will be activated and you will be able to use it to make API requests like in the "Basic usage" section above.

代码示例:在GRA1专用服务器上启用网络突发

以下是如何使用包装器在GRA1专用服务器上启用网络突发的更复杂示例。

<?php
require __DIR__ . '/vendor/autoload.php';
use \Ovh\Api;

// Api credentials can be retreived from the urls specified in the "Supported endpoints" section below.
$ovh = new Api($applicationKey,
                $applicationSecret,
                $endpoint,
                $consumerKey);

// Load the list of dedicated servers
$servers = $conn->get('/dedicated/server/');
foreach ($servers as $server) {
    // Load the server details
    $details = $conn->get('/dedicated/server/'.$server);
    // Filter servers only inside GRA1
    if ($details['datacenter'] == 'gra1') {
        // Activate burst on server
        $content = ['status' => 'active'];
        $conn->put('/dedicated/server/'.$server.'/burst', $content);
        echo 'Burst enabled on '.$server;
    }
}

更多代码示例

您想使用OVH API吗?也许您想要的脚本已经在本存储库的示例部分中编写了!

OVHcloud API认证

要使用OVHcloud API,您需要三个凭证

  • 应用程序密钥
  • 应用程序密钥
  • 消费者密钥

应用程序密钥和密钥不授予对特定账户的访问权限,并且对您的应用程序是唯一的。消费者密钥用于授予对特定OVHcloud账户的访问权限到指定的应用程序。

如果您的应用程序打算供多个账户使用,则可以单独创建它们(您的应用程序需要实现授权流程)。在授权流程中,客户将被提示允许您的应用程序访问他的账户,然后他将重定向到您的应用程序。

如果您的应用程序打算仅使用您自己的OVHcloud账户,则也可以一起创建。

支持的端点

OVHcloud欧洲

OVHcloud US

OVHcloud 北美/加拿大

So you Start 欧洲

So you Start 北美

Kimsufi 欧洲

Kimsufi 北美

构建文档

文档基于phpdocumentor并包含在项目中。要生成文档,可以直接使用

composer phpdoc

文档位于docs/目录中。

代码检查 / Linting

代码检查基于PHP CodeSniffer并包含在项目中。要检查代码,可以直接使用

composer phpcs

代码linting基于PHP Code Beautifier和Fixer并包含在项目中。要lint代码,可以直接使用

composer phpcbf

测试

测试基于phpunit并包含在项目中。要运行功能测试,您需要提供有效的API凭据,这些凭据可以通过环境提供

APP_KEY=xxx APP_SECRET=xxx CONSUMER=xxx ENDPOINT=xxx composer phpunit

贡献

请参阅CONTRIBUTING以获取详细信息。

致谢

许可证

(修改后)BSD许可证。请参阅LICENSE以获取更多信息。