Difference between revisions of "PHP Examples (Rest API)"
From Rackspace Email & Apps API
(new php example) |
(→PHP) |
||
(3 intermediate revisions by one other user not shown) | |||
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 28: | Line 37: | ||
} | } | ||
− | public function post($url_string, $fields) | + | public function post($url_string, $fields, $format) |
{ | { | ||
− | $curl_session = self::construct_session($url_string, | + | $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) | ||
+ | { | ||
+ | $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) | public function delete($url_string) | ||
Line 54: | Line 75: | ||
curl_close($curl_session); | curl_close($curl_session); | ||
− | return | + | return $response; |
} | } | ||
Line 64: | 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 89: | 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 101: | 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 109: | Line 132: | ||
'text/xml'); | 'text/xml'); | ||
− | echo $response | + | echo $response . "\r\n"; |
− | |||
− | |||
?> | ?> | ||
</pre> | </pre> | ||
Line 120: | 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 130: | 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 . "\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: | A get with paging: | ||
Line 142: | 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 152: | 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 171: | 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"; } ?>