X
    Categories: API Tutorials and Use Cases

Introducing Cloudways API PHP-SDK Built By Our Community of Clients

Application Programming Interface or API has become the latest “in” thing in the development world. Regardless of the language or the platform, APIs are used to decrease development time and headaches.

Almost every major platform and service providers now offers a set of APIs to accelerate development time for apps based on the platforms. As an example, take the case of Google. Almost every product in Google ecosystem has its own API that offers seamless integration of a particular service in a third-party product.

Cloudways released API for its platform some time ago. This API allows developers to create apps upon the robust Cloudways platform. In a comprehensive series on Cloudways API, I have covered several application scenarios such as  creating a Slack Chat Bot with Cloudways API, connecting CloudwaysBot with Asana to create tasks, getting Alerts on SMS using Twilio, and sending CloudwaysBot notification to Trello. Refer to the complete list for more topics.

A common feature of these posts is the use of Guzzle and CURL for connecting to the Cloudways API. For some users, the use of these components offers a sizeable challenge. To make things easier for everyone, a client of Cloudways has built a PHP-SDK for Cloudways API. Developers only need to install the PHP-SDK via composer, clone it from bitbucket or download the repository and then connect with Cloudways API.

Where to Get it?

The community powered Cloudways API PHP-SDK is available on Bitbucket under MIT license. If you would like to contribute to the project, feel free to fork it.

Disclaimer: Cloudways doesn’t officially maintain this SDK, it is built and maintained by our clients.

Installing Cloudways API PHP-SDK

Installing PHP-SDK is very straightforward:

  • Create a new file in the root directory and name it composer.json. Paste the following code in it.
    {
      "require": {
        "cloudwaysapi/phpsdk": "1.0.0.x-dev"
      }
    }

    Next, run the following command in your terminal.

    composer install


    Alternatively, you can directly install it via terminal using the following command

    composer require "cloudwaysapi/phpsdk:1.0.0.x-dev"

  • The second method is to clone it using Git.
    git clone https://bitbucket.org/jeremeycloud/cloudwaysapisdk.git


    Once it is installed, move to Cloudways API SDK directory and run composer install so that all required dependencies are installed.

Now that PHP-SDK is installed on the server, I will now use it to perform several server operations.

Working With Cloudways API PHP-SDK

I will start with getting the list of all cloud providers using Cloudways API PHP-SDK.

Create a new file in the root directory and name it index.php. Now, paste the following code in it.

<?php
require "vendor/autoload.php";
use Cloudways\Lists\Lists;
$lists = new Lists();
$lists->SetEmail("Your Email");
$lists->SetKey("Your Cloudways API key");
echo “<pre>”;
print_r($lists->getCloudProviders());
echo “<pre>”;
?>

I will now explain the above code step by step.

First, I included composer autoload and then the Cloudways Lists namespace.

require "vendor/autoload.php";

use Cloudways\Lists\Lists;

Next, I created the object Lists, and set up email and the API key. Once it is set, I called `getCloudProviders()` to get the list of all the Cloudways cloud providers. Once the code is executed, the output would be:

This is just a very brief introduction of the Cloudways PHP-SDK. The SDK could be used to setup all projects based on Cloudways API.

Conclusion

Using PHP-SDK, you can connect with all the services mentioned in Cloudways API documentation. If you discover a bug, feel free to tell us or fork it to remove the bug and become a contributor! You are also welcome to discuss the SDK in the comments below.

Ahmed Khan: Ahmed was a PHP community expert at Cloudways - A Managed PHP Hosting Cloud Platform. He is a software engineer with extensive knowledge in PHP and SEO. He loves watching Game of Thrones is his free time. Follow Ahmed on Twitter to stay updated with his works. You can email him at ahmed.khan@cloudways.com