Home > Uncategorized > Using @livechat #api to get car details for website visitors

Using @livechat #api to get car details for website visitors



Livechat is a great system for chatting live to your website visitors. But sometimes time can be wasted ascertaining pertinent details from the customer that could be looked up automatically as the chat starts.

In this example, a car parts company wanted to know exactly what type of car the person as soon as the chat started.- Using our API at http://www.regcheck.org.uk

So, the first thing to do is to set up a pre-chat survey (under settings), adding a Question saying “What is your registration number”


Once this is enabled, the chat window should appear as shown above. In this example, it’s important that the question is the third in the list, but you can modify the webhook code below to change that.

Next set up a webhook, under settings > integrations. Here it’s important to ensure that visitor and pre_chat_survey are clicked, and chat starts event is selected. The target url should point to your website, and a PHP file called livechathook.php


Now, here’s some code, for the file livechathook.php



$client = new soapclient(‘http://www.regcheck.org.uk/api/reg.asmx?WSDL ‘, array(‘trace’ => 1));
// Set this username to your RegCheck username
$username = ‘your-username’; // Replace this!

// read the webhook sent by LiveChat
$data = file_get_contents(‘php://input’);

file_put_contents(‘raw.txt’, $data);

try {
$json = json_decode($data);
$survey = $json->pre_chat_survey;
$question2 =$survey[2];
$answer2 =$question2->answer;
file_put_contents(‘reg.txt’, print_r($answer2, true));

// create an array of parameters
$param = array(
‘RegistrationNumber’ => $answer2,
‘username’ => $username);

$result = $client->Check($param);
$car = ‘Not found’;
file_put_contents(‘soapresponse.txt’, print_r($client->__getLastResponse(),true));
if (is_soap_fault($result)) {
} else {
$regJson = $result->CheckResult->vehicleJson;
$djson = json_decode($regJson);
$car = $djson->Description;

$fields = array();
$fields[] = (object)array(
‘name’ => ‘Car’,
‘value’ => $car

$curlFields = http_build_query(array(
‘license_id’ => $json->license_id,
‘token’ => $json->token,
‘id’ => ‘my-integration’,
‘fields’ => $fields

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, ‘https://api.livechatinc.com/visitors/ ‘ . $json->visitor->id . ‘/details’);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlFields);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘X-API-Version: 2’));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);

$CurlError = curl_error($ch);
file_put_contents(‘curlerror.txt’, print_r($CurlError, true));


file_put_contents(‘curl.txt’, print_r($result, true));

catch(Exception $e) {
file_put_contents(‘error.txt’, $e->getMessage());


The end result of this, is when you are chatting with a user, you now know the exact make and model of the car they are driving.

car lookup integration

It should be noted, that the above example assumes the user is from the UK, and you will have to change the $client->Check call to $client->CheckUSA or $client->CheckAustralia for other countries (* And you need to collect state information for USA and Australia too).

So, you might be interested to know how this works?

The webhook from LiveChatInc sends the following Json;

“name”:“John Mairs”,
“country”:“United Kingdom”,
“author_name”:“Fiach Reid”,
“text”:“Hello. How may I help you?”,


“name”:“Fiach Reid”,

“answer”:“John Mairs”
“label”:“What is your registration number”,

The parts of interest are the response to the question – the registration number, the token, license and visitor id are required to feed information back to LiveChatInc.

In the code above, you can see that the car registration number is stored in $answer2, and this is then sent to the regcheck webservice, to determine the car details. If this succeeds, then the car description is stored in $car – The $fields object is then created with the car details stored in an associative array, this is then sent back to the LiveChatInc API to be associated with the current visitor.

The code above contains quite a bit of logging, which can be used for debugging purposes, but should be removed before going live.

Raw.Txt – The Json sent to the Webhook
reg.txt – The Visitors registration number
soapresponse.txt – XML returned from the webservice
regerror.txt – Any error returned from the webservice
regdetails.txt – The data returned from the webservice
curlerror.txt – Any error in calling the API
curl.txt – The result of calling the API
error.txt – Any PHP errors.





Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: