Eloqua will soon be exposing our web services in the form of REST Resources. Eloqua already provides a web services API, well suited for integrated development environments like Eclipse and Visual Studio. These IDEs provide toolkits that can generate service proxies, making web services easy to use. The problem is that web services can be a challenge to work with when we don't have these proxies. REST APIs offer a lightweight alternative to web services and are easy to use with languages like Ruby and PHP.

 

In this post, we'll walk through a simple example in PHP to demonstrate how easy it is to work with the REST API. The only prerequisites are PHP + cURL. The code sample invokes an HTTP GET request with basic authentication to retrieve a list of Emails from the Eloqua application. The use cases vary, but we’re essentially providing a hook for retrieving a list of Eloqua Emails and displaying their Content. In future posts, we’ll discuss how to manage the content and deliver the email using the REST API. For now, let's get started with a simple request to Eloqua's REST API :

 

First, you’ll need user credentials for the API

 

// Basic Auth API Credentials
$user = "CompanyName\User.Name";
$password = "xxxxxx";


 

Now let’s provide a function that given a URL endpoint will invoke an HTTP GET request decorated with basic HTTP authentication headers and returns the API response.

 

function get_request($user, $password, $url)
{
       // create the cURL resource
       $ch = curl_init();


       // set cURL options
       curl_setopt($ch, CURLOPT_URL, $url);


       // basic authentication
       curl_setopt($ch, CURLOPT_USERPWD, $user . ':' . $password);


       // execute request and retrieve the response
       $data = curl_exec($ch);


       // close resources and return the response
       curl_close($ch);


       return json_decode($data);
}


 

Now, let’s get some data...

 

// Search for Emails with name containing *Demand*
$emails = get_request($user, $password, "https://secure.eloqua.com/API/REST/1.0/assets/emails?search=*Demand*&page=1&count=50&depth=minimal");


 

And display the results

 

// Display the name and id of each email
foreach($emails['elements'] as $item)
{
       print ('Email:'. $item['name'] . $item['id']);
}


 

Invoke another get request to retrieve an individual Email

 

// Get an Email
$email = get_request($user, $password, "https://secure.eloqua.com/API/REST/1.0/assets/email/111?depth=complete");


 

Finally, let's output the body of the Email

 

// print the Body of the Email
print('Email HTML Body : ' . $email->htmlContent->htmlBody);


 

I hope this was a helpful introduction to the REST API and how easy it is to work with. We'll explore more of the REST API in future posts.

 

Thanks,

Fred