In a previous post, we introduced the REST API with a post on Getting Started with PHP. We recently added a PHP REST Client available here on Github.

In today's post, we'll demonstrate how to create and manage contacts using the PHP REST Client.

 

Let's start by including the client.

Please note that the client library is available here : fredsakr/eloqua-php-request · GitHub

 

<?php
// include the Eloqua REST client
include_once('lib/eloquaRequest.php');
       

 

Next, we'll define a class to represent the Contact record :

 

// define the Contact class
class Contact
{
        public $accountName;
        public $address1;
        public $address2;
        public $address3;
        public $businessPhone;
        public $city;
        public $country;
        public $emailAddress;
        public $firstName;
        public $id;
        public $lastName;
        public $isSubscribed;
        public $isBounceback;
        public $salesPerson;
        public $title;
}
       

 

We're almost ready to make our first request. First construct a new instance of the client, providing your Eloqua credentials :

 

$client = new EloquaRequest('site', 'user', 'password', 'https://secure.eloqua.com/API/REST/1.0');

 

Search Contacts

We'll invoke our first API request, to search for and retrieve a list of contacts :

 

// search for and retrieve a list contacts
$list = $client->get('/data/contacts?search=*@test.com&count=1&page=1&depth=complete');

     

 

Create Contact

The following sample creates a new contact record with Email Address : sample@test.com

 

// instantiate a new instance of the Contact class
$contact = new Contact();
$contact->firstName = 'Sample';
$contact->lastName = 'Last';
$contact->emailAddress = 'sample@test.com';
     

 

// invoke a POST request to create the contact

$response = $client->post('/data/contact', $contact);
     

 

// retrieve the ID of the new contact

$contactId = $response->id;
     

 

Update Contact

The following sample updates the contact's first name to : updated

 

// change the contact's first name
$contact->id = $contactId; 
$contact->firstName = 'updated';
    

 

// invoke a PUT request to update the contact       

$response = $client->put('/data/contact/' . $contactId, $contact);
    

 

Delete Contact

Finally, let's cleanup the record that we've created.

 

$client->delete('/data/contact/' . $contactId);
    

 

We hope that you find this helpful.

Please note that the REST API is not yet public and the code samples available here are my own work. I'll be glad to help support and fix issues in the code, but please understand that Eloqua will not be able to support this - as it is my own.

 

Thanks,
Fred