Code Examples

From API Documentation

(Difference between revisions)
Jump to: navigation, search
(Example 2: read the agents/ID resource)
(Undo revision 1191 by AngelSlayerL0AZ (Talk))
 
(18 intermediate revisions not shown)
Line 1: Line 1:
-
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 meant to be indicative of all API calls.
+
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.
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.
Line 8: Line 8:
// Set some universal configuration variables. These will be
// Set some universal configuration variables. These will be
// the same for all your API requests for a particular account.
// the same for all your API requests for a particular account.
-
$strUsername = 'YOUR_USERNAME';
+
$strUsername = 'YOUR_API_USERNAME';
-
$strPassword = 'YOUR_PASSWORD';
+
$strPassword = 'YOUR_API_PASSWORD';
$strBaseURI = 'http://api.rezora.com/v1/';
$strBaseURI = 'http://api.rezora.com/v1/';
// Set some configuration variables specific to the task at hand.
// Set some configuration variables specific to the task at hand.
// In this example, we'll be reading data from the agents collection
// In this example, we'll be reading data from the agents collection
-
// resource. We'll retrieve the data in text format.
+
// resource. We'll retrieve the data in CSV format.
$strResourcePath = 'agents';
$strResourcePath = 'agents';
$strRequestFormat = 'text/plain';
$strRequestFormat = 'text/plain';
-
$strResponseFormat = 'text/plain';
+
$strResponseFormat = 'text/csv';
// Open a cURL resource
// Open a cURL resource
Line 42: Line 42:
// Set some universal configuration variables. These will be
// Set some universal configuration variables. These will be
// the same for all your API requests for a particular account.
// the same for all your API requests for a particular account.
-
$strUsername = 'YOUR_USERNAME';
+
$strUsername = 'YOUR_API_USERNAME';
-
$strPassword = 'YOUR_PASSWORD';
+
$strPassword = 'YOUR_API_PASSWORD';
$strBaseURI = 'http://api.rezora.com/v1/';
$strBaseURI = 'http://api.rezora.com/v1/';
Line 55: Line 55:
$strResourcePath = 'agents/1234.xml';
$strResourcePath = 'agents/1234.xml';
$strRequestFormat = 'text/plain';
$strRequestFormat = 'text/plain';
-
$strResponseFormat = 'text/plain';
+
$strResponseFormat = 'text/csv';
// Open a cURL resource
// Open a cURL resource
Line 80: Line 80:
// Set some universal configuration variables. These will be
// Set some universal configuration variables. These will be
// the same for all your API requests for a particular account.
// the same for all your API requests for a particular account.
-
$strUsername = 'YOUR_USERNAME';
+
$strUsername = 'YOUR_API_USERNAME';
-
$strPassword = 'YOUR_PASSWORD';
+
$strPassword = 'YOUR_API_PASSWORD';
$strBaseURI = 'http://api.rezora.com/v1/';
$strBaseURI = 'http://api.rezora.com/v1/';
// Set some configuration variables specific to the task at hand.
// Set some configuration variables specific to the task at hand.
// In this example, we'll be posting data to the contacts collection
// In this example, we'll be posting data to the contacts collection
-
// resource to create a new contact. We'll post data in JSON format,
+
// resource to create a new contact. We'll send data in JSON format,
// and retrieve data in XML format.
// and retrieve data in XML format.
$strResourcePath = 'contacts';
$strResourcePath = 'contacts';
-
$strRequestFormat = 'application/xml';
+
$strRequestFormat = 'application/json';
-
$strResponseFormat = 'application/json';
+
$strResponseFormat = 'application/xml';
$strData = '{
$strData = '{
  "data":{
  "data":{
Line 138: Line 138:
</source>
</source>
-
== Example 4: delete all contacts from the ''contacts'' collection resource ==
+
== Example 4: update an individual contact from the ''contacts/ID'' resource ==
<source lang="php">
<source lang="php">
<?php
<?php
// Set some universal configuration variables. These will be
// Set some universal configuration variables. These will be
// the same for all your API requests for a particular account.
// the same for all your API requests for a particular account.
-
$strUsername = 'YOUR_USERNAME';
+
$strUsername = 'YOUR_API_USERNAME';
-
$strPassword = 'YOUR_PASSWORD';
+
$strPassword = 'YOUR_API_PASSWORD';
$strBaseURI = 'http://api.rezora.com/v1/';
$strBaseURI = 'http://api.rezora.com/v1/';
// Set some configuration variables specific to the task at hand.
// Set some configuration variables specific to the task at hand.
-
// In this example, we'll be posting data to the contacts collection
+
// In this example, we'll be updating data for an existing contact resource.
-
// resource to create a new contact. We'll post data in JSON format, and retrieve data in XML format.
+
// 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';
$strResourcePath = 'contacts';
$strRequestFormat = 'text/plain';
$strRequestFormat = 'text/plain';
-
$strResponseFormat = 'text/plain';
+
$strResponseFormat = 'application/json';
// Open a cURL resource
// Open a cURL resource

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