https://github.com/vultr/vultr-php
The Official Vultr API PHP Wrapper
https://github.com/vultr/vultr-php
http-client php psr-17 psr-18 psr-7 version2 vultr vultr-api vultr-api-wrapper vultr-php
Last synced: about 1 month ago
JSON representation
The Official Vultr API PHP Wrapper
- Host: GitHub
- URL: https://github.com/vultr/vultr-php
- Owner: vultr
- License: mit
- Created: 2022-01-30T01:01:07.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-02-13T23:49:08.000Z (over 1 year ago)
- Last Synced: 2025-05-01T02:56:19.819Z (about 1 month ago)
- Topics: http-client, php, psr-17, psr-18, psr-7, version2, vultr, vultr-api, vultr-api-wrapper, vultr-php
- Language: PHP
- Homepage: https://www.vultr.com
- Size: 3.29 MB
- Stars: 21
- Watchers: 4
- Forks: 7
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# Vultr API PHP Client.
[](https://raw.githubusercontent.com/vultr/vultr-php/main/LICENSE)
[](https://github.com/vultr/vultr-php/blob/main/CHANGELOG.md)
[](https://packagist.org/packages/vultr/vultr-php)
[](https://packagist.org/packages/vultr/vultr-php)
[](https://packagist.org/packages/vultr/vultr-php)
[](https://packagist.org/packages/vultr/vultr-php)
[](https://github.com/vultr/vultr-php/actions/workflows/php.yml)
[](https://vultr.github.io/vultr-php/code-coverage/index.html)
[](https://vultr.github.io/vultr-php/docs/index.html)## Getting Started
Must have a PSR7, PSR17, and PSR18 Compatible HTTP Client.
[View all PSR's](https://www.php-fig.org/psr/)
This client will act on those interfaces which allow for dependancy injection. See Usage for more info.
https://packagist.org/providers/psr/http-client-implementation### Installation
```sh
composer require vultr/vultr-php
```### Usage
#### Initializing the client
Once decided on what HTTP client implementation that will be used to initiate the client. If the client implementation you chose is a wider used client, it may be possible to be auto detected. This is because this client uses [PHP-Http/Discovery](https://github.com/php-http/discovery).
```php
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 Usage
ModelOptions are objects that allow the user to pass in many arguments that don't neccessarily belong to a Model object. These are attributes that are specific to creation and update functions throughout the client library. Usage of these objects are quite simple. The idea was to reduce code complexity but also give the flexibility to deprecate certain methods when/if attributes are removed from responses.Lets take InstanceCreate for example. This object has many properties in it, that are all underscore_cased. These property names are than used to generate a request to the api.
To keep the uniformity between the camelCased functions in this client library. ModelOptions makes use of php's `__call` magic method. In order to set these protected properties you can use variation of with functions example: withYourLovelyPropName('hello_world') or set functions example: setYourLovelyPropName('hello_world').
These functions will set your attributes that will be used to generate the request of our underscored_props that will be sent to the api.
With the addition of with and set type functions. There are also get functions that can be used as well. They follow the same camcelCased layout as the with and set functions.
Example usage of these object functions.
```php
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());
```
#### Exception Usage
All exceptions are children of VultrException.
Exception tree
* VultrException
* VultrClientException
* VultrServiceException
* AccountException
* ApplicationException
* BackupException
* BareMetalException
* BillingException
* BlockStorageException
* DNSException
* FirewallException
* InstanceException
* ISOException
* KubernetesException
* LoadBalancerException
* ObjectStorageException
* OperatingSystemException
* PlanException
* RegionException
* ReservedIPException
* SnapshotException
* SSHKeyException
* StartupScriptException
* UserException
* VPCException```php
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);
}
```## Documentation
See our documentation for [detailed information about API v2](https://www.vultr.com/api).
View our code-coverage for a detailed look https://vultr.github.io/vultr-php/code-coverage/index.html
To view the specific library documentation please view https://vultr.github.io/vultr-php/docs/index.html
## Versioning
This project follows [SemVer](https://semver.org/) for versioning. For the versions available, [see the tags on this repository](https://github.com/vultr/vultr-php/tags) or for [stable releases](https://github.com/vultr/vultr-php/releases)
## Contribute
Feel free to send pull requests our way! Please see the [contributing guidelines](CONTRIBUTING.md).
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.