Advertisement
livechatinc

Chat ID integration - based on webhooks

Jul 18th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.90 KB | None | 0 0
  1. <?php
  2.  
  3. // read the webhook sent by LiveChat
  4. $data = file_get_contents('php://input');
  5. $data = json_decode($data);
  6.  
  7. // make sure the "chat_started" event occured
  8. if ($data->event_type === 'chat_started') {
  9.     $chat_id = $data->chat->id;
  10.  
  11.  
  12.     $fields = array();
  13.     $fields[] = (object)array(
  14.         'name' => 'Chat ID',
  15.         'value' => $chat_id
  16.     );
  17.     $curlFields = http_build_query(array(
  18.         'license_id' => $data->license_id,
  19.         'token' => $data->token,
  20.         'id' => 'chat-id-integration',
  21.         'fields' => $fields
  22.     ));
  23.     $ch = curl_init();
  24.     curl_setopt($ch, CURLOPT_URL, 'https://api.livechatinc.com/visitors/'.$data->visitor->id.'/details');
  25.     curl_setopt($ch, CURLOPT_POST, 1);
  26.     curl_setopt($ch, CURLOPT_POSTFIELDS, $curlFields);
  27.     curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-API-Version: 2'));
  28.     $result = curl_exec($ch);
  29.     curl_close($ch);
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement