Skip to content

denislam/laravel-forge-api-sdk-php

Repository files navigation

Laravel Forge API SDK for PHP

Automating tasks on Laravel Forge has never been so easy

The Laravel Forge API SDK for PHP makes it easy for developers to access Laravel Forge in their PHP code via Forge's official REST API, and build robust applications and software to automatically provision, manage, and deploy unlimited PHP applications on AWS, DigitalOcean, Linode, etc. You can get started in minutes by installing the SDK through Composer or by downloading a single zip or phar file from our latest release.

Installation

The Laravel Forge API SDK for PHP can be installed via Composer by requiring the guzzlehttp/guzzle package (see: GuzzleHTTP) and registering a PSR-4 autoloader for the Laravel_Forge_API_PHP_SDK namespace in your project's composer.json.

{
    "require": {
		"guzzlehttp/guzzle": "~6.0"
    },
	"autoload": {
		"psr-4": {
			"Laravel_Forge_API_PHP_SDK\\": "src/laravel-forge-api-php-sdk/"
	}
}

Then run a composer update:

composer update

Require the Composer Autoloader

In your code, require the Composer autoloader vendor/autoload.php.

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

Change the directory path to match your code's architecture if needed.

Set Your Forge API Token

Create a new API Token from your Forge account settings.

Then copy and paste your API token into a Forge::setToken() function and place this inside your code.

Forge::setToken('COPY AND PASTE YOUR FORGE API TOKEN HERE');

Usage

Here are a few examples of what you can do. Server responses are in a JSON string format.

List all your servers:

echo Servers::list();

Get information about a specific server:

echo Servers::get($server_id);

List all your sites on a specific server:

echo Sites::list($server_id);

Create a new server:

Servers::create($payload);

$payload = array(
  "provider"            => $provider,           // string
  "credential_id"       => $credential_id,      // boolean
  "region"              => $region,             // string
  "ip_address"          => $ip_address,         // string
  "private_ip_address"  => $private_ip_address, // string
  "php_version"         => $php_version,        // string
  "database"            => $database,           // string
  "maria"               => $maria,              // boolean
  "load_balancer"       => $load_balancer,      // boolean
  "network"             => $network             // array
);

Functions Quick Reference

Please visit our SDK API documentation for a complete reference of what classes and functions are available.

  • Servers
    • Servers::create($payload = [])
    • Servers::list()
    • Servers::get($server_id)
    • Servers::update($server_id)
    • Serverse::update_database_password($payload = [])
    • Servers::delete($server_id)
    • Servers::reboot($server_id)
    • Servers::revoke($server_id)
    • Servers::reconnect($server_id)
    • Servers::reactivate($server_id)
  • Services
    • Services::reboot_mysql($server_id)
      • Services::stop_mysql($server_id)
      • Services::reboot_nginx($server_id)
      • Services::stop_nginx($server_id)
      • Services::reboot_postgres($server_id)
      • Services::stop_postgres($server_id)
      • Services::install_blackfire($payload = [])
      • Services::install_papertrail($payload = [])
      • Services::remove_papertrail($payload = [])
  • Daemons
    • Daemons::create($server_id, $payload = [])
    • Daemons::list($server_id)
    • Daemons::get($server_id, $daemon_id)
    • Daemons::delete($server_id, $daemon_id)
    • Daemons::restart($server_id, $daemon_id)
  • Firewall Rules
    • FirewallRules::create($server_id, $payload = [])
    • FirewallRules::list($server_id)
    • FirewallRules::get($server_id, $rule_id)
    • FirewallRules::delete($server_id, $rule_id)
  • Scheduled Jobs
    • ScheduledJobs::create($server_id, $payload = [])
    • ScheduledJobs::list($server_id)
    • ScheduledJobs::get($server_id, $job_id)
    • ScheduledJobs::delete($server_id, $job_id)
  • MySQL Databases
    • MySQLDatabases::create($server_id, $payload = [])
    • MySQLDatabases::list($server_id)
    • MySQLDatabases::get($server_id, $database_id)
    • MySQLDatabases::delete($server_id, $database_id)
  • MySQL Database Users
    • MySQLDatabaseUsers::create($server_id, $payload = [])
    • MySQLDatabaseUsers::list($server_id)
    • MySQLDatabaseUsers::get($server_id, $user_id)
    • MySQLDatabaseUsers::update($server_id, $user_id, $payload = [])
    • MySQLDatabaseUsers::delete($server_id, $user_id)
  • Sites
    • Sites::create($server_id, $payload = [])
    • Sites::list($server_id)
    • Sites::get($server_id, $site_id)
    • Sites::update($server_id, $site_id, $payload = [])
    • Sites::delete($server_id, $site_id)
    • Sites::balancing($server_id, $site_id, $payload = [])
  • SSL Certificates
    • SSLCertificates::create($server_id, $site_id, $payload = [])
    • SSLCertificates::install_existing($server_id, $site_id, $payload = [])
    • SSLCertificates::clone($server_id, $site_id, $payload = [])
    • SSLCertificates::letsencrypt($server_id, $site_id, $payload = [])
    • SSLCertificates::list($server_id, $site_id)
    • SSLCertificates::get($server_id, $site_id, $ssl_id)
    • SSLCertificates::install($server_id, $site_id, $ssl_id, $payload = [])
    • SSLCertificates::activate($server_id, $site_id, $ssl_id)
    • SSLCertificates::delete($server_id, $site_id, $ssl_id)
  • SSH Keys
    • SSHKeys::create($server_id, $payload = [])
    • SSHKeys::list($server_id)
    • SSHKeys::get($server_id, $key_id)
    • SSHKeys::delete($server_id, $key_id)
  • Workers
    • Workers::create($server_id, $site_id, $payload = [])
    • Workers::list($server_id, $site_id)
    • Workers::get($server_id, $site_id, $worker_id)
    • Workers::delete($server_id, $site_id, $worker_id)
    • Workers::restart($server_id, $site_id, $worker_id)
  • Deployment
    • Deployment::enable($server_id, $site_id)
    • Deployment::disable($server_id, $site_id)
    • Deployment::get_script($server_id, $site_id)
    • Deployment::update($server_id, $site_id, $payload = [])
    • Deployment::deploy($server_id, $site_id)
    • Deployment::reset($server_id, $site_id)
    • Deployment::get_log($server_id, $site_id)
  • Configuration Files
    • ConfigurationFiles::get_nginx($server_id, $site_id)
    • ConfigurationFiles::update_nginx($server_id, $site_id, $payload = [])
    • ConfigurationFiles::get_env($server_id, $site_id)
    • ConfigurationFiles::update_env($server_id, $site_id, $payload = [])
  • git Projects
    • GitProjects::install($server_id, $site_id, $payload = [])
    • GitProjects::remove($server_id, $site_id)
  • WordPress
    • WordPress::install($server_id, $site_id, $payload = [])
    • WordPress::uninstall($server_id, $site_id)
  • Recipes
    • Recipes::create($payload = [])
    • Recipes::list()
    • Recipes::get($recipe_id)
    • Recipes::get($recipe_id, $payload = [])
    • Recipes::delete($recipe_id)
    • Recipes::run($recipe_id, $payload = [])
  • Credentials
    • Credentials::list()

Complete Documentation

Check out ../public/docs/index.php for a complete reference of the SDK's available API functions.

Live Demo

Check out ../public/docs/demo.php for a live demo of more usage examples.

Code Sandbox

Use the framework set up in ../public/index.php to test your code.

Bonus Features

Multiple HTTP Client Support

The SDK includes handlers for two of the most powerful PHP HTTP clients:

By default, the Guzzle client is used to handle all Forge API requests.

Instant Dynamic HTTP Client Switching

If you desire to add other clients or change between clients, you can do so in a matter of seconds:

  1. Open Forge.php
  2. Change const PHP_HTTP_CLIENT on line 35 to the client you want.
  3. Save the file and you're done! The SDK will automatically use the HTTP client class located within ../HttpHandlers/ to make API requests.

Custom Exceptions Handling

The SDK makes it easy to create and trigger custom exceptions in your code. Here's all you have to do:

  1. Open ../Exceptions/Errors.php.
  2. Enter your custom error reference ID and message into the errors($id) function array. The message ID may contain spaces and numbers.
$data = [
  //..
  'Custom Error ID 1' => ['context'  => 'Your custom error message.' ],
  'custom_error_id_2' => ['context'  => 'Another custom error message.' ],
  //..
];
  1. To return a custom error message in your code, simply use the get() function of the Errors base class like this:
return Errors::get('Custom Error ID 1');
  1. That's it!

Am I Missing An Essential Feature?

  • Nothing is impossible!
  • Open an issue and let's make the SDK better together!
  • Bug reports, feature requests, fixes, and well-wishes are always welcome.
  • Want to say thanks or buy me a beer? Donations are welcome. ☺

Contributing

  1. Create an issue and describe your idea.
  2. Fork it
  3. Create your feature branch (git checkout -b my-new-feature)
  4. Commit your changes (git commit -am 'Add some feature')
  5. Publish the branch (git push origin my-new-feature)
  6. Create a new Pull Request

To test your workflow, you can use my test repo.

License

Laravel Forge API SDK for PHP is released under the MIT License

Donate

Buy me a few beers if you feel this SDK saved you a few jars of sweat or put a smile on your face. Cheers! ☺

Bitcoin

You can send Bitcoin donations to 1BM3kt5UKe4v6Rb8sQBXA9pnFgJdMGCwWq or via Coinbase by clicking on the button below.

coinbase

PayPal

Credit cards are also accepted here:

PayPal

Flattr

flattr

About

Laravel Forge API SDK for PHP

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published