Rackspace Cloud SDK for PHP Getting Started Guide_PHP Tutorial

WBOY
Release: 2016-07-15 13:21:45
Original
1122 people have browsed it

Getting Started Guide to Rackspace Cloud SDK for PHP

Rackspace Cloud SDK for PHP is an SDK used to help PHP developers more conveniently develop applications based on OpenStack and Rackspace clouds (including public and private clouds).
The SDK includes the following components:
1. php-opencloud language binding (in the "lib" directory)
2. Documentation (under the "docs/" directory)
3. Sample code (in the "samples/" directory)
How to install SDK
The SDK is available through Rackspace’s GitHub source. http://github.com/rackspace/php-opencloud
Get the code for the most easily available release (stable version):
1. Click the Tags link
2. Select the latest version and click the “zip” or “.tar.gz” button below
3. Find the file you just downloaded in your download directory. Remember the directory location, you'll need it later.
Create some cloud servers
In this example, you will write code to create two 1GB cloud servers running Ubuntu 13.04 system. You will need a text editor to edit the code below. Here is the complete code:
require('/path/to/lib/php-opencloud.php');
define('IMAGE_UBUNTU', '9922a7c7-5a42-4a56-bc6a-93f857ae2346');
define('FLAVOR_1GB', '3');
// Create certificate
$connection = new OpenCloudRackspace(
RACKSPACE_US,
array( 'username' => 'USERNAME',
                'password' => 'PASSWORD'));
// Connect to compute service
$compute = $connection->Compute('cloudServersOpenStack', 'ORD');
// Find mirror
$image = $compute->Image(IMAGE_UBUNTU);
// Get flavor object
$flavor = $compute->Flavor(FLAVOR_1GB);
// Create server
for ($i=0; $i<2; $i++) {
                                                                                                                                                                                                                                                                 
$resp= $server->Create(array(
                                                                                                                                                                                                                                                                                                    
                                                                                                                                                   
                                                                                                                                                                       
                                                                                                                                            >
                                  die("Errorbuilding server. Response is ".$resp->HttpBody());
                                                                                                                                                     
                                                                                                                                                                                                                                                printf("Server[%s] is building. Root password is [%s]n",
                                                                                                                                                                                    $server->Name(),$server->adminPass);
}
Understand the program
require('/path/to/lib/php-opencloud.php');
The Require() statement includes the **php-opencloud** library. You will need to edit this field to specify the exact path to the php-opencloud.php file (in the lib/ folder of the directory you just downloaded).
define('IMAGE_UBUNTU', '9922a7c7-5a42-4a56-bc6a-93f857ae2346');
define('FLAVOR_1GB','3');
These two constants define the image ID of the Ubuntu13.04 image and the flavor of 1GB. To use a different flavor or image, use the novaclient CLI to query the Rackspace Control Panle for available values.
// establish our credentials
$connection = new OpenCloudRackspace(
RACKSPACE_US,
array( 'username' => 'USERNAME',
                'password' => 'PASSWORD'));
This creates a '$connection' object using the 'OpenCloudRackspace' connector. This object requires two parameters:
1. The URL of the authentication endpoint (`RACKSPACE_US` is a very useful constant provided by **php-opencloud**).
2. An array containing your username and password. Edit the value of 'USERNAME' and 'PASSWORD' to map your own username and password.
// Now, connect to compute service
$compute = $connection->Compute('cloudServersOpenStack', 'ORD');
Create a "Compute" class that connects to the specified service capability in the ORD region (in this case, the service is named "cloudServersOpenStack"),
The Compute() method returns a new connection to the service on each call.
// first, find the image
$image = $compute->Image(IMAGE_UBUNTU);
// get a flavor object
$flavor = $compute->Flavor(FLAVOR_1GB);
These are two other factory methods, returning an "Image" object and a "Flavor" object respectively.
// create the server
for ($i=0; $i<2; $i++) {
                                                                                                                                                                                                                                                                 
$resp= $server->Create(array(
                                                                                                                                                                                                                                                                                                    
                                                                                                                                                   
                                                                                                                                                                       
                      //check for errors
>
                                  die("Errorbuilding server. Response is ".$resp->HttpBody());
                                                                                                                                       
                                                                                                                                                                                                                                      printf("Server[%s] is building. Root password is [%s]n",
                                                                                                                                                                                                                           
}
This actually creates the server. Creates a new, empty server object from the "$compute" service, using the "Server()" method.
Finally, the "Server" object calls the "Create" method and needs to pass an attribute array as a parameter. 'name', 'image', 'flavor' are the properties required to create a new server.
http://www.bkjia.com/PHPjc/477125.html
www.bkjia.com
true

http: //www.bkjia.com/PHPjc/477125.html

Rackspace Cloud SDK for PHP Getting Started Guide Rackspace Cloud SDK for PHP is a development tool used to help PHP developers more conveniently Based on OpenStack and Rackspace cloud (including public and private cloud) applications...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template