Chatwee PHP SDK

Chatwee PHP SDK is a library that allows you to easily integrate your PHP-based website with the Chatwee app. It provides several convenient methods covering all the logic necessary to interact with Chatwee HTTP API. The list below presents some of the essential methods that a developer may come across, as well as their sample usage.

Setup

To use Chatwee PHP SDK you need to download it first (click here to download the PHP SDK for Chatwee). Next, you have to include it in your project. In order to do that, just add the following line to your code.


<?php
  require_once(dirname( __FILE__ ) . "/ChatweeV2_SDK/Chatwee.php");
?>
	

Before you can perform any of the Chatwee API operations, you have to set up your credentials. You can find them in your Chatwee Dashboard in the INTEGRATION section of the Customize tab.


<?php
  ChatweeV2_Configuration::setChatId(YOUR_PERSONAL_CHAT_ID);
  ChatweeV2_Configuration::setClientKey(YOUR_PERSONAL_CLIENT_KEY);
?>
	

ChatweeV2_SsoManager class

This class handles the entire logic needed to use the Chatwee Single Sign-on feature. Use it if you need the standard SSO solution for your website. It covers all the methods necessary to manage your users and their sessions. Stick to the methods included in this class, unless you need advanced SSO implementation.

ChatweeV2_SsoManager::registerUser

Registers a new Chatwee user. This method returns the ID of a newly created user. The returned value needs to be stored somewhere (for instance, in your database) if you want to perform any actions related to this user in the future.


<?php

  require_once(dirname( __FILE__ ) . "/ChatweeV2_SDK/Chatwee.php");

  ChatweeV2_Configuration::setChatId(YOUR_PERSONAL_CHAT_ID);
  ChatweeV2_Configuration::setClientKey(YOUR_PERSONAL_CLIENT_KEY);

  try {
    $userId = ChatweeV2_SsoManager::registerUser(Array(
      "login" => "Test user",
      "avatar" => "http://mypage.com/avatar.png"
    ));
    echo "A new user has been registered with the following user ID: " . $userId;
  } catch(Exception $exception) {
    echo "An error occured: " . $exception->getMessage();
  }
?>
	
Name Description Type Mandatory Remarks
login user login string yes -
avatar user avatar string no an absolute URL of the user's avatar image

ChatweeV2_SsoManager::loginUser

Logs in the user identified by the userId parameter to Chatwee. Call this method whenever user logs into your service.


<?php

  require_once(dirname( __FILE__ ) . "/ChatweeV2_SDK/Chatwee.php");

  ChatweeV2_Configuration::setChatId(YOUR_PERSONAL_CHAT_ID);
  ChatweeV2_Configuration::setClientKey(YOUR_PERSONAL_CLIENT_KEY);

  try {
    $sessionId = ChatweeV2_SsoManager::loginUser(Array(
      "userId" => "507f191e810c19729de860ea"
    ));
    echo "The user has been logged in with the following session ID: " . $sessionId;
  } catch(Exception $exception) {
    echo "An error occured: " . $exception->getMessage();
  }
?>
	
Name Description Type Mandatory Remarks
userId ID of the user string yes the value obtained from the ChatweeV2_SsoManager::registerUser method

ChatweeV2_SsoManager::logoutUser

Logs out the user from Chatwee. Call this method whenever user logs out from your service.


<?php

  require_once(dirname( __FILE__ ) . "/ChatweeV2_SDK/Chatwee.php");

  ChatweeV2_Configuration::setChatId(YOUR_PERSONAL_CHAT_ID);
  ChatweeV2_Configuration::setClientKey(YOUR_PERSONAL_CLIENT_KEY);

  try {
    ChatweeV2_SsoManager::logoutUser();
    echo "The user has been logged out";
  } catch(Exception $exception) {
    echo "An error occured: " . $exception->getMessage();
  }
?>
	
This method takes no parameters.

ChatweeV2_SsoManager::editUser

This method modifies the properties of the user identified by the userId parameter. Call this method whenever your user's profile changes.


