daktela/daktela-v6-php-connector 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

daktela/daktela-v6-php-connector

最新稳定版本:2.3.1

Composer 安装命令:

composer require daktela/daktela-v6-php-connector

包简介

Enables connecting to Daktela V6 installation REST API from PHP code

README 文档

README

Daktela V6 PHP Connector is a library that enables your PHP application to connect to your Daktela V6 REST API. This connector requires you to have the Daktela Contact Centre application already purchased, installed, and ready for use. The Daktela Contact Centre is an application enabling all-in-one handling of all customer communication coming through various channels, for example calls, e-mails, web chats, SMS, or social media.

Installation

The recommended way to install is through Composer:

composer require daktela/daktela-v6-php-connector

Setup

The connector requires following prerequisites:

  • Instance URL in the form of https://URL/
  • Access token for each access to the Daktela V6 REST API based on required permissions

Usage

There are two ways you can use the Daktela V6 PHP Connector:

  1. By instantiating the connector instance - useful when calling API with one authentication credentials
  2. Using static access method - useful when switching access tokens and URL

1. Using instance of the connector

use Daktela\DaktelaV6\Client;
use Daktela\DaktelaV6\RequestFactory;

$instance = "https://mydaktela.daktela.com/";
$accessToken = "0b7cb37b6c2b96a4b68128b212c799056564e0f2";

$client = new Client($instance, $accessToken);
$request = RequestFactory::buildReadRequest("Users")
    ->addFilter("username", "eq", "admin");
$response = $client->execute($request);

2. Using static access methods

use Daktela\DaktelaV6\Client;
use Daktela\DaktelaV6\RequestFactory;

$instance = "https://mydaktela.daktela.com/";
$accessToken = "0b7cb37b6c2b96a4b68128b212c799056564e0f2";

$client = Client::getInstance($instance, $accessToken);
$request = RequestFactory::buildReadRequest("Users")
    ->addFilter("username", "eq", "admin");
$response = $client->execute($request);

Operations

The allowed operations serve for CRUD manipulation with objects. Each operation uses the builder pattern and corresponds to specific REST action.

Reading entities

In order to list all objects for specific entities use the execute() method:

$request = RequestFactory::buildReadRequest("CampaignsRecords")
    ->addFilter("created", "gte", "2020-11-01 00:00:00")
    ->addSort("created", "asc");
$response = $client->execute($request);

In order to get one specific object for entity use the RequestFactory::buildbuildReadSingleRequest() method or use the method setObjectName() passing the object unique name along with setRequestType(RequestType::TYPE_SINGLE):

$request = RequestFactory::buildReadSingleRequest("CampaignsRecords", "records_5fa299a48ab72834012563");

$request = RequestFactory::buildReadRequest("CampaignsRecords")
    ->setRequestType(ReadRequest::TYPE_SINGLE)
    ->setObjectName("records_5fa299a48ab72834012563");
$response = $client->execute($request);

If relation data should be read use the RequestFactory::buildbuildReadRelationRequest() method or use the methods setObjectName() and setRelation() passing the object unique name and relation name along with setRequestType(RequestType::TYPE_MULTIPLE):

$request = RequestFactory::buildReadRelationRequest("CampaignsRecords", "records_5fa299a48ab72834012563", "activities");

$request = RequestFactory::buildReadRequest("CampaignsRecords")
    ->setRequestType(ReadRequest::TYPE_MULTIPLE)
    ->setRelation("activities")
    ->setObjectName("records_5fa299a48ab72834012563");
$response = $client->execute($request);

Standard loading reads always entities of one page. For pagination use the setTake() and setSkip() methods.

$request = RequestFactory::buildReadRequest("CampaignsRecords")
    ->setTake(1000)
    ->setSkip(10);
$response = $client->execute($request);

If you don't want to handle pagination, use the following request type to read all records:

$request = RequestFactory::buildReadRequest("CampaignsRecords")
    ->setRequestType(ReadRequest::TYPE_ALL)
    ->addFilter("created", "gte", "2020-11-01 00:00:00")
    ->addSort("created", "asc");
$response = $client->execute($request);

You can use different methods for defining filters:

$request = RequestFactory::buildReadRequest("CampaignsRecords")
    ->addFilter("created", "gte", "2020-11-01 00:00:00")
    ->addFilterFromArray([
            ["field" => "edited", "operator" => "lte", "2020-11-30 23:59:59"],
            ["action", "eq", "0"]
        ])
    ->addSort("created", "asc");
$response = $client->execute($request);

Creating entities

$request = RequestFactory::buildCreateRequest("CampaignsRecords")
    ->addStringAttribute("number", "00420226211245")
    ->addIntAttribute("number", 0)
    ->addAttributes(["queue" => 3000]);
$response = $client->execute($request);

Updating entities

$request = RequestFactory::buildUpdateRequest("CampaignsRecords")
    ->setObjectName("records_5fa299a48ab72834012563")
    ->addStringAttribute("number", "00420226211245")
    ->addIntAttribute("number", 0)
    ->addAttributes(["queue" => 3000]);
$response = $client->execute($request);

Deleting entities

$request = RequestFactory::buildDeleteRequest("CampaignsRecords")
    ->setObjectName("records_5fa299a48ab72834012563");
$response = $client->execute($request);

Processing response

The response entity contains the parsed data returned by the REST API.

$response   =   $client->execute($request);
$data       =   $response->getData();
$total      =   $response->getTotal();
$errors     =   $response->getErrors();
$httpStatus =   $response->getHttpStatus();

Handling exceptions

In case of a problem with executing the request sent, an exception is usually thrown. All the exceptions are descendants of the \DaktelaV6\Exception\RequestException class. In case a sub-library throws any exception, this exception is caught and rethrown as a child of this library's class.

You can handle the response exception in standard way using the try-catch expression:

use Daktela\DaktelaV6\Exception\RequestException;

try {
    $response = $client->execute($request);
} catch(RequestException $ex) {
    //Exception handling
}

Authentication Methods

The connector supports two authentication methods for passing the access token to the Daktela V6 API:

1. Header-based Authentication (Default)

By default, the access token is sent via the X-AUTH-TOKEN HTTP header. This is the recommended method as it keeps the token out of URLs and logs.

use Daktela\DaktelaV6\Client;
use Daktela\DaktelaV6\Http\ApiCommunicator;

$client = new Client($instance, $accessToken);
// Token is automatically sent via X-AUTH-TOKEN header

2. Query Parameter Authentication

Alternatively, you can send the access token as a query parameter (accessToken). This method may be useful for compatibility with certain proxy configurations or firewall rules.

use Daktela\DaktelaV6\Client;
use Daktela\DaktelaV6\Http\ApiCommunicator;

$client = new Client($instance, $accessToken);
$client->getApiCommunicator()->setAuthenticationMethod(
    ApiCommunicator::AUTHENTICATION_METHOD_QUERY
);

To switch back to header-based authentication:

$client->getApiCommunicator()->setAuthenticationMethod(
    ApiCommunicator::AUTHENTICATION_METHOD_HEADER
);

Security Note: Header-based authentication is recommended for production use as it prevents the access token from appearing in server logs, browser history, and other places where URLs are typically recorded.

统计信息

  • 总下载量: 418.32k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2020-11-24