Difference between revisions of "PHP Examples (Rest API)"
From Rackspace Email & Apps API
					
										
					
					 (Sync to latest.)  | 
				 (→PHP)  | 
				||
| (4 intermediate revisions by 2 users not shown) | |||
| Line 1: | Line 1: | ||
=== PHP ===  | === PHP ===  | ||
| + | An API library for performing basic operations. Save the following to a file named <code>ApiClient.php</code>:  | ||
<pre>  | <pre>  | ||
<?php  | <?php  | ||
| − | class   | + | /**  | 
| + |  * Simple client class for consuming the Email & Apps REST API  | ||
| + |  * (http://api-wiki.apps.rackspace.com/api-wiki/index.php/Main_Page)  | ||
| + |  *  | ||
| + |  * Pre-requisites:  | ||
| + |  *  | ||
| + |  * - PHP's curl extension (apt-get install php5-curl)  | ||
| + |  * - CA cert files at /etc/ssl/certs (apt-get install ca-certificates)  | ||
| + |  *  | ||
| + |  * Contributors:  | ||
| + |  *  | ||
| + |  * - Rackspace Email & Apps REST API Team  | ||
| + |  * - Josh Shilling  | ||
| + |  */  | ||
| + | class ApiClient  | ||
{  | {  | ||
| − |      const VERSION = '  | + |     const USER_KEY = 'xxxxxxxxxxxxxxxxxxxx';  | 
| + |     const SECRET_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';  | ||
| + |     const USER_AGENT = 'PHP Demo Client';  | ||
| + | |||
| + |      const VERSION = 'v1';  | ||
     const SERVER_HOST = 'api.emailsrvr.com';  |      const SERVER_HOST = 'api.emailsrvr.com';  | ||
     public function get($url_string, $format)  |      public function get($url_string, $format)  | ||
     {  |      {  | ||
| − | + |          $headers = array("Accept: $format");  | |
| − |          $headers = array(  | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − |          $  | + |          $curl_session = self::construct_session($url_string, $headers);  | 
| − | |||
| − | + |          $http_message = self::send_request($curl_session);  | |
| + |         return $http_message;  | ||
     }  |      }  | ||
     public function post($url_string, $fields, $format)  |      public function post($url_string, $fields, $format)  | ||
     {  |      {  | ||
| − | + |          $headers = array("Accept: $format");  | |
| − |          $headers = array(  | + |          $curl_session = self::construct_session($url_string, $headers);  | 
| − | |||
| − |          $  | ||
| − | |||
| − |          curl_setopt($  | + |          curl_setopt($curl_session, CURLOPT_POST, true);  | 
| − | + |          curl_setopt($curl_session, CURLOPT_POSTFIELDS, $fields);  | |
| − | |||
| − |          curl_setopt($  | ||
| − | |||
| − |          $  | + |          $http_message = self::send_request($curl_session);  | 
| − | + |          return $http_message;  | |
| − | |||
| − | |||
     }  |      }  | ||
| − |      public function put($url_string, $fields  | + |      public function put($url_string, $fields)  | 
     {  |      {  | ||
| − |          $  | + |          $curl_session = self::construct_session($url_string, array());  | 
| − | |||
| − | |||
| − | |||
| − | |||
| − |          curl_setopt($  | + |          curl_setopt($curl_session, CURLOPT_CUSTOMREQUEST, 'PUT');  | 
| − | + |          curl_setopt($curl_session, CURLOPT_POSTFIELDS, $fields);  | |
| − | |||
| − |          curl_setopt($  | ||
| − | |||
| − |          $  | + |          $http_message = self::send_request($curl_session);  | 
| − | + |          return $http_message;  | |
| − | |||
| − | |||
     }  |      }  | ||
| − |      public function delete($url_string  | + |      public function delete($url_string)  | 
     {  |      {  | ||
| − |          $  | + |          $curl_session = self::construct_session($url_string, array());  | 
| − | + | ||
| − | + |          curl_setopt($curl_session, CURLOPT_CUSTOMREQUEST, 'DELETE');  | |
| − | |||
| − | |||
| − | + |          $http_message = self::send_request($curl_session);  | |
| − | + |          return $http_message;  | |
| − | + |     }  | |
| − | |||
| − |          $response = curl_exec($  | + |     private function send_request($curl_session)  | 
| − |          curl_close($  | + |     {  | 
| + |          $response = curl_exec($curl_session);  | ||
| + |          curl_close($curl_session);  | ||
| − | + |          return $response;  | |
     }  |      }  | ||
| − | + |      private function construct_session($url_string, $existing_headers)  | |
     {  |      {  | ||
| − |          $  | + |          $headers = array_merge(  | 
| − | + |                 self::authorization_headers(), $existing_headers);  | |
| − |          $  | + |          $url = self::construct_uri($url_string);  | 
| − |          $  | + |          $curl_session = curl_init($url);  | 
| − |          $  | + |          curl_setopt($curl_session, CURLOPT_VERBOSE, false); //set to true for debug  | 
| − | + |         curl_setopt($curl_session, CURLOPT_HTTPHEADER, $headers);  | |
| + |         curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, true);  | ||
| + |         curl_setopt($curl_session, CURLOPT_FOLLOWLOCATION, true);  | ||
| + |          curl_setopt($curl_session, CURLOPT_SSL_VERIFYPEER, true);  | ||
| + |         curl_setopt($curl_session, CURLOPT_CAPATH, "/etc/ssl/certs");  | ||
| − |          $  | + |          return $curl_session;  | 
| − | |||
     }  |      }  | ||
| − | + |      private function authorization_headers()  | |
     {  |      {  | ||
| − |          $headers[] = "  | + |         $time_stamp = date('YmdHis');  | 
| + | |||
| + |         $data_to_sign = self::USER_KEY . self::USER_AGENT .  | ||
| + |             $time_stamp. self::SECRET_KEY;  | ||
| + | |||
| + |         $signature = base64_encode(sha1($data_to_sign, true));  | ||
| + | |||
| + |         $headers = array();  | ||
| + |          $headers[] = "User-Agent: " . self::USER_AGENT;  | ||
| + |         $headers[] = 'X-Api-Signature: ' .  | ||
| + |             self::USER_KEY . ":$time_stamp:$signature";  | ||
| + |         return $headers;  | ||
     }  |      }  | ||
| − | + |      private function construct_uri($url_string)  | |
     {  |      {  | ||
| − |          $url = '  | + |          $url = 'https://' . self::SERVER_HOST . '/' . self::VERSION . $url_string;  | 
         return $url;  |          return $url;  | ||
     }  |      }  | ||
}  | }  | ||
| + | </pre>  | ||
| + | |||
| + | |||
| + | A simple GET:  | ||
| + | <pre>  | ||
| + | <?php  | ||
| + | header('Content-Type:  text/plain');  | ||
| + | require_once 'ApiClient.php';  | ||
| + | |||
| + | $client = new  ApiClient();  | ||
| + | |||
| + | $response = $client->get(  | ||
| + |     '/customers/me/domains',  | ||
| + |     'text/xml');  | ||
| + | |||
| + | echo  $response . "\r\n";  | ||
| + | ?>  | ||
| + | </pre>  | ||
| + | |||
| + | |||
| + | A simple POST:  | ||
| + | <pre>  | ||
| + | <?php  | ||
| + | header('Content-Type:  text/plain');  | ||
| + | require_once 'ApiClient.php';  | ||
| + | |||
| + | $client = new  ApiClient();  | ||
| + | |||
| + | $fields  = Array(  | ||
| + |     'serviceType' => 'exchange',  | ||
| + |     'exchangeMaxNumMailboxes' => '4');  | ||
| + | |||
| + | $response = $client->post(  | ||
| + |     '/customers/me/domains/newdomain.com',  | ||
| + |     $fields,  | ||
| + |     'application/x-www-form-urlencoded');  | ||
| + | |||
| + | echo  $response . "\r\n";  | ||
| + | ?>  | ||
| + | </pre>  | ||
| + | |||
| + | |||
| + | A simple PUT:  | ||
| + | <pre>  | ||
| + | <?php  | ||
| + | header('Content-Type:  text/plain');  | ||
| + | require_once 'ApiClient.php';  | ||
| + | |||
| + | $client = new  ApiClient();  | ||
| + | |||
| + | $fields= Array(  | ||
| + |     'displayName' => 'My Display',   | ||
| + |     'password' => 'testPass12#',   | ||
| + |     'size' => '10240',   | ||
| + |     'enabled' => 'true');  | ||
| + | |||
| + | $response = $client->put(  | ||
| + |     '/customers/me/domains/testdomain.com/rs/mailboxes/test.mailbox',  | ||
| + |     $fields,  | ||
| + |     'application/x-www-form-urlencoded');  | ||
| + | |||
| + | echo  $response . "\r\n";  | ||
?>  | ?>  | ||
</pre>  | </pre>  | ||
| + | |||
| + | A get with paging:  | ||
<pre>  | <pre>  | ||
<?php  | <?php  | ||
| − | require_once '  | + | header('Content-Type: text/plain');  | 
| + | require_once 'ApiClient.php';  | ||
| − | $client = new   | + | $client = new ApiClient();  | 
| + | $format = 'application/json';  | ||
| + | $domain = 'apiPagingTest.com';  | ||
| − | $url = '/customers';  | + | // Get mailboxes. page size defaults to 50.  | 
| − | $  | + | $url = '/customers/me/domains/' . $domain . '/ex/mailboxes';  | 
| − | $format   | + | $response = $client->get($url, $format);  | 
| + | |||
| + | echo $response . "\n";  | ||
| + | $result = json_decode($response);  | ||
| + | |||
| + | // check results  | ||
| + | $totalSize = $result->{"total"};  | ||
| + | $resultSize = $result->{"size"};  | ||
| + | |||
| + | // If there are more results, read the rest.  | ||
| + | if($totalSize > $resultSize)  | ||
| + | {  | ||
| + |     // start at last read index and set page size  | ||
| + |     $offset = $resultSize;  | ||
| + |     $pageSize = 50;  | ||
| + | |||
| + |     // read up to the next 50  | ||
| + |     $url = '/customers/me/domains/' . $domain . '/ex/mailboxes?size='  | ||
| + |                             . $pageSize . '&offset=' . $offset;  | ||
| + |     $response = $client->get($url, $format);  | ||
| + |     echo $response . "\n";  | ||
| + | }  | ||
| − | |||
?>  | ?>  | ||
| + | |||
</pre>  | </pre>  | ||
Latest revision as of 15:02, 5 November 2014
PHP
An API library for performing basic operations. Save the following to a file named ApiClient.php:
<?php
/**
 * Simple client class for consuming the Email & Apps REST API
 * (http://api-wiki.apps.rackspace.com/api-wiki/index.php/Main_Page)
 *
 * Pre-requisites:
 *
 * - PHP's curl extension (apt-get install php5-curl)
 * - CA cert files at /etc/ssl/certs (apt-get install ca-certificates)
 *
 * Contributors:
 *
 * - Rackspace Email & Apps REST API Team
 * - Josh Shilling
 */
class ApiClient
{
    const USER_KEY = 'xxxxxxxxxxxxxxxxxxxx';
    const SECRET_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
    const USER_AGENT = 'PHP Demo Client';
    const VERSION = 'v1';
    const SERVER_HOST = 'api.emailsrvr.com';
    public function get($url_string, $format)
    {
        $headers = array("Accept: $format");
        $curl_session = self::construct_session($url_string, $headers);
        $http_message = self::send_request($curl_session);
        return $http_message;
    }
    public function post($url_string, $fields, $format)
    {
        $headers = array("Accept: $format");
        $curl_session = self::construct_session($url_string, $headers);
        curl_setopt($curl_session, CURLOPT_POST, true);
        curl_setopt($curl_session, CURLOPT_POSTFIELDS, $fields);
        $http_message = self::send_request($curl_session);
        return $http_message;
    }
    public function put($url_string, $fields)
    {
        $curl_session = self::construct_session($url_string, array());
        curl_setopt($curl_session, CURLOPT_CUSTOMREQUEST, 'PUT');
        curl_setopt($curl_session, CURLOPT_POSTFIELDS, $fields);
        $http_message = self::send_request($curl_session);
        return $http_message;
    }
    public function delete($url_string)
    {
        $curl_session = self::construct_session($url_string, array());
        curl_setopt($curl_session, CURLOPT_CUSTOMREQUEST, 'DELETE');
        $http_message = self::send_request($curl_session);
        return $http_message;
    }
    private function send_request($curl_session)
    {
        $response = curl_exec($curl_session);
        curl_close($curl_session);
        return $response;
    }
    private function construct_session($url_string, $existing_headers)
    {
        $headers = array_merge(
                self::authorization_headers(), $existing_headers);
        $url = self::construct_uri($url_string);
        $curl_session = curl_init($url);
        curl_setopt($curl_session, CURLOPT_VERBOSE, false); //set to true for debug
        curl_setopt($curl_session, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl_session, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($curl_session, CURLOPT_SSL_VERIFYPEER, true);
        curl_setopt($curl_session, CURLOPT_CAPATH, "/etc/ssl/certs");
        return $curl_session;
    }
    private function authorization_headers()
    {
        $time_stamp = date('YmdHis');
        $data_to_sign = self::USER_KEY . self::USER_AGENT .
            $time_stamp. self::SECRET_KEY;
        $signature = base64_encode(sha1($data_to_sign, true));
        $headers = array();
        $headers[] = "User-Agent: " . self::USER_AGENT;
        $headers[] = 'X-Api-Signature: ' .
            self::USER_KEY . ":$time_stamp:$signature";
        return $headers;
    }
    private function construct_uri($url_string)
    {
        $url = 'https://' . self::SERVER_HOST . '/' . self::VERSION . $url_string;
        return $url;
    }
}
A simple GET:
<?php
header('Content-Type:  text/plain');
require_once 'ApiClient.php';
$client = new  ApiClient();
$response = $client->get(
    '/customers/me/domains',
    'text/xml');
echo  $response . "\r\n";
?>
A simple POST:
<?php
header('Content-Type:  text/plain');
require_once 'ApiClient.php';
$client = new  ApiClient();
$fields  = Array(
    'serviceType' => 'exchange',
    'exchangeMaxNumMailboxes' => '4');
$response = $client->post(
    '/customers/me/domains/newdomain.com',
    $fields,
    'application/x-www-form-urlencoded');
echo  $response . "\r\n";
?>
A simple PUT:
<?php
header('Content-Type:  text/plain');
require_once 'ApiClient.php';
$client = new  ApiClient();
$fields= Array(
    'displayName' => 'My Display', 
    'password' => 'testPass12#', 
    'size' => '10240', 
    'enabled' => 'true');
$response = $client->put(
    '/customers/me/domains/testdomain.com/rs/mailboxes/test.mailbox',
    $fields,
    'application/x-www-form-urlencoded');
echo  $response . "\r\n";
?>
A get with paging:
<?php
header('Content-Type: text/plain');
require_once 'ApiClient.php';
$client = new ApiClient();
$format = 'application/json';
$domain = 'apiPagingTest.com';
// Get mailboxes. page size defaults to 50.
$url = '/customers/me/domains/' . $domain . '/ex/mailboxes';
$response = $client->get($url, $format);
echo $response . "\n";
$result = json_decode($response);
// check results
$totalSize = $result->{"total"};
$resultSize = $result->{"size"};
// If there are more results, read the rest.
if($totalSize > $resultSize)
{
    // start at last read index and set page size
    $offset = $resultSize;
    $pageSize = 50;
    // read up to the next 50
    $url = '/customers/me/domains/' . $domain . '/ex/mailboxes?size='
                            . $pageSize . '&offset=' . $offset;
    $response = $client->get($url, $format);
    echo $response . "\n";
}
?>