API call to create conversation from PHP

Here is a short example how to call LiveAgent API and create a conversation with custom fields.
 

<?php

$service_url = 'https://localhost/LiveAgent/LiveAgent/server/api/index.php?handler=conversations';

$curl = curl_init($service_url);

//use custom field codes defined by administrator, otherwise they will not be visible in ticket detail (but still inserted to DB)

$arrcustomVars = array(array('code'=>'varsymbol', 'value'=>'43894209'), array('code'=>'anotherfield', 'value'=>'test value'));

 

$curl_post_data = array(

        'message' => 'test message',

        'useridentifier' => 'example@example.com',

        'department' => '38ff3d45', //department id

        'subject' => 'My first conversation',

        'recipient' => 'example@example.com',

        'customfields' => json_encode($arrcustomVars),

        'apikey' => '<<YOUR API KEY>>'

);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

curl_setopt($curl, CURLOPT_POST, true);

curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);

$curl_response = curl_exec($curl);

 

if ($curl_response === false) {

    $info = curl_getinfo($curl);

    curl_close($curl);

    die('error occured during curl exec. Additioanl info: ' . var_export($info));

}

curl_close($curl);

$decoded = json_decode($curl_response);

if (isset($decoded->response->status) && $decoded->response->status == 'ERROR') {

    die('error occured: ' . $decoded->response->errormessage);

}

?>