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

From Rackspace Email & Apps API
Jump to: navigation, search
(PHP)
(PHP)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
 
=== PHP ===
 
=== PHP ===
  
An API library for performing basic operations. This requires the curl and pecl_http PHP extensions:
+
An API library for performing basic operations. Save the following to a file named <code>ApiClient.php</code>:
 
<pre>
 
<pre>
 
<?php
 
<?php
 
/**
 
/**
  * Needs PHP's curl and pecl_http extensions.
+
  * Simple client class for consuming the Email & Apps REST API
 +
* (http://api-wiki.apps.rackspace.com/api-wiki/index.php/Main_Page)
 
  *
 
  *
  * All results are returned as an HttpMessage object.
+
  * 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 ApiServer
+
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 = 'v0';
+
     const VERSION = 'v1';
 
     const SERVER_HOST = 'api.emailsrvr.com';
 
     const SERVER_HOST = 'api.emailsrvr.com';
  
Line 28: Line 37:
 
     }
 
     }
  
     public function post($url_string, $fields)
+
     public function post($url_string, $fields, $format)
 
     {
 
     {
         $curl_session = self::construct_session($url_string, array());
+
        $headers = array("Accept: $format");
 +
         $curl_session = self::construct_session($url_string, $headers);
  
 
         curl_setopt($curl_session, CURLOPT_POST, true);
 
         curl_setopt($curl_session, CURLOPT_POST, true);
Line 37: Line 47:
 
         $http_message = self::send_request($curl_session);
 
         $http_message = self::send_request($curl_session);
 
         return $http_message;
 
         return $http_message;
    }
+
    }
  
 
     public function put($url_string, $fields)
 
     public function put($url_string, $fields)
 
     {
 
     {
 
         $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 65: Line 75:
 
         curl_close($curl_session);
 
         curl_close($curl_session);
  
         return new HttpMessage($response);
+
         return $response;
 
     }
 
     }
  
Line 75: Line 85:
 
         $curl_session = curl_init($url);
 
         $curl_session = curl_init($url);
  
         curl_setopt($curl_session, CURLOPT_HEADER, true);
+
         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 100: Line 113:
 
     private function construct_uri($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>
 
</pre>
  
Line 112: Line 124:
 
<?php
 
<?php
 
header('Content-Type:  text/plain');
 
header('Content-Type:  text/plain');
require_once 'ApiServer.php';
+
require_once 'ApiClient.php';
  
$client = new  ApiServer();
+
$client = new  ApiClient();
  
 
$response = $client->get(
 
$response = $client->get(
Line 120: Line 132:
 
     'text/xml');
 
     'text/xml');
  
echo  $response->getResponseCode() . "\r\n" .
+
echo  $response . "\r\n";
      $response->getHeader("x-error-message") . "\r\n" .
 
      $response->getBody();
 
 
?>
 
?>
 
</pre>
 
</pre>
Line 131: Line 141:
 
<?php
 
<?php
 
header('Content-Type:  text/plain');
 
header('Content-Type:  text/plain');
require_once 'ApiServer.php';
+
require_once 'ApiClient.php';
  
$client = new  ApiServer();
+
$client = new  ApiClient();
  
 
$fields  = Array(
 
$fields  = Array(
Line 141: 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->getResponseCode() . "\r\n" .
+
echo  $response . "\r\n";
      $response->getHeader("x-error-message") . "\r\n" .
 
      $response->getBody();
 
 
?>
 
?>
 
</pre>
 
</pre>
Line 154: Line 163:
 
<?php
 
<?php
 
header('Content-Type:  text/plain');
 
header('Content-Type:  text/plain');
require_once 'ApiServer.php';
+
require_once 'ApiClient.php';
  
$client = new  ApiServer();
+
$client = new  ApiClient();
  
 
$fields= Array(
 
$fields= Array(
Line 166: 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->getResponseCode() . "\r\n" .
+
echo  $response . "\r\n";
      $response->getHeader("x-error-message") . "\r\n" .
 
      $response->getBody();
 
 
?>
 
?>
 
</pre>
 
</pre>
Line 179: Line 187:
 
<?php
 
<?php
 
header('Content-Type: text/plain');
 
header('Content-Type: text/plain');
require_once 'ApiServer.php';
+
require_once 'ApiClient.php';
  
$client = new ApiServer();
+
$client = new ApiClient();
 
$format = 'application/json';
 
$format = 'application/json';
 
$domain = 'apiPagingTest.com';
 
$domain = 'apiPagingTest.com';
Line 189: Line 197:
 
$response = $client->get($url, $format);
 
$response = $client->get($url, $format);
  
$body = $response->getBody();
+
echo $response . "\n";
$result = json_decode($response->getBody());
+
$result = json_decode($response);
echo $body . "\n";
 
  
 
// check results
 
// check results
Line 208: Line 215:
 
                             . $pageSize . '&offset=' . $offset;
 
                             . $pageSize . '&offset=' . $offset;
 
     $response = $client->get($url, $format);
 
     $response = $client->get($url, $format);
     echo $response->getBody(). "\n";
+
     echo $response . "\n";
 
}
 
}
  

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";
}

?>