Code Examples

From API Documentation

(Difference between revisions)
Jump to: navigation, search
(Example 2: read an individual agent resource)
(Undo revision 1191 by AngelSlayerL0AZ (Talk))
 
(43 intermediate revisions not shown)
Line 1: Line 1:
-
Below are examples of how to access the API using PHP. These examples use the cURL library for handling the HTTP request. The examples us a PHP class for making the REST requests. This class is included after the examples.
+
Below are examples of how to access the API using PHP. These examples demonstrate a variety of available methods and content formats. While we do not cover every possible use of the API here, these examples are meant to be indicative of all API calls.
 +
 
 +
All examples use the cURL library for handling the HTTP request. There are other methods available in PHP (and other languages, of course), and any means that allows you to send and receive the properly formatted requests and responses is perfectly acceptable.
 +
 
 +
== Example 1: read the ''agents'' collection resource ==
 +
<source lang="php">
 +
<?php
 +
// Set some universal configuration variables. These will be
 +
// the same for all your API requests for a particular account.
 +
$strUsername = 'YOUR_API_USERNAME';
 +
$strPassword = 'YOUR_API_PASSWORD';
 +
$strBaseURI = 'http://api.rezora.com/v1/';
 +
 
 +
// Set some configuration variables specific to the task at hand.
 +
// In this example, we'll be reading data from the agents collection
 +
// resource. We'll retrieve the data in CSV format.
 +
$strResourcePath = 'agents';
 +
$strRequestFormat = 'text/plain';
 +
$strResponseFormat = 'text/csv';
 +
 
 +
// Open a cURL resource
 +
$ch = curl_init();
 +
 
 +
// Set the necessary parameters
 +
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
 +
curl_setopt($ch, CURLOPT_USERPWD, $strUsername . ':' . $strPassword);
 +
curl_setopt($ch, CURLOPT_URL, $strBaseURI . $strResourcePath);
 +
curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Accept: ' . $strResponseFormat, 'Content-type: ' . $strRequestFormat));
 +
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 +
 
 +
// Make the request and output the response.
 +
echo curl_exec($ch);
 +
 
 +
// Close the cURL resource
 +
curl_close($ch);
 +
?>
 +
</source>
-
== Example 1: read the agents collection resource ==
 
-
<?php
 
-
// Include the RestMakeRequest class file
 
-
include_once 'RestMakeRequest.php';
 
-
 
-
// Set some universal configuration variables. These will be
 
-
// the same for all your API requests for a particular account.
 
-
$strUsername = 'YOUR_USERNAME';
 
-
$strPassword = 'YOUR_PASSWORD';
 
-
$strBaseURI = '<nowiki>http://api.rezora.com/v1/</nowiki>';
 
-
 
-
// Set some configuration variables specific to the task at hand.
 
-
// In this example, we'll be reading data from the agents collection
 
-
// resource. We'll retrieve the data in text format.
 
-
$strResourcePath = 'agents';
 
-
 
-
// Create an instance of the RestMakeRequest.
 
-
$objRequest = new RestMakeRequest($strBaseURI . $strResourcePath);
 
-
 
-
// Set the necessary parameters.
 
-
$objRequest->setUsername($strUsername);
 
-
$objRequest->setPassword($strPassword);
 
-
 
-
// Make the request and capture the response.
 
-
$objRequest->execute();
 
-
 
-
// Output the response data.
 
-
echo $objRequest->getResponseBody();
 
-
?>
 
-
 
