定制 hedeqiang/ten-im 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

hedeqiang/ten-im

Composer 安装命令:

composer require hedeqiang/ten-im

包简介

腾讯云即时通信 SDK for PHP

README 文档

README

腾讯云 IM.

StyleCI build status Latest Stable Version Total Downloads Latest Unstable Version License

安装

$ composer require hedeqiang/ten-im -vvv

说明:本项目默认会引入 tencent/tls-sig-api-v2 扩展包,所以你不需要手动引入 tencent/tls-sig-api-v2,否则可能会造成依赖冲突

配置

使用本扩展前需要登录 即时通信 IM 控制台 创建应用,配置管理员、获取 app_id、Key 等关键信息

更多请查看并熟读 即时通信 IM 服务端API

REST API 接口列表

require __DIR__ .'/vendor/autoload.php';

use Hedeqiang\TenIM\IM;

$config = [
    'sdk_app_id' => '',
    'identifier' => '',
    'secret_key' => '',
    'region'     => 'zh' // zh、sgp、kr、ger、ind、usa
];

$im = new IM($config);

使用

其中 send 方法接收三个参数。第一个参数 $servicename : 内部服务名,不同的 servicename 对应不同的服务类型;第二个参数 $command:命令字,与 servicename 组合用来标识具体的业务功能;第三个参数为请求包主体

示例:v4/im_open_login_svc/account_import,其中 im_open_login_svcservicenameaccount_importcommand

请求包示例:

{
    "From_Account":"id",
    "ProfileItem":
    [
        {
            "Tag":"Tag_Profile_IM_Nick",
            "Value":"MyNickName"
        }
    ]
}

导入单个帐号

$params = [
    'Identifier' => "hedeqiang",
    'Nick'       => 'hedeqiang',
    'FaceUrl'    => '',
];

print_r($im->send('im_open_login_svc','account_import',$params));

获取用户在线状态

<?php
$params = [
    'To_Account' => ['hedeqiang']
];

print_r($im->send('openim','querystate',$params));

返回示例

{
	"ActionStatus": "OK",
	"ErrorInfo": "",
	"ErrorCode": 0,
	"QueryResult": [{
		"To_Account": "1",
		"State": "Offline"
	}]
}

设置资料

$params = [
    'From_Account' => 'hedeqiang',
        'ProfileItem' => [
            ['Tag' => 'Tag_Profile_IM_Nick', 'Value' => 'hedeqiang'],
            ['Tag' => 'Tag_Profile_IM_Gender', 'Value' => 'Gender_Type_Male'],
            ['Tag' => 'Tag_Profile_IM_BirthDay', 'Value' => 19940410],
            ['Tag' => 'Tag_Profile_IM_SelfSignature', 'Value' => '程序人生的寂静欢喜'],
            ['Tag' => 'Tag_Profile_IM_Image', 'Value' => 'https://upyun.laravelcode.cn/upload/avatar/1524205770e4fbfbff-86ae-3bf9-b7b8-e0e70ce14553.png'],
        ],
];

print_r($im->send('profile','portrait_set',$params));

返回示例:

{
	"ActionStatus": "OK",
	"ErrorCode": 0,
	"ErrorInfo": "",
	"ErrorDisplay": ""
}

单发单聊消息

$params = [
    'SyncOtherMachine' => 1, // 消息不同步至发送方
    'From_Account' => '1',
    'To_Account' => '2',
    'MsgRandom' => 1287657,
    'MsgTimeStamp' => 1557387418,
    'MsgBody' => [
        [
            'MsgType' => 'TIMTextElem',
            'MsgContent' => [
                'Text' => '晚上去撸串啊'
            ]
        ]
    ]
];

print_r($im->send('openim','sendmsg',$params));

返回示例:

{
    "ActionStatus":"OK",
    "ErrorInfo":"",
    "ErrorCode":0,
    "MsgTime":1573179125,
    "MsgKey":"748144182_1287657_1573179125"
}

在 Hyperf 中使用

发布配置文件

php bin/hyperf.php vendor:publish hedeqiang/ten-im
编写 .env 文件
SDK_APP_ID=
IDENTIFIER=
SECRET_KEY=
REGION=     // zh、sgp、kr、ger、ind、usa
使用
use Hedeqiang\TenIM\IM;
use Hyperf\Utils\ApplicationContext;

$response = ApplicationContext::getContainer()->get(IM::class)->send($servername,$command,$params);

在 Laravel 中使用

发布配置文件

php artisan vendor:publish --provider="Hedeqiang\TenIM\ServiceProvider"
编写 .env 文件
SDK_APP_ID=
IDENTIFIER=
SECRET_KEY=
REGION=     // zh、sgp、kr、ger、ind、usa

方法参数注入

use Hedeqiang\TenIM\IM;

public function index(IM $im)
{
    $params = [
        'SyncOtherMachine' => 1, // 消息不同步至发送方
        'From_Account' => '1',
        'To_Account' => '2',
        'MsgRandom' => 1287657,
        'MsgTimeStamp' => 1557387418,
        'MsgBody' => [
            [
                'MsgType' => 'TIMTextElem',
                'MsgContent' => [
                    'Text' => '晚上去撸串啊'
                ]
            ]
        ]
    ];
    $response = $im->send('openim','sendmsg',$params);
}

服务名访问

public function index()
{
    $params = [
        'SyncOtherMachine' => 1, // 消息不同步至发送方
        'From_Account' => '1',
        'To_Account' => '2',
        'MsgRandom' => 1287657,
        'MsgTimeStamp' => 1557387418,
        'MsgBody' => [
            [
                'MsgType' => 'TIMTextElem',
                'MsgContent' => [
                    'Text' => '晚上去撸串啊'
                ]
            ]
        ]
    ];
    $response = app('im')->send('openim','sendmsg',$params);
}

Facades 门面使用(可以提示)

use Hedeqiang\TenIM\Facades\IM;

$params = [
        'SyncOtherMachine' => 1, // 消息不同步至发送方
        'From_Account' => '1',
        'To_Account' => '2',
        'MsgRandom' => 1287657,
        'MsgTimeStamp' => 1557387418,
        'MsgBody' => [
            [
                'MsgType' => 'TIMTextElem',
                'MsgContent' => [
                    'Text' => '晚上去撸串啊'
                ]
            ]
        ]
    ];
$response = IM::im()->send('openim','sendmsg',$params);

更多用法请参考 REST API 接口列表

TODO

Contributing

You can contribute in one of three ways:

  1. File bug reports using the issue tracker.
  2. Answer questions or fix bugs on the issue tracker.
  3. Contribute new features or update the wiki.

The code contribution process is not very formal. You just need to make sure that you follow the PSR-0, PSR-1, and PSR-2 coding guidelines. Any new code contributions must be accompanied by unit tests where applicable.

License

MIT

hedeqiang/ten-im 适用场景与选型建议

hedeqiang/ten-im 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17.35k 次下载、GitHub Stars 达 36, 最近一次更新时间为 2019 年 11 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 「im」 「腾讯云」 「即时通信」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 hedeqiang/ten-im 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 hedeqiang/ten-im 我们能提供哪些服务?
定制开发 / 二次开发

基于 hedeqiang/ten-im 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

  • Stars: 36
  • Watchers: 2
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-11-08