PHP code example of fortytwo-studio / webfaction-php

1. Go to this page and download the library: Download fortytwo-studio/webfaction-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

fortytwo-studio / webfaction-php example snippets



    use FortyTwoStudio\WebFactionPHP\WebFactionClient;
    use FortyTwoStudio\WebFactionPHP\WebFactionException;
    
    class MyAwesomeClass {
        ...
    }

    $wf = new WebFactionClient('USERNAME', 'PASSWORD', 'MACHINE', 'VERSION');



// ing this file is in a "webroot" type folder...
FortyTwoStudio\WebFactionPHP\WebFactionException;

class MyAwesomeClass
{

    public function createDatabase($username = "new_db_user", $dbname = "my_new_db")
    {
        try
        {
            // create a connection to the API, use your own credentials here, obvs
            $wf = new WebFactionClient('USERNAME', 'PASSWORD', 'MACHINE', 'VERSION');

            // static method to generate random strings of given length
            $db_pass = WebFactionClient::generatePassword(21);

            // https://docs.webfaction.com/xmlrpc-api/apiref.html#method-create_db
            $database = $wf->createDb($dbname, 'mysql', $db_pass, $username);

            // https://docs.webfaction.com/xmlrpc-api/apiref.html#method-change_db_user_password
            //otherwise it doesn't seem to use it. Possibly because we're creating the user at the same time as the DB above
            $wf->changeDbUserPassword($username, $db_pass, 'mysql');

        } catch (WebFactionException $e)
        {
            // Something went wrong, find out what with $e->getMessage() but be warned, WebFaction exception messages are often
            // vague and unhelpful!
            return "rut roh, this went wrong: " . $e->getMessage();
        }

        // database created, keep a record of $db_pass if you want to use it somewhere!
        return "$db_pass";
    }

}

echo (new MyAwesomeClass())->createDatabase(); // if you didn't change the credentials in this example => rut roh, this went wrong: LoginError