== Example 2: read the ''agents/ID'' resource ==
== Example 2: read the ''agents/ID'' resource ==
-
<?php
+
<source lang="php">
-
// Include the RestMakeRequest class file
+
<?php
-
include_once 'RestMakeRequest.php';
+
// Set some universal configuration variables. These will be
-
+
// the same for all your API requests for a particular account.
-
// Set some universal configuration variables. These will be
+
$strUsername = 'YOUR_API_USERNAME';
-
// the same for all your API requests for a particular account.
+
$strPassword = 'YOUR_API_PASSWORD';
-
$strUsername = 'YOUR_USERNAME';
+
$strBaseURI = 'http://api.rezora.com/v1/';
-
$strPassword = 'YOUR_PASSWORD';
+
-
$strBaseURI = '<nowiki>http://api.rezora.com/v1/</nowiki>';
+
-
+
-
// Set some configuration variables specific to the task at hand.
+
-
// In this example, we'll be reading data from the resource for
+
-
// an agent with the ID 1234. We'll retrieve the data in XML format.
+
-
$strResourcePath = '<nowiki>agents/1234.xml</nowiki>';
+
-
+
-
// Create an instance of the RestMakeRequest.
+
-
$objRequest = new RestMakeRequest($strBaseURI . $strResourcePath);
+
-
+
-
// Set the necessary parameters.
+
-
$objRequest->setUsername($strUsername);
+
-
$objRequest->setPassword($strPassword);
+
-
+
-
// Make the request and capture the response.
+
-
$objRequest->execute();
+
-
+
-
// Output the response data.
+
-
echo $objRequest->getResponseBody();
+
-
?>
+
-
== PHP Class for Making REST Requests ==
+
// Set some configuration variables specific to the task at hand.
-
<?php
+
// In this example, we'll be reading data from the resource for
-
//*****************************************
+
// an agent with the ID 1234. We'll retrieve the data in XML format.
-
// The following class handles making the REST request and capturing
+
// Notice that the presence of the .xml extension will override the
-
// the response from the API. This class is adapted from a script
+
// Accept header value ($strResponseFormat). Alternatively, you
-
// provided in an excellent article on REST API consumption by
+
// could leave off the extension and set the Accept header value
-
// Ian Selby, titled "Making RESTful Requests in PHP."
+
// ($strResponseFormat) to application/xml.
-
// <nowiki>http://www.gen-x-design.com/archives/making-restful-requests-in-php/</nowiki>
+
$strResourcePath = 'agents/1234.xml';
-
//*****************************************
+
$strRequestFormat = 'text/plain';
-
class API_RestMakeRequest {
+
$strResponseFormat = 'text/csv';
-
    protected $url;
+
 
-
    protected $verb;
+
// Open a cURL resource
-
    protected $requestBody;
+
$ch = curl_init();
-
    protected $requestLength;
+
 
-
    protected $username;
+
// Set the necessary parameters
-
    protected $password;
+
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
-
    protected $contentType;
+
curl_setopt($ch, CURLOPT_USERPWD, $strUsername . ':' . $strPassword);
-
    protected $acceptType;
+
curl_setopt($ch, CURLOPT_URL, $strBaseURI . $strResourcePath);
-
    protected $responseBody;
+
curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Accept: ' . $strResponseFormat, 'Content-type: ' . $strRequestFormat));
-
    protected $responseInfo;
+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
-
    protected $requestInfo;
+
 
-
+
// Make the request and output the response.
-
    public function __construct ($url = null, $verb = 'GET', $requestBody = array()) {
+
echo curl_exec($ch);
-
        $this->url                = $url;
+
 
-
        $this->verb                = $verb;
+
// Close the cURL resource
-
        $this->requestBody        = $requestBody;
+
curl_close($ch);
-
        $this->requestLength    = 0;
+
?>
-
        $this->username            = null;
+
</source>
-
        $this->password            = null;
+
 
-
        $this->contentType        = 'text/plain';
+
== Example 3: add a new contact to the ''contacts'' collection resource ==
-
        $this->acceptType        = 'text/plain';
+
<source lang="php">
-
        $this->responseBody        = null;
+
<?php
-
        $this->responseInfo        = null;
+
// Set some universal configuration variables. These will be
-
        $this->requestInfo        = null;
+
// the same for all your API requests for a particular account.
-
    }
+
$strUsername = 'YOUR_API_USERNAME';
-
    public function flush () {
+
$strPassword = 'YOUR_API_PASSWORD';
-
        $this->requestBody        = null;
+
$strBaseURI = 'http://api.rezora.com/v1/';
-
        $this->requestLength    = 0;
+
 
-
        $this->verb                = 'GET';
+
// Set some configuration variables specific to the task at hand.
-
        $this->responseBody        = null;
+
// In this example, we'll be posting data to the contacts collection
-
        $this->responseInfo        = null;
+
// resource to create a new contact. We'll send data in JSON format,
-
        $this->requestInfo        = null;
+
// and retrieve data in XML format.
-
    }
+
$strResourcePath = 'contacts';
-
    public function execute () {
+
$strRequestFormat = 'application/json';
-
        $ch = curl_init();
+
$strResponseFormat = 'application/xml';
-
        $this->setAuth($ch);
+
$strData = '{
-
        try {
+
"data":{
-
            switch (strtoupper($this->verb)) {
+
  "contact_name":"Jane Smith",
-
                case 'GET':
+
  "contact_email":"jane.smith@company.com",
-
                    $this->executeGet($ch);
+
  "contact_type":"Active",
-
                    break;
+
  "contact_title":"Consultant",
-
                case 'POST':
+
  "contact_company":"Acme Consulting",
-
                    $this->executePost($ch);
+
  "contact_phone_home":"303-555-1234",
-
                    break;
+
  "contact_phone_work":"303-555-1235",
-
                case 'PUT':
+
  "contact_phone_mobile":"303-555-1236",
-
                    $this->executePut($ch);
+
  "contact_phone_fax":"303-555-1237",
-
                    break;
+
  "contact_business_street":"123 Church Street",
-
                case 'DELETE':
+
  "contact_business_street2":"Suite 12",
-
                    $this->executeDelete($ch);
+
  "contact_business_city":"Shady Acres",
-
                    break;
+
  "contact_business_state":"CO",
-
                default:
+
  "contact_business_zip":"12345",
-
                    throw new InvalidArgumentException('Current verb (' . $this->verb . ') is an invalid REST verb.');
+
  "contact_home_street":"124 Church Street",
-
            }
+
  "contact_home_street2":"",
-
        }
+
  "contact_home_city":"Shady Acres",
-
        catch (InvalidArgumentException $e) {
+
  "contact_home_state":"CO",
-
            curl_close($ch);
+
  "contact_home_zip":"12345"
-
            throw $e;
+
-
        }
+
-
        catch (Exception $e) {
+
-
            curl_close($ch);
+
-
            throw $e;
+
-
        }
+
-
    }
+
-
    public function buildPostBody ($data = null) {
+
-
        $data = ($data !== null) ? $data : $this->requestBody;
+
-
        if ($data !== null) {
+
-
            if (!is_array($data)) {
+
-
                throw new InvalidArgumentException('Invalid data input for postBody. Array expected');
+
-
            }
+
-
            if (strpos($this->contentType, 'json')) {
+
-
                $data = 'data=' . json_encode($data);
+
-
            } else if (strpos($this->contentType, 'xml')) {
+
-
                $objXML = new XmlWriter();
+
-
                $objXML->openMemory();
+
-
                $objXML->startDocument('1.0', 'UTF-8');
+
-
                API_RestMakeRequest::arrayToXML($objXML, $data, $strItemNode);
+
-
                $data = 'data=' . urlencode($objXML->outputMemory(true));
+
-
            } else {
+
-
                $data = http_build_query($data);
+
-
            }
+
-
        }
+
-
        $this->requestBody = $data;
+
-
    }
+
-
    protected function executeGet ($ch) {
+
-
        if (!is_string($this->requestBody)) {
+
-
            $this->buildPostBody();
+
-
        }
+
-
        if (strlen($this->requestBody) > 0) {
+
-
            $this->url .= '?' . $this->requestBody;
+
-
        }
+
-
        $this->doExecute($ch);
+
-
    }
+
-
    protected function executePost ($ch) {
+
-
        if (!is_string($this->requestBody)) {
+
-
            $this->buildPostBody();
+
-
        }
+
-
        curl_setopt($ch, CURLOPT_POSTFIELDS, $this->requestBody);
+
-
        curl_setopt($ch, CURLOPT_POST, 1);
+
-
        $this->doExecute($ch);
+
-
    }
+
-
    protected function executePut ($ch) {
+
-
        if (!is_string($this->requestBody)) {
+
-
            $this->buildPostBody();
+
-
        }
+
-
        $this->requestLength = strlen($this->requestBody);
+
-
        $fh = fopen('php://memory', 'rw');
+
-
        fwrite($fh, $this->requestBody);
+
-
        rewind($fh);
+
-
        curl_setopt($ch, CURLOPT_INFILE, $fh);
+
-
        curl_setopt($ch, CURLOPT_INFILESIZE, $this->requestLength);
+
-
        curl_setopt($ch, CURLOPT_PUT, true);
+
-
        $this->doExecute($ch);
+
-
        fclose($fh);
+
-
    }
+
-
    protected function executeDelete ($ch) {
+
-
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
+
-
        $this->doExecute($ch);
+
-
    }
+
-
    protected function doExecute (&$curlHandle) {
+
-
        $this->setCurlOpts($curlHandle);
+
-
        $this->responseBody = curl_exec($curlHandle);
+
-
        $this->responseInfo    = curl_getinfo($curlHandle);
+
-
        $this->requestInfo = curl_getinfo($curlHandle, CURLINFO_HEADER_OUT);
+
-
        curl_close($curlHandle);
+
-
    }
+
-
    protected function setCurlOpts (&$curlHandle) {
+
-
        curl_setopt($curlHandle, CURLOPT_TIMEOUT, 10);
+
-
        curl_setopt($curlHandle, CURLOPT_URL, $this->url);
+
-
        curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, true);
+
-
        curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array ('Accept: ' . $this->acceptType, 'Content-type: ' . $this->contentType));
+
-
        curl_setopt($curlHandle, CURLINFO_HEADER_OUT, true);
+
-
    }
