Export customers to CSV file

Let's imagine you want to export your contacts with their names and contact details to a csv file.
LiveAgent user interface currently doesn't offer the option to do this so here is a short example on how to export all contacts with their contact information using REST API calls:
 

<?php
//URL TO YOUR LIVEAGENT INSTALLATION WITH LAST TRAILING SLASH
const LIVEAGENT_API_URL = 'https://www.example.com/';

//YOUR API KEY YOU CAN FIND IN MENU Configuration -> System -> Api
const API_KEY = '21cbfad52e46dddaa7crhu91207a268b';

//open CSV file for writing
$fp = fopen('customers.csv', 'w');

// iterate through customer records
$offset=0;
while (true) {
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,LIVEAGENT_API_URL.'api/customers?&apikey='.API_KEY.'&limitfrom='.$offset.'&limitcount=200');
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    $curl_response=curl_exec($ch);
    if ($curl_response === false) {
        $info = curl_error($ch);
        curl_close($ch);
        die("error occurred during curl exec. Additional info: " . var_export($info));
        }
    curl_close($ch);
    /* process $curl_response here */
    $batch=json_decode($curl_response);
    
    if (isset($batch->response->errormessage) && ($batch->response->errormessage!='')) {
        die("error occurred: " . $batch->response->errormessage);
    }

    $batch=$batch->response->customers;
    
    foreach ($batch as $customer) {
        if ((stripos($customer->systemname,'Visitor')!==FALSE) || ($customer->systemname=='')) //skip unidentified visitors
                continue;
        
        //load all contact details of every identified customer.
        $ch = curl_init();
        curl_setopt($ch,CURLOPT_URL,LIVEAGENT_API_URL.'/api/customers/'.$customer->contactid.'?&apikey='.API_KEY);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
        $curl_response=curl_exec($ch);
        if ($curl_response === false) {
            $info = curl_error($ch);
            curl_close($ch);
            die("error occured during curl exec. Additional info: " . var_export($info));
            }
        curl_close($ch);
        /* process $curl_response here */
        $customer=json_decode($curl_response);
        $customer=$customer->response;
        $unique = "[";
        foreach ($customer->uniquefields as $uniquefield) {
            $unique .= "{" . $uniquefield->code.' - '.$uniquefield->value . "},";
        }
        
        $unique .= "]";
    
            fputcsv($fp, array(
                        $customer->firstname,
                        $customer->lastname,
                        $customer->email,
                        $customer->datecreated,
                        $unique
                ),';');
    }
    
    $offset+=200;
    
    if (count($batch) < 200)
        break;
}
fclose($fp);
?>

The more customers you have the longer this script will take to finish since it executes separate API call for fetching ALL the contact details of a customer. As you know in LiveAgent a customer can have multiple email addresses, or even phone numbers and if you want to get all these you need to execute separate call for every single returned customer. If you are fine with getting simply the names and first emails of customers then instead of the above code you can use this one:
 

<?php
//URL TO YOUR LIVEAGENT INSTALLATION WITH LAST TRAILING SLASH
const LIVEAGENT_API_URL = 'https://www.example.com/';

//YOUR API KEY YOU CAN FIND IN MENU Configuration -> System -> Api
const API_KEY = '21cbfad52e46dddaa7crhu91207a268b';

//open CSV file for writing
$fp = fopen('customers.csv', 'w');

// iterate through customer records
$offset=0;
while (true) {
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_URL,LIVEAGENT_API_URL.'api/customers?&apikey='.API_KEY.'&limitfrom='.$offset.'&limitcount=200');
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    $curl_response=curl_exec($ch);
    if ($curl_response === false) {
        $info = curl_error($ch);
        curl_close($ch);
        die("error occured during curl exec. Additional info: " . var_export($info));
        }
    curl_close($ch);
    /* process $curl_response here */
    $batch=json_decode($curl_response);
    $batch=$batch->response->customers;
    
    foreach ($batch as $customer) {
        if ($customer->email=='')) //skip customers without email address
                continue;
    
        fputcsv($fp, array(
            $customer->firstname,
            $customer->lastname,
            $customer->email,
            $customer->datecreated,
            ),';');
    }
    
    $offset+=200;
    
    if (count($batch) < 200)
        break;
}

fclose($fp);
?>

This is not by any means complete export solution, it is just an example on how to get customer information from LiveAgent. The way you will format those data is up to your imagination.

You can play with this example and e.g. fetch only information about customers created recently or store data in different file format (e.g. write it to PDF or XML file).

Ready to try LiveAgent?

It's free for the first 14 days! No credit card required.

Get Started
We work well with others...
Magento Joomla Wordpress Mailchimp
Contact us

support@ladesk.com

+1-800-811-6590 (Toll Free in USA & Canada)

+421 2 33 456 826 (European Union & Worldwide)

Quality Unit, LLC 616 Corporate Way, Suite 2-3278 Valley Cottage, NY 10989

Stay in touch
Blog Google+
© 2004-2015 QualityUnit.com, All rights reserved