<?php

  require_once(dirname( __FILE__ ) . "/ChatweeV2_SDK/Chatwee.php");

  ChatweeV2_Configuration::setChatId(YOUR_PERSONAL_CHAT_ID);
  ChatweeV2_Configuration::setClientKey(YOUR_PERSONAL_CLIENT_KEY);

  try {
    ChatweeV2_SsoManager::editUser(Array(
      "userId" => "507f191e810c19729de860ea"
      "login" => "New user name",
      "avatar" => "http://mypage.com/new_avatar.png"
    ));
    echo "The user has been edited";
  } catch(Exception $exception) {
    echo "An error occured: " . $exception->getMessage();
  }
?>
	
Name Description Type Mandatory Remarks
userId ID of the user string yes -
login user login string yes -
avatar user avatar string no an absolute URL of the user's avatar image

ChatweeV2_SsoUser class

ChatweeV2_SsoUser represents the /sso-user part of Chatwee API. It comes with all the methods required for using the Single Sign-on mechanism. If you are not sure how to use Chatwee API, use ChatweeV2_SsoManager instead.

ChatweeV2_SsoUser::register

Calls the /sso-user/register Chatwee API method. It returns the ID of a newly created user.


<?php

  require_once(dirname( __FILE__ ) . "/ChatweeV2_SDK/Chatwee.php");

  ChatweeV2_Configuration::setChatId(YOUR_PERSONAL_CHAT_ID);
  ChatweeV2_Configuration::setClientKey(YOUR_PERSONAL_CLIENT_KEY);

  try {
    $userId = ChatweeV2_SsoUser::register(Array(
      "login" => "Test user",
      "avatar" => "http://mypage.com/avatar.png"
    ));
    echo "A new user has been registered with the following user ID: " . $userId;
  } catch(Exception $exception) {
    echo "An error occured: " . $exception->getMessage();
  }
?>
	
Name Description Type Mandatory Remarks
login user login string yes -
avatar user avatar string no an absolute URL of the user's avatar image

ChatweeV2_SsoUser::login

Calls the /sso-user/login Chatwee API method. It returns the ID of a newly created session.


<?php

  require_once(dirname( __FILE__ ) . "/ChatweeV2_SDK/Chatwee.php");

  ChatweeV2_Configuration::setChatId(YOUR_PERSONAL_CHAT_ID);
  ChatweeV2_Configuration::setClientKey(YOUR_PERSONAL_CLIENT_KEY);

  try {
    $sessionId = ChatweeV2_SsoUser::login(Array(
      "userId" => "507f191e810c19729de860ea"
    ));
    echo "The user has been logged in with the following session ID: " . $sessionId;
  } catch(Exception $exception) {
    echo "An error occured: " . $exception->getMessage();
  }
?>
	
Name Description Type Mandatory Remarks
userId ID of the user string yes the value obtained from the ChatweeV2_SsoUser::register method

ChatweeV2_SsoUser::logout

Calls the /sso-user/logout Chatwee API method. It terminates all backend sessions belonging to the user, identified by the particular user ID.


<?php

  require_once(dirname( __FILE__ ) . "/ChatweeV2_SDK/Chatwee.php");

  ChatweeV2_Configuration::setChatId(YOUR_PERSONAL_CHAT_ID);
  ChatweeV2_Configuration::setClientKey(YOUR_PERSONAL_CLIENT_KEY);

  try {
    ChatweeV2_SsoUser::logout(Array(
      "userId" => "507f191e810c19729de860ea",
    ));
    echo "The user has been logged out";
  } catch(Exception $exception) {
    echo "An error occured: " . $exception->getMessage();
  }
?>
	
Name Description Type Mandatory Remarks
userId ID of the user string yes the value obtained from the ChatweeV2_SsoUser::register method

ChatweeV2_SsoUser::removeSession

Calls the /sso-user/remove-session Chatwee API method. It terminates all backend sessions belonging to the user, identified by the particular user ID.


<?php

  require_once(dirname( __FILE__ ) . "/ChatweeV2_SDK/Chatwee.php");

  ChatweeV2_Configuration::setChatId(YOUR_PERSONAL_CHAT_ID);
  ChatweeV2_Configuration::setClientKey(YOUR_PERSONAL_CLIENT_KEY);

  try {
    ChatweeV2_SsoUser::removeSession(Array(
      "sessionId" => "3d14222e14addf548a12ffad",
    ));
    echo "The user session has been removed";
  } catch(Exception $exception) {
    echo "An error occured: " . $exception->getMessage();
  }