+
-
    protected function setAuth (&$curlHandle) {
+
-
        if ($this->username !== null && $this->password !== null) {
+
-
            curl_setopt($curlHandle, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
+
-
            curl_setopt($curlHandle, CURLOPT_USERPWD, $this->username . ':' . $this->password);
+
-
        }
+
-
    }
+
-
    protected function arrayToXML(XMLWriter $objXML, $arrData){
+
-
        foreach($arrData as $key => $value){
+
-
            if(is_array($value)){
+
-
                $this->arrayToXML($objXML, $value);
+
-
                continue;
+
-
            }
+
-
            $objXML->writeElement($key, $value);
+
-
        }
+
-
    }
+
-
    public function getContentType () {
+
-
        return $this->contentType;
+
-
    }
+
-
    public function setContentType ($contentType) {
+
-
        $this->contentType = $contentType;
+
-
    }
+
-
    public function getAcceptType () {
+
-
        return $this->acceptType;
+
-
    }
+
-
    public function setAcceptType ($acceptType) {
+
-
        $this->acceptType = $acceptType;
+
-
    }
+
-
    public function getPassword () {
+
-
        return $this->password;
+
-
    }
+
-
    public function setPassword ($password) {
+
-
        $this->password = $password;
+
-
    }
+
-
    public function getResponseBody () {
+
-
        return $this->responseBody;
+
-
    }
+
-
    public function getResponseInfo () {
+
-
        return $this->responseInfo;
+
-
    }
+
-
    public function getUrl () {
+
-
        return $this->url;
+
-
    }
+
-
    public function setUrl ($url) {
+
-
        $this->url = $url;
+
-
    }
+
-
    public function getUsername () {
+
-
        return $this->username;
+
-
    }
+
-
    public function setUsername ($username) {
+
-
        $this->username = $username;
+
-
    }
+
-
    public function getVerb () {
+
-
        return $this->verb;
+
-
    }
+
-
    public function setVerb ($verb) {
+
-
        $this->verb = $verb;
+
-
    }
+
-
    public function getRequestInfo() {
+
-
        return $this->requestInfo;
+
-
    }
+
  }
  }
