Home  >  Article  >  PHP Framework  >  How to use Tencent Cloud IM for real-time communication operations in ThinkPHP6?

How to use Tencent Cloud IM for real-time communication operations in ThinkPHP6?

WBOY
WBOYOriginal
2023-06-12 08:03:111719browse

With the rapid development of the Internet and mobile Internet, real-time communication technology has become an indispensable part of many applications. As one of the leading real-time communication platforms in China, Tencent Cloud IM (i.e. Instant Messaging IM) has also been widely recognized for its functions and performance. This article will introduce how to use Tencent Cloud IM in the ThinkPHP6 framework for real-time communication operations.

1. Apply for a Tencent Cloud IM account

First, you need to go to the Tencent Cloud official website (https://cloud.tencent.com/product/im) to register and apply for IM services. After the registration is completed, create an application in the console and obtain the corresponding SDKAppID, Identifier and SecretKey. This information will be used in subsequent configuration.

2. Install the official IM SDK

Tencent Cloud provides IM SDK in multiple languages ​​for developers to use. This article mainly introduces the PHP version of IM SDK. Enter the official GitHub repository (https://github.com/tencentyun/TIMServer/tree/master/examples/php) to download the latest php-sdk-v4.

In the ThinkPHP6 application directory, install the ImSDK package through composer and execute the command: composer require tencentyun/php-sdk-v4:latest to realize automatic loading of IM SDK.

3. Configure IM SDK

In the ThinkPHP6 project configuration file (default is config/app.php), add the following configuration items:

'imsdk' => [
    'sdk_app_id'   => 'SDKAppID',   // 应用ID
    'identifier'   => 'Identifier', // 用户标识
    'exp_time'     => 86400,        // 身份凭证有效时间(单位:秒)
    'private_key'  => 'SecretKey',  // 应用密钥
    'public_key'   => 'PublicKey',  // 公钥,非必填项
    'http_scheme'  => 'https',      // HTTPS协议
    'account_type' => '1',          // 账号类型,非必填项
],

Among them, sdk_app_id is the application ID obtained when applying for IM service, identifier is the user's identification in the application, exp_time is the validity time of the identity certificate, private_key is the application key obtained when applying for IM service, http_scheme is the communication protocol, account_type is the account type (default is 1).

4. Integrate IM SDK

In ThinkPHP6, you can use the service container to integrate IM SDK. First, you need to create a Service directory in the project root directory and create a new IMService class in this directory. The code of this class is as follows:

<?php
namespace appservice;

use IlluminateSupportFacadesLog;
use TencentyunTIMTIMSdk;

class IMService
{
    private $sdk;

    public function __construct()
    {
        $sdk = new TIMSdk(config('imsdk.sdk_app_id'), config('imsdk.identifier'));
        $sdk->setPrivateKey(config('imsdk.private_key'));
        $sdk->setPublickey(config('imsdk.public_key', ''));
        $sdk->setExpire(config('imsdk.exp_time'));
        $sdk->setAccountType(config('imsdk.account_type', '1'));
        $sdk->setHttpScheme(config('imsdk.http_scheme', 'https'));
        $this->sdk = $sdk;
    }

    public function createGroup($name)
    {
        $group = $this->sdk->getGroup();
        $data = [
            'Type' => 'Public',   // 群组类型(Public:公开群)
            'Name' => $name,      // 群组名称
        ];
        return $group->create($data);
    }
}

This class mainly implements the creation of an IM service instance and encapsulates some IM operations, such as creating groups.

Next, create an im.php file in the config/ directory to set the binding of the service container. The code is as follows:

<?php
use appserviceIMService;
return [
    'im' => IMService::class,
];

This code connects the IMService class to the service container The name im is bound to.

Finally, where the IM service needs to be used, such as in the controller, the bound service can be used through dependency injection, as follows:

<?php
namespace appcontroller;

use appserviceIMService;
use thinkacadeRequest;

class Index
{
    public function index(IMService $im)
    {
        $groupName = Request::param('groupName');
        $result = $im->createGroup($groupName);
        if ($result['ErrorCode'] > 0) {
            return json([
                'code' => 0,
                'msg'  => $result['ErrorInfo'],
            ]);
        }
        return json([
            'code' => 1,
            'msg'  => '创建群组成功',
            'data' => [
                'groupId' => $result['GroupId'],
            ],
        ]);
    }
}

The above code injects the IMService service through dependency injection, in The controller uses this service to create a Tencent Cloud IM group and returns the group ID. Other IM operations can be performed as needed.

5. Conclusion

This article introduces how to use Tencent Cloud IM in ThinkPHP6 for real-time communication operations. Through configuration, integration and dependency injection, you can easily use Tencent Cloud IM to perform various real-time communication operations, such as creating groups, sending messages, etc. In addition, more IM functions can be developed according to specific needs, such as instant messaging, video calls, etc.

The above is the detailed content of How to use Tencent Cloud IM for real-time communication operations in ThinkPHP6?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn