2022-08-03 20:28:35 +02:00
# OVHcloud APIs lightweight PHP wrapper
2014-08-22 18:22:16 +02:00
[![PHP Wrapper for OVH APIs ](https://github.com/ovh/php-ovh/blob/master/img/logo.png )](https://packagist.org/packages/ovh/ovh)
2022-08-03 20:28:35 +02:00
[![Source Code ](https://img.shields.io/badge/source-ovh/php--ovh-blue.svg?style=flat-square )](https://github.com/ovh/php-ovh)
[![Build Status ](https://img.shields.io/github/actions/workflow/status/ovh/php-ovh/ci.yaml?label=CI&logo=github&style=flat-square )](https://github.com/ovh/php-ovh/actions?query=workflow%3ACI)
[![Codecov Code Coverage ](https://img.shields.io/codecov/c/gh/ovh/php-ovh?label=codecov&logo=codecov&style=flat-square )](https://codecov.io/gh/ovh/php-ovh)
[![Total Downloads ](https://img.shields.io/packagist/dt/ovh/ovh.svg?style=flat-square )](https://packagist.org/packages/ovh/ovh)
This PHP package is a lightweight wrapper for OVHcloud APIs.
The easiest way to use OVHcloud APIs in your PHP applications.
2014-08-22 18:22:16 +02:00
2022-08-03 20:28:35 +02:00
Compatible with PHP 7.4, 8.0, 8.1, 8.2.
## Installation
2023-05-26 17:56:21 +02:00
Install this wrapper and integrate it inside your PHP application with [Composer ](https://getcomposer.org ):
2022-08-03 20:28:35 +02:00
composer require ovh/ovh
## Basic usage
2014-10-09 18:37:55 +02:00
2014-08-22 18:22:16 +02:00
```php
< ?php
require __DIR__ . '/vendor/autoload.php';
use \Ovh\Api;
2023-05-26 16:37:10 +02:00
// Api credentials can be retrieved from the urls specified in the "Supported endpoints" section below.
2022-08-03 20:28:35 +02:00
$ovh = new Api($applicationKey,
2014-08-22 18:22:16 +02:00
$applicationSecret,
2016-02-03 10:26:24 +01:00
$endpoint,
2022-08-03 20:28:35 +02:00
$consumerKey);
echo 'Welcome '.$ovh->get('/me')['firstname'];
2014-08-22 18:22:16 +02:00
```
2022-08-03 20:28:35 +02:00
## Advanced usage
2014-08-22 18:22:16 +02:00
2022-08-03 20:28:35 +02:00
### Handle exceptions
2014-08-22 18:22:16 +02:00
2022-08-03 20:28:35 +02:00
Under the hood, ```php-ovh``` uses [Guzzle ](http://docs.guzzlephp.org/en/latest/quickstart.html ) by default to issue API requests.
2016-11-15 16:24:34 +01:00
2022-08-03 20:28:35 +02:00
If everything goes well, it will return the response directly as shown in the examples above.
2016-11-15 16:24:34 +01:00
2022-08-03 20:28:35 +02:00
If there is an error like a missing endpoint or object (404), an authentication or authorization error (401 or 403) or a parameter error, the Guzzle will raise a ``GuzzleHttp\Exception\ClientException`` exception. For server-side errors (5xx), it will raise a ``GuzzleHttp\Exception\ServerException`` exception.
2014-08-22 18:22:16 +02:00
2022-08-03 20:28:35 +02:00
You can get the error details with a code like:
2014-08-22 18:22:16 +02:00
2022-08-03 20:28:35 +02:00
```php
try {
echo "Welcome " . $ovh->get('/me')['firstname'];
} catch (GuzzleHttp\Exception\ClientException $e) {
$response = $e->getResponse();
$responseBodyAsString = $response->getBody()->getContents();
echo $responseBodyAsString;
}
2014-08-22 18:22:16 +02:00
```
2022-08-03 20:28:35 +02:00
### Customize HTTP client configuration
2014-08-22 18:22:16 +02:00
2022-08-03 20:28:35 +02:00
You can inject your own HTTP client with your specific configuration. For instance, you can edit user-agent and timeout for all your requests
2014-08-22 18:22:16 +02:00
2022-08-03 20:28:35 +02:00
```php
< ?php
require __DIR__ . '/vendor/autoload.php';
use \Ovh\Api;
use GuzzleHttp\Client;
2014-08-22 18:22:16 +02:00
2022-08-03 20:28:35 +02:00
// Instantiate a custom Guzzle HTTP client and tweak it
$client = new Client();
$client->setDefaultOption('timeout', 1);
$client->setDefaultOption('headers', ['User-Agent' => 'api_client']);
2016-01-18 17:47:23 +01:00
2023-05-26 16:37:10 +02:00
// Api credentials can be retrieved from the urls specified in the "Supported endpoints" section below.
2022-08-03 20:28:35 +02:00
// Inject the custom HTTP client as the 5th argument of the constructor
$ovh = new Api($applicationKey,
$applicationSecret,
$endpoint,
$consumerKey,
$client);
2016-01-18 17:47:23 +01:00
2022-08-03 20:28:35 +02:00
echo 'Welcome '.$ovh->get('/me')['firstname'];
```
### Authorization flow
This flow will allow you to request consumerKey from an OVHcloud account owner.
After allowing access to his account, he will be redirected to your application.
See "OVHcloud API authentication" section below for more information about the authorization flow.
2014-08-22 18:22:16 +02:00
```php
use \Ovh\Api;
session_start();
2023-05-26 16:37:10 +02:00
// Api credentials can be retrieved from the urls specified in the "Supported endpoints" section below.
2022-08-03 20:28:35 +02:00
$ovh = new Api($applicationKey,
$applicationSecret,
$endpoint);
2014-08-22 18:22:16 +02:00
2022-08-03 20:28:35 +02:00
// Specify the list of API routes you want to request
$rights = [
[ 'method' => 'GET', 'path' => '/me*' ],
];
2014-08-22 18:22:16 +02:00
2022-08-03 20:28:35 +02:00
// After allowing your application access, the customer will be redirected to this URL.
$redirectUrl = 'https://your_application_redirect_url'
$credentials = $conn->requestCredentials($rights, $redirectUrl);
2014-08-22 18:22:16 +02:00
// Save consumer key and redirect to authentication page
2022-08-03 20:28:35 +02:00
$_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.
2014-08-22 18:22:16 +02:00
```
2023-05-26 17:56:21 +02:00
### Code sample: Enable network burst on GRA1 dedicated servers
2022-08-03 20:28:35 +02:00
Here is a more complex example of how to use the wrapper to enable network burst on GRA1 dedicated servers.
2014-08-22 18:22:16 +02:00
```php
< ?php
require __DIR__ . '/vendor/autoload.php';
use \Ovh\Api;
2023-05-26 16:37:10 +02:00
// Api credentials can be retrieved from the urls specified in the "Supported endpoints" section below.
2022-08-03 20:28:35 +02:00
$ovh = new Api($applicationKey,
$applicationSecret,
$endpoint,
$consumerKey);
2014-08-22 18:22:16 +02:00
2022-08-03 20:28:35 +02:00
// Load the list of dedicated servers
2014-08-22 18:22:16 +02:00
$servers = $conn->get('/dedicated/server/');
foreach ($servers as $server) {
2022-08-03 20:28:35 +02:00
// Load the server details
$details = $conn->get('/dedicated/server/'.$server);
// Filter servers only inside GRA1
if ($details['datacenter'] == 'gra1') {
2014-08-22 18:22:16 +02:00
// Activate burst on server
2022-08-03 20:28:35 +02:00
$content = ['status' => 'active'];
$conn->put('/dedicated/server/'.$server.'/burst', $content);
echo 'Burst enabled on '.$server;
2014-08-22 18:22:16 +02:00
}
}
```
2015-12-15 18:16:11 +01:00
2022-08-03 20:28:35 +02:00
### More code samples
2015-12-15 18:16:11 +01:00
2022-08-03 20:28:35 +02:00
Do you want to use OVH APIs? Maybe the script you want is already written in the [example part ](examples/README.md ) of this repository!
2015-12-15 18:16:11 +01:00
2022-08-03 20:28:35 +02:00
## OVHcloud API authentication
2015-12-15 18:16:11 +01:00
2023-05-26 17:56:21 +02:00
To use the OVHcloud APIs you need three credentials:
2015-12-15 18:16:11 +01:00
2022-08-03 20:28:35 +02:00
* An application key
* An application secret
* A consumer key
2014-08-22 18:22:16 +02:00
2022-08-03 20:28:35 +02:00
The application key and secret are not granting access to a specific account and are unique to identify your application.
The consumer key is used to grant access to a specific OVHcloud account to a specified application.
2016-06-27 10:24:47 +02:00
2022-08-03 20:28:35 +02:00
They can be created separately if your application is intended to be used by multiple accounts (your app will need to implement an authorization flow).
In the authorization flow, the customer will be prompted to allow access to his account to your application, then he will be redirected to your application.
2016-06-27 10:24:47 +02:00
2022-08-03 20:28:35 +02:00
They can also be created together if your application is intended to use only your own OVHcloud account.
2016-06-27 10:24:47 +02:00
2022-08-03 20:28:35 +02:00
## Supported endpoints
2016-06-27 10:24:47 +02:00
2022-08-03 20:28:35 +02:00
### OVHcloud Europe
2016-06-27 10:24:47 +02:00
2022-08-03 20:28:35 +02:00
* ```$endpoint = 'ovh-eu';```
* Documentation: < https: // eu . api . ovh . com />
* Console: < https: // eu . api . ovh . com / console >
* Create application credentials (generate only application credentials, your app will need to implement an authorization flow): < https: // eu . api . ovh . com / createApp />
* Create account credentials (all keys at once for your own account only): < https: // eu . api . ovh . com / createToken />
* Community support: api-subscribe@ml.ovh.net
2014-08-22 18:22:16 +02:00
2022-08-03 20:28:35 +02:00
### OVHcloud US
2014-08-22 18:22:16 +02:00
2022-08-03 20:28:35 +02:00
* ```$endpoint = 'ovh-us';```
* Documentation: < https: // api . us . ovhcloud . com />
* Console: < https: // api . us . ovhcloud . com / console >
* Create application credentials (generate only application credentials, your app will need to implement an authorization flow): < https: // api . us . ovhcloud . com / createApp />
* Create account credentials (all keys at once for your own account only): < https: // api . us . ovhcloud . com / createToken />
2014-08-22 18:22:16 +02:00
2022-08-03 20:28:35 +02:00
### OVHcloud North America / Canada
2014-08-22 18:22:16 +02:00
2022-08-03 20:28:35 +02:00
* ```$endpoint = 'ovh-ca';```
* Documentation: < https: // ca . api . ovh . com />
* Console: < https: // ca . api . ovh . com / console >
* Create application credentials (generate only application credentials, your app will need to implement an authorization flow): < https: // ca . api . ovh . com / createApp />
* Create account credentials (all keys at once for your own account only): < https: // ca . api . ovh . com / createToken />
* Community support: api-subscribe@ml.ovh.net
2014-08-22 18:22:16 +02:00
2022-08-03 20:28:35 +02:00
### So you Start Europe
2014-08-22 18:22:16 +02:00
2022-08-03 20:28:35 +02:00
* ```$endpoint = 'soyoustart-eu';```
* Documentation: < https: // eu . api . soyoustart . com />
* Console: < https: // eu . api . soyoustart . com / console />
* Create application credentials (generate only application credentials, your app will need to implement an authorization flow): < https: // eu . api . soyoustart . com / createApp />
* Create account credentials (all keys at once for your own account only): < https: // eu . api . soyoustart . com / createToken />
* Community support: api-subscribe@ml.ovh.net
2014-08-22 18:22:16 +02:00
2022-08-03 20:28:35 +02:00
### So you Start North America
2014-08-22 18:22:16 +02:00
2022-08-03 20:28:35 +02:00
* ```$endpoint = 'soyoustart-ca';```
* Documentation: < https: // ca . api . soyoustart . com />
* Console: < https: // ca . api . soyoustart . com / console />
* Create application credentials (generate only application credentials, your app will need to implement an authorization flow): < https: // ca . api . soyoustart . com / createApp />
* Create account credentials (all keys at once for your own account only): < https: // ca . api . soyoustart . com / createToken />
* Community support: api-subscribe@ml.ovh.net
2014-08-22 18:22:16 +02:00
2022-08-03 20:28:35 +02:00
### Kimsufi Europe
2014-08-22 18:22:16 +02:00
2022-08-03 20:28:35 +02:00
* ```$endpoint = 'kimsufi-eu';```
* Documentation: < https: // eu . api . kimsufi . com />
* Console: < https: // eu . api . kimsufi . com / console />
* Create application credentials (generate only application credentials, your app will need to implement an authorization flow): < https: // eu . api . kimsufi . com / createApp />
* Create account credentials (all keys at once for your own account only): < https: // eu . api . kimsufi . com / createToken />
* Community support: api-subscribe@ml.ovh.net
2014-08-22 18:22:16 +02:00
2022-08-03 20:28:35 +02:00
### Kimsufi North America
2014-08-22 18:22:16 +02:00
2022-08-03 20:28:35 +02:00
* ```$endpoint = 'kimsufi-ca';```
* Documentation: < https: // ca . api . kimsufi . com />
* Console: < https: // ca . api . kimsufi . com / console />
* Create application credentials (generate only application credentials, your app will need to implement an authorization flow): < https: // ca . api . kimsufi . com / createApp />
* Create account credentials (all keys at once for your own account only): < https: // ca . api . kimsufi . com / createToken />
* Community support: api-subscribe@ml.ovh.net
2014-08-22 18:22:16 +02:00
2022-08-03 20:28:35 +02:00
## Building documentation
2014-08-22 18:22:16 +02:00
2022-08-03 20:28:35 +02:00
Documentation is based on phpdocumentor and inclued in the project.
To generate documentation, it's possible to use directly:
2018-06-12 10:29:29 +02:00
2022-08-03 20:28:35 +02:00
composer phpdoc
2018-06-12 10:29:29 +02:00
2022-08-03 20:28:35 +02:00
Documentation is available in docs/ directory.
2014-08-22 18:22:16 +02:00
2022-08-03 20:28:35 +02:00
## Code check / Linting
2014-08-22 18:22:16 +02:00
2022-08-03 20:28:35 +02:00
Code check is based on PHP CodeSniffer and inclued in the project.
To check code, it's possible to use directly:
2014-12-05 18:07:27 +01:00
2022-08-03 20:28:35 +02:00
composer phpcs
2014-12-05 18:07:27 +01:00
2022-08-03 20:28:35 +02:00
Code linting is based on PHP Code Beautifier and Fixer and inclued in the project.
To lint code, it's possible to use directly:
2014-12-05 18:07:27 +01:00
2022-08-03 20:28:35 +02:00
composer phpcbf
2014-12-05 18:07:27 +01:00
2022-08-03 20:28:35 +02:00
## Testing
2014-12-05 18:07:27 +01:00
2022-08-03 20:28:35 +02:00
Tests are based on phpunit and inclued in the project.
To run functionals tests, you need to provide valid API credentials, that you can provide them via environment:
2014-12-05 18:07:27 +01:00
2022-08-03 20:28:35 +02:00
APP_KEY=xxx APP_SECRET=xxx CONSUMER=xxx ENDPOINT=xxx composer phpunit
2014-12-05 18:07:27 +01:00
2022-08-03 20:28:35 +02:00
## Contributing
2014-12-05 18:07:27 +01:00
2022-08-03 20:28:35 +02:00
Please see [CONTRIBUTING ](https://github.com/ovh/php-ovh/blob/master/CONTRIBUTING.rst ) for details.
2014-08-22 18:22:16 +02:00
2022-08-03 20:28:35 +02:00
## Credits
2014-08-22 18:22:16 +02:00
2022-08-03 20:28:35 +02:00
[All Contributors from this repo ](https://github.com/ovh/php-ovh/contributors )
2014-08-22 18:22:16 +02:00
2022-08-03 20:28:35 +02:00
## License
2014-08-22 18:22:16 +02:00
2022-08-03 20:28:35 +02:00
(Modified) BSD license. Please see [LICENSE ](https://github.com/ovh/php-ovh/blob/master/LICENSE ) for more information.