-
  ?>
+
}
 +
';
 +
 
 +
// Open a cURL resource
 +
$ch = curl_init();
 +
 
 +
// Set the necessary parameters
 +
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
 +
curl_setopt($ch, CURLOPT_USERPWD, $strUsername . ':' . $strPassword);
 +
curl_setopt($ch, CURLOPT_URL, $strBaseURI . $strResourcePath);
 +
curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Accept: ' . $strResponseFormat, 'Content-type: ' . $strRequestFormat));
 +
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 +
 
 +
// Make this a POST request
 +
curl_setopt($ch, CURLOPT_POST, true);
 +
curl_setopt($ch, CURLOPT_POSTFIELDS, $strData);
 +
 
 +
// Make the request and output the response.
 +
echo curl_exec($ch);
 +
 
 +
// Close the cURL resource
 +
curl_close($ch);
 +
?>
 +
</source>
 +
 
 +
== Example 4: update an individual contact from the ''contacts/ID'' resource ==
 +
<source lang="php">
 +
<?php
 +
// Set some universal configuration variables. These will be
 +
// the same for all your API requests for a particular account.
 +
$strUsername = 'YOUR_API_USERNAME';
 +
$strPassword = 'YOUR_API_PASSWORD';
 +
$strBaseURI = 'http://api.rezora.com/v1/';
 +
 
 +
// Set some configuration variables specific to the task at hand.
 +
// In this example, we'll be updating data for an existing contact resource.
 +
// We'll send data in XML format, and retrieve data in CSV format.
 +
$strResourcePath = 'contacts/123456';
 +
$strRequestFormat = 'application/xml';
 +
$strResponseFormat = 'text/csv';
 +
$strData = '<?xml version="1.0" encoding="UTF-8"?>
 +
<data>
 +
  <contact_name>Jane Smith-Johnson</contact_name>
 +
<contact_email>jane.smith.johnson@company.com</contact_email>
 +
<contact_type>Active</contact_type>
 +
<contact_title>Consultant</contact_title>
 +
<contact_company>Acme Consulting</contact_company>
 +
<contact_phone_home>303-555-1234</contact_phone_home>
 +
<contact_phone_work>303-555-1235</contact_phone_work>
 +
<contact_phone_mobile>303-555-1236</contact_phone_mobile>
 +
<contact_phone_fax>303-555-1237</contact_phone_fax>
 +
<contact_business_street>123 Church Street</contact_business_street>
 +
<contact_business_street2>Suite 12</contact_business_street2>
 +
<contact_business_city>Shady Acres</contact_business_city>
 +
<contact_business_state>CO</contact_business_state>
 +
<contact_business_zip>12345</contact_business_zip>
 +
<contact_home_street>124 Church Street</contact_home_street>
 +
<contact_home_street2></contact_home_street2>
 +
<contact_home_city>Shady Acres</contact_home_city>
 +
<contact_home_state>CO</contact_home_state>
 +
<contact_home_zip>12345</contact_home_zip>
 +
</data>
 +
';
 +
 
 +
// Open a cURL resource
 +
$ch = curl_init();
 +
 
 +
// Set the necessary parameters
 +
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
 +
curl_setopt($ch, CURLOPT_USERPWD, $strUsername . ':' . $strPassword);
 +
curl_setopt($ch, CURLOPT_URL, $strBaseURI . $strResourcePath);
 +
curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Accept: ' . $strResponseFormat, 'Content-type: ' . $strRequestFormat));
 +
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 +
 
 +
// PUT is originally intended for uploading files,
 +
// so we have to make our data look like a file.
 +
// Open a file resource using the php://memory stream
 +
$fh = fopen('php://memory', 'rw');
 +
 
 +
// Write the data to the file
 +
fwrite($fh, $strData);
 +
 
 +
// Move back to the beginning of the file
 +
rewind($fh);
 +
 
 +
// Make this a PUT request
 +
curl_setopt($ch, CURLOPT_PUT, true);
 +
curl_setopt($ch, CURLOPT_INFILE, $fh);
 +
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($strData));
 +
 
 +
// Make the request and output the response.
 +
echo curl_exec($ch);
 +
 
 +
// Close the file resource
 +
fclose($fh);
 +
 
 +
// Close the cURL resource
 +
curl_close($ch);
 +
?>
 +
</source>
 +
 
 +
== Example 5: delete all contacts from the ''contacts'' collection resource ==
 +
<source lang="php">
 +
<?php
 +
// Set some universal configuration variables. These will be
 +
// the same for all your API requests for a particular account.
 +
$strUsername = 'YOUR_API_USERNAME';
 +
$strPassword = 'YOUR_API_PASSWORD';
 +
$strBaseURI = 'http://api.rezora.com/v1/';
 +
 
 +
// Set some configuration variables specific to the task at hand.
 +
// In this example, we'll be deleting data from the contacts collection
 +
// resource. We'll retrieve the data in JSON format.
 +
$strResourcePath = 'contacts';
 +
$strRequestFormat = 'text/plain';
 +
$strResponseFormat = 'application/json';
 +
 
 +
// Open a cURL resource
 +
$ch = curl_init();
 +
 
 +
// Set the necessary parameters
 +
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
 +
curl_setopt($ch, CURLOPT_USERPWD, $strUsername . ':' . $strPassword);
 +
curl_setopt($ch, CURLOPT_URL, $strBaseURI . $strResourcePath);
 +
curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Accept: ' . $strResponseFormat, 'Content-type: ' . $strRequestFormat));
 +
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 +
 
 +
// Make this a DELETE request
 +
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
 +
 
 +
// Make the request and output the response.
 +
echo curl_exec($ch);
 +
 
 +
// Close the cURL resource
 +
curl_close($ch);
 +
?>
 +
</source>

Latest revision as of 16:50, 1 March 2012

Below are examples of how to access the API using PHP. These examples demonstrate a variety of available methods and content formats. While we do not cover every possible use of the API here, these examples are meant to be indicative of all API calls.

All examples use the cURL library for handling the HTTP request. There are other methods available in PHP (and other languages, of course), and any means that allows you to send and receive the properly formatted requests and responses is perfectly acceptable.

Contents

Example 1: read the agents collection resource

<?php
// Set some universal configuration variables. These will be
// the same for all your API requests for a particular account.
$strUsername = 'YOUR_API_USERNAME';
$strPassword = 'YOUR_API_PASSWORD';
$strBaseURI = 'http://api.rezora.com/v1/';
 
// Set some configuration variables specific to the task at hand.
// In this example, we'll be reading data from the agents collection
// resource. We'll retrieve the data in CSV format.
$strResourcePath = 'agents';
$strRequestFormat = 'text/plain';
$strResponseFormat = 'text/csv';
 
// Open a cURL resource
$ch = curl_init();
 
// Set the necessary parameters
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_USERPWD, $strUsername . ':' . $strPassword);
curl_setopt($ch, CURLOPT_URL, $strBaseURI . $strResourcePath);
curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Accept: ' . $strResponseFormat, 'Content-type: ' . $strRequestFormat));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
// Make the request and output the response.
echo curl_exec($ch);
 
// Close the cURL resource
curl_close($ch);
?>

Example 2: read the agents/ID resource

<?php
// Set some universal configuration variables. These will be
// the same for all your API requests for a particular account.
$strUsername = 'YOUR_API_USERNAME';
$strPassword = 'YOUR_API_PASSWORD';
$strBaseURI = 'http://api.rezora.com/v1/';
 
// Set some configuration variables specific to the task at hand.
// In this example, we'll be reading data from the resource for
// an agent with the ID 1234. We'll retrieve the data in XML format.
// Notice that the presence of the .xml extension will override the
// Accept header value ($strResponseFormat). Alternatively, you
// could leave off the extension and set the Accept header value
// ($strResponseFormat) to application/xml.
$strResourcePath = 'agents/1234.xml';
$strRequestFormat = 'text/plain';
$strResponseFormat = 'text/csv';
 
// Open a cURL resource
$ch = curl_init();
 
// Set the necessary parameters
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_USERPWD, $strUsername . ':' . $strPassword);
curl_setopt($ch, CURLOPT_URL, $strBaseURI . $strResourcePath);
curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Accept: ' . $strResponseFormat, 'Content-type: ' . $strRequestFormat));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
// Make the request and output the response.
echo curl_exec($ch);
 
// Close the cURL resource
curl_close($ch);
?>

Example 3: add a new contact to the contacts collection resource

<?php
// Set some universal configuration variables. These will be
// the same for all your API requests for a particular account.
$strUsername = 'YOUR_API_USERNAME';
$strPassword = 'YOUR_API_PASSWORD';
$strBaseURI = 'http://api.rezora.com/v1/';
 
// Set some configuration variables specific to the task at hand.
// In this example, we'll be posting data to the contacts collection
// resource to create a new contact. We'll send data in JSON format,
// and retrieve data in XML format.
$strResourcePath = 'contacts';
$strRequestFormat = 'application/json';
$strResponseFormat = 'application/xml';
$strData = '{
 "data":{
  "contact_name":"Jane Smith",
  "contact_email":"jane.smith@company.com",
  "contact_type":"Active",
  "contact_title":"Consultant",
  "contact_company":"Acme Consulting",
  "contact_phone_home":"303-555-1234",
  "contact_phone_work":"303-555-1235",
  "contact_phone_mobile":"303-555-1236",
  "contact_phone_fax":"303-555-1237",
  "contact_business_street":"123 Church Street",
  "contact_business_street2":"Suite 12",
  "contact_business_city":"Shady Acres",
  "contact_business_state":"CO",
  "contact_business_zip":"12345",
  "contact_home_street":"124 Church Street",
  "contact_home_street2":"",
  "contact_home_city":"Shady Acres",
  "contact_home_state":"CO",
  "contact_home_zip":"12345"
 }
}
';
 
// Open a cURL resource
$ch = curl_init();
 
// Set the necessary parameters
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_USERPWD, $strUsername . ':' . $strPassword);
curl_setopt($ch, CURLOPT_URL, $strBaseURI . $strResourcePath);
curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Accept: ' . $strResponseFormat, 'Content-type: ' . $strRequestFormat));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
// Make this a POST request
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $strData);
 
// Make the request and output the response.
echo curl_exec($ch);
 
// Close the cURL resource
curl_close($ch);
?>

Example 4: update an individual contact from the contacts/ID resource

<?php
// Set some universal configuration variables. These will be
// the same for all your API requests for a particular account.
$strUsername = 'YOUR_API_USERNAME';
$strPassword = 'YOUR_API_PASSWORD';
$strBaseURI = 'http://api.rezora.com/v1/';
 
// Set some configuration variables specific to the task at hand.
// In this example, we'll be updating data for an existing contact resource.
// We'll send data in XML format, and retrieve data in CSV format.
$strResourcePath = 'contacts/123456';
$strRequestFormat = 'application/xml';
$strResponseFormat = 'text/csv';
$strData = '<?xml version="1.0" encoding="UTF-8"?>
<data>
 <contact_name>Jane Smith-Johnson</contact_name>
 <contact_email>jane.smith.johnson@company.com</contact_email>
 <contact_type>Active</contact_type>
 <contact_title>Consultant</contact_title>
 <contact_company>Acme Consulting</contact_company>
 <contact_phone_home>303-555-1234</contact_phone_home>
 <contact_phone_work>303-555-1235</contact_phone_work>
 <contact_phone_mobile>303-555-1236</contact_phone_mobile>
 <contact_phone_fax>303-555-1237</contact_phone_fax>
 <contact_business_street>123 Church Street</contact_business_street>
 <contact_business_street2>Suite 12</contact_business_street2>
 <contact_business_city>Shady Acres</contact_business_city>
 <contact_business_state>CO</contact_business_state>
 <contact_business_zip>12345</contact_business_zip>
 <contact_home_street>124 Church Street</contact_home_street>
 <contact_home_street2></contact_home_street2>
 <contact_home_city>Shady Acres</contact_home_city>
 <contact_home_state>CO</contact_home_state>
 <contact_home_zip>12345</contact_home_zip>
</data>
';
 
// Open a cURL resource
$ch = curl_init();
 
// Set the necessary parameters
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_USERPWD, $strUsername . ':' . $strPassword);
curl_setopt($ch, CURLOPT_URL, $strBaseURI . $strResourcePath);
curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Accept: ' . $strResponseFormat, 'Content-type: ' . $strRequestFormat));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
// PUT is originally intended for uploading files,
// so we have to make our data look like a file.
// Open a file resource using the php://memory stream
$fh = fopen('php://memory', 'rw');
 
// Write the data to the file
fwrite($fh, $strData);
 
// Move back to the beginning of the file
rewind($fh);
 
// Make this a PUT request
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_INFILE, $fh);
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($strData));
 
// Make the request and output the response.
echo curl_exec($ch);
 
// Close the file resource
fclose($fh);
 
// Close the cURL resource
curl_close($ch);
?>

Example 5: delete all contacts from the contacts collection resource

<?php
// Set some universal configuration variables. These will be
// the same for all your API requests for a particular account.
$strUsername = 'YOUR_API_USERNAME';
$strPassword = 'YOUR_API_PASSWORD';
$strBaseURI = 'http://api.rezora.com/v1/';
 
// Set some configuration variables specific to the task at hand.
// In this example, we'll be deleting data from the contacts collection
// resource. We'll retrieve the data in JSON format.
$strResourcePath = 'contacts';
$strRequestFormat = 'text/plain';
$strResponseFormat = 'application/json';
 
// Open a cURL resource
$ch = curl_init();
 
// Set the necessary parameters
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_USERPWD, $strUsername . ':' . $strPassword);
curl_setopt($ch, CURLOPT_URL, $strBaseURI . $strResourcePath);
curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Accept: ' . $strResponseFormat, 'Content-type: ' . $strRequestFormat));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
// Make this a DELETE request
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
 
// Make the request and output the response.
echo curl_exec($ch);
 
// Close the cURL resource
curl_close($ch);
?>
Personal tools