Difference between revisions of "PHP Examples (Rest API)"

From Rackspace Email & Apps API
Jump to: navigation, search
(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 WebMethods
+
/**
 +
* 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 = 'v0';
+
    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)
 
     {
 
     {
        $url = $this->getUrl($url_string);
+
         $headers = array("Accept: $format");
         $headers = array();
 
        $this->signRequest($headers);
 
        $this->assignFormat($headers, $format);
 
        $session = curl_init($url);
 
        curl_setopt($session, CURLOPT_HEADER, true);
 
        curl_setopt($session, CURLOPT_HTTPHEADER, $headers);
 
        curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
 
  
         $response = curl_exec($session);
+
         $curl_session = self::construct_session($url_string, $headers);
        curl_close($session);
 
  
         echo $response;
+
         $http_message = self::send_request($curl_session);
 +
        return $http_message;
 
     }
 
     }
  
 
     public function post($url_string, $fields, $format)
 
     public function post($url_string, $fields, $format)
 
     {
 
     {
        $url = $this->getUrl($url_string);
+
         $headers = array("Accept: $format");
         $headers = array();
+
         $curl_session = self::construct_session($url_string, $headers);
        $this->signRequest($headers);
 
         $this->assignFormat($headers, $format);
 
        $session = curl_init($url);
 
  
         curl_setopt($session, CURLOPT_HEADER, true);
+
         curl_setopt($curl_session, CURLOPT_POST, true);
        curl_setopt($session, CURLOPT_HTTPHEADER, $headers);
+
         curl_setopt($curl_session, CURLOPT_POSTFIELDS, $fields);
        curl_setopt($session, CURLOPT_POST, true);
 
         curl_setopt($session, CURLOPT_POSTFIELDS, $fields);
 
        curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
 
  
         $response = curl_exec($session);
+
         $http_message = self::send_request($curl_session);
         curl_close($session);
+
         return $http_message;
 
 
        echo $response;
 
 
     }
 
     }
  
     public function put($url_string, $fields, $format)
+
     public function put($url_string, $fields)
 
     {
 
     {
         $url = $this->getUrl($url_string);
+
         $curl_session = self::construct_session($url_string, array());
        $headers = array();
 
        $this->signRequest($headers);
 
        $this->assignFormat($headers, $format);
 
        $session = curl_init($url);
 
  
         curl_setopt($session, CURLOPT_HEADER, true);
+
         curl_setopt($curl_session, CURLOPT_CUSTOMREQUEST, 'PUT');
        curl_setopt($session, CURLOPT_HTTPHEADER, $headers);
+
         curl_setopt($curl_session, CURLOPT_POSTFIELDS, $fields);
        curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'PUT');
 
         curl_setopt($session, CURLOPT_POSTFIELDS, $fields);
 
        curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
 
  
         $response = curl_exec($session);
+
         $http_message = self::send_request($curl_session);
         curl_close($session);
+
         return $http_message;
 
 
        echo $response;
 
 
     }
 
     }
  
     public function delete($url_string, $format)
+
     public function delete($url_string)
 
     {
 
     {
         $url = $this->getUrl($url_string);
+
         $curl_session = self::construct_session($url_string, array());
        $headers = array();
+
 
        $this->signRequest($headers);
+
         curl_setopt($curl_session, CURLOPT_CUSTOMREQUEST, 'DELETE');
         $this->assignFormat($headers, $format);
 
        $session = curl_init($url);
 
  
         curl_setopt($session, CURLOPT_HEADER, true);
+
         $http_message = self::send_request($curl_session);
        curl_setopt($session, CURLOPT_HTTPHEADER, $headers);
+
         return $http_message;
         curl_setopt($session, CURLOPT_CUSTOMREQUEST, 'DELETE');
+
    }
        curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
 
  
         $response = curl_exec($session);
+
    private function send_request($curl_session)
         curl_close($session);
+
    {
 +
         $response = curl_exec($curl_session);
 +
         curl_close($curl_session);
  
         echo $response;
+
         return $response;
 
     }
 
     }
  
     public function signRequest(&$headers)
+
     private function construct_session($url_string, $existing_headers)
 
     {
 
     {
         $user_agent = 'Api Test';
+
         $headers = array_merge(
        $time_stamp = 'YYYYMMDDHHmmss';
+
                self::authorization_headers(), $existing_headers);
         $api_key = 'XXXXXXXXXXXXXXXXXXXX';
+
         $url = self::construct_uri($url_string);
         $secret_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
+
         $curl_session = curl_init($url);
  
         $data_to_sign = $api_key . $user_agent . $time_stamp . $secret_key;
+
         curl_setopt($curl_session, CURLOPT_VERBOSE, false); //set to true for debug
         $signature = base64_encode(sha1($data_to_sign, true));
+
        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");
  
         $headers[] = "User-Agent: $user_agent";
+
         return $curl_session;
        $headers[] = "X-Api-Signature: $api_key:$time_stamp:$signature";
 
 
     }
 
     }
  
     public function assignFormat(&$headers, $format)
+
     private function authorization_headers()
 
     {
 
     {
         $headers[] = "Accept: $format";
+
        $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;
 
     }
 
     }
  
     public function getUrl($url_string)
+
     private function construct_uri($url_string)
 
     {
 
     {
         $url = 'http://' . self::SERVER_HOST . '/' . self::VERSION . $url_string;
+
         $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 'WebMethods.php';
+
header('Content-Type: text/plain');
 +
require_once 'ApiClient.php';
  
$client = new WebMethods();
+
$client = new ApiClient();
 +
$format = 'application/json';
 +
$domain = 'apiPagingTest.com';
  
$url = '/customers';
+
// Get mailboxes. page size defaults to 50.
$form = 'name=new customer&referenceNumber=1000';
+
$url = '/customers/me/domains/' . $domain . '/ex/mailboxes';
$format = 'text/xml';
+
$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";
 +
}
  
$client->post($url, $form, $format);
 
 
?>
 
?>
 +
 
</pre>
 
</pre>

Latest revision as of 16: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";
}

?>