Difference between revisions of "RestAPI/Examples/PHP"

From Rackspace Email & Apps API
Jump to: navigation, search
(Create PHP Example page)
 
(No difference)

Latest revision as of 08:59, 11 September 2009

PHP

<?php
class WebMethods
{
    const VERSION = 'v0';
    const SERVER_HOST = 'api.emailsrvr.com';

    public function get($url_string, $format)
    {
        $url = $this->getUrl($url_string);
        $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_close($session);

        echo $response;
    }

    public function post($url_string, $fields, $format)
    {
        $url = $this->getUrl($url_string);
        $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_POST, true);
        curl_setopt($session, CURLOPT_POSTFIELDS, $fields);
        curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

        $response = curl_exec($session);
        curl_close($session);

        echo $response;
    }

    public function put($url_string, $fields, $format)
    {
        $url = $this->getUrl($url_string);
        $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_CUSTOMREQUEST, 'PUT');
        curl_setopt($session, CURLOPT_POSTFIELDS, $fields);
        curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

        $response = curl_exec($session);
        curl_close($session);

        echo $response;
    }

    public function delete($url_string, $format)
    {
        $url = $this->getUrl($url_string);
        $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_CUSTOMREQUEST, 'DELETE');
        curl_setopt($session, CURLOPT_RETURNTRANSFER, true);

        $response = curl_exec($session);
        curl_close($session);

        echo $response;
    }

    public function signRequest(&$headers)
    {
        $user_agent = 'Api Test';
        $time_stamp = 'YYYYMMDDHHmmssff';
        $api_key = 'XXXXXXXXXXXXXXXXXXXX';
        $secret_key = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';

        $data_to_sign = $api_key . $user_agent . $time_stamp . $secret_key;
        $signature = base64_encode(sha1($data_to_sign, true));

        $headers[] = "User-Agent: $user_agent";
        $headers[] = "X-Api-Signature: $api_key:$time_stamp:$signature";
    }

    public function assignFormat(&$headers, $format)
    {
        $headers[] = "Accept: $format";
    }

    public function getUrl($url_string)
    {
        $url = 'http://' . self::SERVER_HOST . '/' . self::VERSION . $url_string;
        return $url;
    }
}
?>
<?php
require_once 'WebMethods.php';

$client = new WebMethods();

$url = '/customers';
$form = 'name=new customer&referenceNumber=1000';
$format = 'text/xml';

$client->post($url, $form, $format);
?>