?>
	
Name Description Type Mandatory Remarks
sessionId ID of the session string yes the value obtained from the ChatweeV2_SsoUser::login method

ChatweeV2_SsoUser::edit

Calls the /sso-user/edit Chatwee API method. It modifies the user identified by the userId parameter.


<?php

  require_once(dirname( __FILE__ ) . "/ChatweeV2_SDK/Chatwee.php");

  ChatweeV2_Configuration::setChatId(YOUR_PERSONAL_CHAT_ID);
  ChatweeV2_Configuration::setClientKey(YOUR_PERSONAL_CLIENT_KEY);

  try {
    ChatweeV2_SsoUser::edit(Array(
      "userId" => "507f191e810c19729de860ea",
      "login" => "Test user",
      "avatar" => "http://mypage.com/avatar.png"
    ));
    echo "The user has been edited";
  } catch(Exception $exception) {
    echo "An error occured: " . $exception->getMessage();
  }
?>
	
Name Description Type Mandatory Remarks
userId ID of the user string yes -
login user login string yes -
avatar user avatar string no an absolute URL of the user's avatar image

ChatweeV2_Session class

This class is used to manage the Chatwee client session.

ChatweeV2_Session::isSessionSet

Determines if a Chatwee client session has been set.


<?php

  require_once(dirname( __FILE__ ) . "/ChatweeV2_SDK/Chatwee.php");

  ChatweeV2_Configuration::setChatId(YOUR_PERSONAL_CHAT_ID);
  ChatweeV2_Configuration::setClientKey(YOUR_PERSONAL_CLIENT_KEY);

  try {
    if(ChatweeV2_Session::isSessionSet() === true) {
    	echo "The Chatwee client session is set";
    }
  } catch(Exception $exception) {
    echo "An error occured: " . $exception->getMessage();
  }
?>
	
This method takes no parameters.

ChatweeV2_Session::setSessionId

Sets the Chatwee client session. It requires the session ID, which can be obtained with the ChatweeV2_SsoUser::login method.


<?php

  require_once(dirname( __FILE__ ) . "/ChatweeV2_SDK/Chatwee.php");

  ChatweeV2_Configuration::setChatId(YOUR_PERSONAL_CHAT_ID);
  ChatweeV2_Configuration::setClientKey(YOUR_PERSONAL_CLIENT_KEY);

  try {
    ChatweeV2_Session::setSessionId($sessionId);
    echo "The session ID has been set";
  } catch(Exception $exception) {
    echo "An error occured: " . $exception->getMessage();
  }
?>
	
Name Description Type Mandatory Remarks
sessionId ID of the session string yes the value obtained from the ChatweeV2_SsoUser::login method

ChatweeV2_Session::getSessionId

Gets the Chatwee client session.


<?php

  require_once(dirname( __FILE__ ) . "/ChatweeV2_SDK/Chatwee.php");

  ChatweeV2_Configuration::setChatId(YOUR_PERSONAL_CHAT_ID);
  ChatweeV2_Configuration::setClientKey(YOUR_PERSONAL_CLIENT_KEY);

  try {
    $sessionId = ChatweeV2_Session::getSessionId();
    echo "The current session ID is: " . $sessionId;
  } catch(Exception $exception) {
    echo "An error occured: " . $exception->getMessage();
  }
?>
	
This method takes no parameters.

ChatweeV2_Session::clearSessionId

Terminates the Chatwee client session.


<?php

  require_once(dirname( __FILE__ ) . "/ChatweeV2_SDK/Chatwee.php");

  ChatweeV2_Configuration::setChatId(YOUR_PERSONAL_CHAT_ID);
  ChatweeV2_Configuration::setClientKey(YOUR_PERSONAL_CLIENT_KEY);

  try {
    ChatweeV2_Session::clearSessionId();
    echo "Chatwee cient session has been removed";
  } catch(Exception $exception) {
    echo "An error occured: " . $exception->getMessage();
  }
?>
	
This method takes no parameters.