Skip to content

Instantly share code, notes, and snippets.

@rgbkrk
Last active December 28, 2015 20:29
Show Gist options
  • Save rgbkrk/7557637 to your computer and use it in GitHub Desktop.
Save rgbkrk/7557637 to your computer and use it in GitHub Desktop.
Simple PHP-OpenCloud sample code
<?php
// Let's enable error reporting on this page
error_reporting(E_ALL);
ini_set('display_errors', True);
// If using composer
require 'vendor/autoload.php';
//// If using a zip, will have to register the OpenCloud namespace
// Define the path to the library
//$libraryPath = '/path/to/php-opencloud';
// Include the autoloader
//require_once $libraryPath . '/Autoload.php';
// Register the root OpenCloud namespace
//$classLoader = new SplClassLoader('OpenCloud', $libraryPath . '/lib');
//$classLoader->register();
//require_once "php-opencloud.php";
use OpenCloud\Rackspace;
$mysecret = array(
'username' => 'dse.junk',
'apiKey' => 'XXXXXXXXXXXXXXXXXXXXXXXXX' # Get this from account settings
);
// establish our credentials
$connection = new Rackspace(Rackspace::US_IDENTITY_ENDPOINT, $mysecret);
$service = $connection->objectStoreService('cloudFiles', 'IAD');
// Note: can also leave region out
//$service = $connection->objectStoreService('cloudFiles');
$containerList = $service->listContainers();
while ($container = $containerList->next()) {
print("<p>Container: $container->name </p>");
}
// Create a container
//$container = $service->createContainer('stuff');
// Get the container (shown as example in case container exists)
$container = $service->getContainer('stuff');
print("<p>Container: $container->name </p>");
// CDN enable the container
$container->enableCdn();
// Get the CDN location
$cdn = $container->getCdn();
$cdn_uri = $cdn->getCdnUri();
print("<p> CDN URI: $cdn_uri </p>");
// The basic building block of cloudfiles, an object
$obj = $container->uploadObject("test.txt", "That was fun");
$obj = $container->getObject("test.txt");
$public_url = $obj->getPublicUrl();
$content = $obj->getContent();
print("<a href='$public_url'>$content</a>");
?>
# System dependencies
sudo apt-get update -y
sudo apt-get install -y apache2
sudo apt-get install -y mysql-server libapache2-mod-auth-mysql php5-mysql # Must do MySQL setup next
sudo apt-get install -y php5 libapache2-mod-php5 php5-mcrypt
sudo apt-get install -y curl wget git php5-curl
# Get composer, drops it locally into your current directory
curl -sS https://getcomposer.org/installer | php
php composer.phar require rackspace/php-opencloud:dev-master
@rgbkrk
Copy link
Author

rgbkrk commented Nov 20, 2013

Sample run of installing composer

root@justphp:/tmp# curl -sS https://getcomposer.org/installer | php
#!/usr/bin/env php
All settings correct for using Composer
Downloading...

Composer successfully installed to: /tmp/composer.phar
Use it: php composer.phar
root@justphp:/tmp# php composer.phar require rackspace/php-opencloud:dev-master
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing symfony/event-dispatcher (v2.3.7)
    Loading from cache

  - Installing guzzle/common (v3.7.4)
    Loading from cache

  - Installing guzzle/stream (v3.7.4)
    Loading from cache

  - Installing guzzle/parser (v3.7.4)
    Loading from cache

  - Installing guzzle/http (v3.7.4)
    Loading from cache

  - Installing rackspace/php-opencloud (dev-master d1e13e7)
    Cloning d1e13e7105fa22c4c614d41545bc76f504ad9256

symfony/event-dispatcher suggests installing symfony/dependency-injection ()
symfony/event-dispatcher suggests installing symfony/http-kernel ()
Writing lock file
Generating autoload files
root@justphp:/tmp# ls -la
total 956
drwxrwxrwt  5 root root   4096 Nov 20 04:44 .
drwxr-xr-x 23 root root   4096 Nov  8 19:47 ..
-rw-r--r--  1 root root     75 Nov 20 04:44 composer.json
-rw-r--r--  1 root root  10905 Nov 20 04:44 composer.lock
-rwxr-xr-x  1 root root 934848 Nov 20 04:44 composer.phar
drwxr-xr-x  6 root root   4096 Nov 20 04:44 vendor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment