Difference between revisions of "PHP Examples (Rest API)"
From Rackspace Email & Apps API
					
										
					
					James.morgan (talk | contribs)  (→PHP)  | 
				 (→PHP)  | 
				||
| Line 1: | Line 1: | ||
=== PHP ===  | === PHP ===  | ||
| − | An API library for performing basic operations.   | + | An API library for performing basic operations. Save the following to a file named <code>ApiClient.php</code>:  | 
<pre>  | <pre>  | ||
<?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   | + | class ApiClient  | 
{  | {  | ||
     const USER_KEY = 'xxxxxxxxxxxxxxxxxxxx';  |      const USER_KEY = 'xxxxxxxxxxxxxxxxxxxx';  | ||
| Line 15: | Line 24: | ||
     const USER_AGENT = 'PHP Demo Client';  |      const USER_AGENT = 'PHP Demo Client';  | ||
| − |      const VERSION = '  | + |      const VERSION = 'v1';  | 
     const SERVER_HOST = 'api.emailsrvr.com';  |      const SERVER_HOST = 'api.emailsrvr.com';  | ||
| Line 43: | Line 52: | ||
     {  |      {  | ||
         $curl_session = self::construct_session($url_string, array());  |          $curl_session = self::construct_session($url_string, array());  | ||
| − | + | ||
         curl_setopt($curl_session, CURLOPT_CUSTOMREQUEST, 'PUT');  |          curl_setopt($curl_session, CURLOPT_CUSTOMREQUEST, 'PUT');  | ||
         curl_setopt($curl_session, CURLOPT_POSTFIELDS, $fields);  |          curl_setopt($curl_session, CURLOPT_POSTFIELDS, $fields);  | ||
| Line 66: | Line 75: | ||
         curl_close($curl_session);  |          curl_close($curl_session);  | ||
| − |          return   | + |          return $response;  | 
     }  |      }  | ||
| Line 76: | Line 85: | ||
         $curl_session = curl_init($url);  |          $curl_session = curl_init($url);  | ||
| − |          curl_setopt($curl_session,   | + |          curl_setopt($curl_session, CURLOPT_VERBOSE, false); //set to true for debug  | 
         curl_setopt($curl_session, CURLOPT_HTTPHEADER, $headers);  |          curl_setopt($curl_session, CURLOPT_HTTPHEADER, $headers);  | ||
         curl_setopt($curl_session, CURLOPT_RETURNTRANSFER, true);  |          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;  |          return $curl_session;  | ||
| Line 101: | Line 113: | ||
     private function construct_uri($url_string)  |      private function construct_uri($url_string)  | ||
     {  |      {  | ||
| − |          $url = '  | + |          $url = 'https://' . self::SERVER_HOST . '/' . self::VERSION . $url_string;  | 
         return $url;  |          return $url;  | ||
     }  |      }  | ||
}  | }  | ||
| − | |||
</pre>  | </pre>  | ||
| Line 113: | Line 124: | ||
<?php  | <?php  | ||
header('Content-Type:  text/plain');  | header('Content-Type:  text/plain');  | ||
| − | require_once '  | + | require_once 'ApiClient.php';  | 
| − | $client = new    | + | $client = new  ApiClient();  | 
$response = $client->get(  | $response = $client->get(  | ||
| Line 121: | Line 132: | ||
     'text/xml');  |      'text/xml');  | ||
| − | echo  $response  | + | echo  $response . "\r\n";  | 
| − | |||
| − | |||
?>  | ?>  | ||
</pre>  | </pre>  | ||
| Line 132: | Line 141: | ||
<?php  | <?php  | ||
header('Content-Type:  text/plain');  | header('Content-Type:  text/plain');  | ||
| − | require_once '  | + | require_once 'ApiClient.php';  | 
| − | $client = new    | + | $client = new  ApiClient();  | 
$fields  = Array(  | $fields  = Array(  | ||
| Line 142: | Line 151: | ||
$response = $client->post(  | $response = $client->post(  | ||
     '/customers/me/domains/newdomain.com',  |      '/customers/me/domains/newdomain.com',  | ||
| − |      $fields);  | + |      $fields,  | 
| + |     'application/x-www-form-urlencoded');  | ||
| − | echo  $response  | + | echo  $response . "\r\n";  | 
| − | |||
| − | |||
?>  | ?>  | ||
</pre>  | </pre>  | ||
| Line 155: | Line 163: | ||
<?php  | <?php  | ||
header('Content-Type:  text/plain');  | header('Content-Type:  text/plain');  | ||
| − | require_once '  | + | require_once 'ApiClient.php';  | 
| − | $client = new    | + | $client = new  ApiClient();  | 
$fields= Array(  | $fields= Array(  | ||
| Line 167: | Line 175: | ||
$response = $client->put(  | $response = $client->put(  | ||
     '/customers/me/domains/testdomain.com/rs/mailboxes/test.mailbox',  |      '/customers/me/domains/testdomain.com/rs/mailboxes/test.mailbox',  | ||
| − |      $fields);  | + |      $fields,  | 
| + |     'application/x-www-form-urlencoded');  | ||
| − | echo  $response  | + | echo  $response . "\r\n";  | 
| − | |||
| − | |||
?>  | ?>  | ||
</pre>  | </pre>  | ||
| Line 180: | Line 187: | ||
<?php  | <?php  | ||
header('Content-Type: text/plain');  | header('Content-Type: text/plain');  | ||
| − | require_once '  | + | require_once 'ApiClient.php';  | 
| − | $client = new   | + | $client = new ApiClient();  | 
$format = 'application/json';  | $format = 'application/json';  | ||
$domain = 'apiPagingTest.com';  | $domain = 'apiPagingTest.com';  | ||
| Line 190: | Line 197: | ||
$response = $client->get($url, $format);  | $response = $client->get($url, $format);  | ||
| − | + | echo $response . "\n";  | |
| − | $result = json_decode($response  | + | $result = json_decode($response);  | 
| − | |||
// check results  | // check results  | ||
| Line 209: | Line 215: | ||
                             . $pageSize . '&offset=' . $offset;  |                              . $pageSize . '&offset=' . $offset;  | ||
     $response = $client->get($url, $format);  |      $response = $client->get($url, $format);  | ||
| − |      echo $response  | + |      echo $response . "\n";  | 
}  | }  | ||
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";
}
?>