vultr/vultr-php

官方Vultr API PHP包装器。

v1.0.2 2022-12-13 05:07 UTC

This package is auto-updated.

Last update: 2024-09-14 01:14:41 UTC


README

Vultr API PHP客户端。

GitHub license Vultr-Php Changelog PHP Version Require Latest Stable Version Latest Unstable Version Total Downloads PHP Tests Test Coverage Library Documentation

入门指南

必须有PSR7、PSR17和PSR18兼容的HTTP客户端。 查看所有PSR 此客户端将在这些接口上执行,这允许依赖注入。更多信息请参阅用法。 https://packagist.org.cn/providers/psr/http-client-implementation

安装

composer require vultr/vultr-php

用法

初始化客户端

一旦确定将使用哪种HTTP客户端实现来初始化客户端。如果您选择的客户端实现是一个广泛使用的客户端,它可能可以自动检测。这是因为此客户端使用了PHP-Http/Discovery

<?php

declare(strict_types=1);

require (__DIR__.'/../vendor/autoload.php');

$client = Vultr\VultrPhp\VultrClient::create('Your Lovely Vultr API Key');

上面的代码示例将尝试使用HTTP Discovery方法初始化客户端。如果您想自定义HTTP客户端,或者发现方法没有找到它,VultrClient将允许传递PSR18客户端以及PSR17 HTTP工厂。

<?php

declare(strict_types=1);

require (__DIR__.'/../vendor/autoload.php');

$http_factory = new GuzzleHttp\Psr7\HttpFactory();
$client = Vultr\VultrPhp\VultrClient::create('Heres my api key', new GuzzleHttp\Client(), $http_factory, $http_factory);

使用客户端

此客户端实现了当前版本2 API中所有的服务端点。可以在这里找到。

有关更详细的示例,请参阅示例文件夹。

分页

客户端使用链表在您的游标之间分页。每次列表调用都返回一个通过引用传递的ListOptions,您可以在随后的调用中对其进行操作,因此函数也会对其进行操作。这使得您可以选择之前和/或下一个游标链接进行导航。

<?php

declare(strict_types=1);

require(__DIR__.'/../vendor/autoload.php');

$client = Vultr\VultrPhp\VultrClient::create('Your Lovely Vultr API Key');

$options = new Vultr\VultrPhp\Util\ListOptions();
// Or
// $options = null;
/**
 * Whether you pass in a null $options or a ListOptions. You can always expect to have ListOptions be passed back out too you when calling the function.
 */
while (true)
{
	$instances = [];
	foreach ($client->instances->getInstances(null, $options) as $instance)
	{
		$instances[] = $instance;
	}

	// Exit our loop, we have reached the end. Hooray!
	if ($options->getNextCursor() == '')
	{
		break;
	}
	// Setting the "CurrentCursor" will tell the client which page it should transcode the url to make the request too.
	$options->setCurrentCursor($options->getNextCursor());
}

ModelOptions用法

ModelOptions是允许用户传递许多不属于Model对象的参数的对象。这些是针对客户端库中创建和更新函数的特定属性。这些对象的使用非常简单。想法是减少代码复杂性,同时提供在响应中删除属性时删除某些方法的灵活性。

以InstanceCreate为例。该对象包含许多属性,它们的名称都是下划线形式。然后使用这些属性名称生成对API的请求。

为了保持客户端库中驼峰式函数的一致性。ModelOptions使用PHP的__call魔法方法。要设置这些受保护的属性,可以使用with函数的变体:withYourLovelyPropName('hello_world')或set函数的变体:setYourLovelyPropName('hello_world')。

这些函数将设置用于生成发送到API的下划线_props请求的属性的值。

除了with和set类型函数之外,还有get函数也可以使用。它们的布局与with和set函数相同。

这些对象函数的示例用法。

declare(strict_types=1);

require(__DIR__.'/../vendor/autoload.php');

use Vultr\VultrPhp\Services\Instances\InstanceCreate;

$create = new InstanceCreate('ewr', 'vc2-6c-16gb');

$create->setOsId(6969);

$create = $create->withHostname('my-amazing-hostname');

var_dump($create->getOsId(), $create->getHostname());

异常用法

所有异常都是VultrException的子类。

异常树

  • VultrException
    • VultrClientException
    • VultrServiceException
      • AccountException
      • ApplicationException
      • BackupException
      • 裸金属异常
      • 计费异常
      • 块存储异常
      • DNS异常
      • 防火墙异常
      • 实例异常
      • ISO异常
      • Kubernetes异常
      • 负载均衡异常
      • 对象存储异常
      • 操作系统异常
      • 计划异常
      • 区域异常
      • 预留IP异常
      • 快照异常
      • SSH密钥异常
      • 启动脚本异常
      • 用户异常
      • VPC异常
<?php

declare(strict_types=1);

require (__DIR__.'/../vendor/autoload.php');

$client = Vultr\VultrPhp\VultrClient::create('Your Lovely Vultr API Key');

try
{
	$account = $client->account->getAccount();
}
catch (Vultr\VultrPhp\Services\Account\AccountException $e)
{
	exit('Just a little http error no biggy :wink: : '. $e->getMessage().PHP_EOL);
}
catch (Vultr\VultrPhp\VultrException $e)
{
	exit('O crap something really bad happen: '.$e->getMessage().PHP_EOL);
}

文档

有关API v2的详细信息,请参阅我们的文档:https://www.vultr.com/api

查看我们的代码覆盖率以获取详细信息:https://vultr.github.io/vultr-php/code-coverage/index.html

要查看特定库的文档,请参阅:https://vultr.github.io/vultr-php/docs/index.html

版本控制

本项目遵循SemVer进行版本控制。有关可用的版本,请参阅本存储库的标签稳定发布版

贡献

请随时向我们发送拉取请求!请参阅贡献指南

许可证

本项目采用MIT许可证 - 有关详细信息,请参阅LICENSE文件。