OVH API包装器

v2.0.2 2020-10-09 11:52 UTC

This package is auto-updated.

Last update: 2024-09-09 20:34:52 UTC


README

PHP Wrapper for OVH APIs

这个PHP包是一个轻量级的OVH API包装器。这是在PHP应用程序中使用OVH.com API的最简单方法。

Build Status HHVM Status

<?php
/**
 * # Instantiate. Visit https://api.ovh.com/createToken/index.cgi?GET=/me
 * to get your credentials
 */
require __DIR__ . '/vendor/autoload.php';
use \Ovh\Api;

$ovh = new Api( $applicationKey,
                $applicationSecret,
                $endpoint,
                $consumer_key);
echo "Welcome " . $ovh->get('/me')['firstname'];
?>

快速入门

要下载此包装器并将其集成到您的PHP应用程序中,您可以使用Composer

以下命令可快速集成

composer require ovh/ovh

或者,在您的composer.json文件中添加仓库,或者如果您还没有此文件,在项目的根目录中创建它,内容如下

{
    "name": "Example Application",
    "description": "This is an example of OVH APIs wrapper usage",
    "require": {
        "ovh/ovh": "dev-master"
    }
}

然后,您可以使用以下命令安装OVH API包装器和依赖项

php composer.phar install

这将把ovh/ovh安装到./vendor,以及包括autoload.php在内的其他依赖项。

OVH菜谱

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

如何作为用户登录?

为了与API通信,SDK在每个请求中都会使用一个令牌来识别用户。这个令牌被称为消费者密钥。要有一个经过验证的消费者密钥,您需要将您的用户重定向到特定的认证页面。一旦用户登录,令牌就会得到验证,用户将被重定向到$redirection URL。

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

session_start();

// Informations about your application
$applicationKey = "your_app_key";
$applicationSecret = "your_app_secret";
$redirection = "http://your_url.ovh";

// Information about API and rights asked
$endpoint = 'ovh-eu';
$rights = array( (object) [
    'method'    => 'GET',
    'path'      => '/me*'
]);

// Get credentials
$conn = new Api($applicationKey, $applicationSecret, $endpoint);
$credentials = $conn->requestCredentials($rights, $redirection);

// Save consumer key and redirect to authentication page
$_SESSION['consumer_key'] = $credentials["consumerKey"];
header('location: '. $credentials["validationUrl"]);
...
?>

如何使用OVH API在SBG1服务器上启用网络突发?

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

// Informations about your application
$applicationKey = "your_app_key";
$applicationSecret = "your_app_secret";
$consumer_key = "your_consumer_key";

// Information about API and rights asked
$endpoint = 'ovh-eu';

// Get servers list
$conn = new Api(    $applicationKey,
                    $applicationSecret,
                    $endpoint,
                    $consumer_key);
$servers = $conn->get('/dedicated/server/');

foreach ($servers as $server) {

    // Search servers inside SBG1
    $details = $conn->get('/dedicated/server/'. $server);
    if ($details['datacenter'] == 'sbg1') {

        // Activate burst on server
        $content = (object) array('status' => "active");
        $conn->put('/dedicated/server/'. $server . '/burst', $content);
        echo "We burst " . $server;
    }
}

?>

如何自定义HTTP客户端配置?

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

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

// Informations about your application
$applicationKey = "your_app_key";
$applicationSecret = "your_app_secret";
$consumer_key = "your_consumer_key";

// Information about API and rights asked
$endpoint = 'ovh-eu';

$client = new Client();
$client->setDefaultOption('timeout', 1);
$client->setDefaultOption('headers', array('User-Agent' => 'api_client') );

// Get servers list
$conn = new Api(    $applicationKey,
                    $applicationSecret,
                    $endpoint,
                    $consumer_key,
                    $client);
$webHosting = $conn->get('/hosting/web/');

foreach ($webHosting as $webHosting) {
        echo "One of our web hosting: " . $webHosting . "\n";
}
?>

如何打印API错误详情?

在内部,php-ovh默认使用GuzzlePHP 6来发出API请求。如果一切顺利,它将直接返回响应,如上面的示例所示。如果发生错误,如缺少端点或对象(404)、认证或授权错误(401或403)或参数错误,Guzzle将引发一个GuzzleHttp\Exception\ClientException异常。对于服务器端错误(5xx),它将引发一个GuzzleHttp\Exception\ServerException异常。

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

<?php
/**
 * # Instantiate. Visit https://api.ovh.com/createToken/index.cgi?GET=/me
 * to get your credentials
 */
require __DIR__ . '/vendor/autoload.php';
use \Ovh\Api;

$ovh = new Api( $applicationKey,
                $applicationSecret,
                $endpoint,
                $consumer_key);

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

如何构建文档?

文档基于phpdocumentor。要安装它和其他质量工具,您可以在克隆项目时安装本地npm项目

git clone https://github.com/ovh/php-ovh.git
cd php-ovh
php composer.phar install

要生成文档,可以直接使用

vendor/bin/phing phpdocs

文档位于docs/目录中。

如何运行测试?

测试基于phpunit。要安装它和其他质量工具,您可以在克隆项目时安装本地npm项目

git https://github.com/ovh/php-ovh.git
cd php-ovh
php composer.phar install

编辑phpunit.xml文件,使用您的凭据通过功能测试。然后,您可以直接使用phing运行单元测试和功能测试。

vendor/bin/phing test

要跳过功能测试并仅运行单元测试,可以使用only.units选项

vendor/bin/phing test -Donly.units=true

支持的API

OVH欧洲

OVH 美国

OVH 北美

So you Start 欧洲

So you Start 北美

Kimsufi 欧洲

Kimsufi 北美

Runabove

相关链接