ÔõÑùÔÚThinkPHP6ÖÐʹÓÃÌÚѶÔÆIM¾ÙÐÐʵʱͨѶ²Ù×÷£¿
Ëæ×Å»¥ÁªÍøºÍÒƶ¯»¥ÁªÍøµÄ¿ìËÙÉú³¤£¬ÊµÊ±Í¨Ñ¶ÊÖÒÕÒѾ³ÉΪÁËÐí¶àÓ¦ÓÃÖв»¿É»òȱµÄÒ»»·¡£¶øÌÚѶÔÆim£¨¼´¼´Ê±Í¨Ñ¶im£©×÷Ϊº£ÄÚÁìÏȵÄʵʱͨѶƽ̨֮һ£¬Æ书ЧºÍÐÔÄÜÒ²»ñµÃÁËÆÕ±éµÄÈÏ¿É¡£±¾ÎĽ«ÏÈÈÝÔõÑùÔÚthinkphp6¿ò¼ÜÖÐʹÓÃÌÚѶÔÆim¾ÙÐÐʵʱͨѶ²Ù×÷¡£
Ò»¡¢ÉêÇëÌÚѶÔÆIMÕ˺Å
Ê×ÏÈ£¬ÐèÒªµ½ÌÚѶÔƹÙÍø£¨https://cloud.tencent.com/product/im£©¾ÙÐÐ×¢²áºÍÉêÇëIMЧÀÍ¡£×¢²áÍê³Éºó£¬µ½¿ØÖÆ̨Öн¨ÉèÓ¦Óò¢»ñÈ¡¶ÔÓ¦µÄSDKAppID¡¢IdentifierºÍSecretKey£¬ÕâЩÐÅÏ¢½«ÔÚºóÐøÉèÖÃÖÐÓõ½¡£
¶þ¡¢×°Öùٷ½IM SDK
ÌÚѶÔÆÌṩÁ˶àÖÖÓïÑÔµÄIM SDK¹©¿ª·¢ÕßʹÓ㬱¾ÎÄÖ÷ÒªÏÈÈݵÄÊÇPHP°æ±¾µÄIM SDK¡£½øÈë¹Ù·½GitHub¿â£¨https://github.com/tencentyun/TIMServer/tree/master/examples/php£©ÏÂÔØ×îеÄphp-sdk-v4¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
ÔÚThinkPHP6Ó¦ÓÃĿ¼Ï£¬Í¨¹ýcomposer×°ÖÃImSDK°ü£¬Ö´ÐÐÏÂÁcomposer require tencentyun/php-sdk-v4:latest£¬ÒÔ´ËʵÏÖIM SDKµÄ×Ô¶¯ÔØÈë¡£
Èý¡¢ÉèÖÃIM SDK
ÔÚThinkPHP6µÄÏîÄ¿ÉèÖÃÎļþ£¨Ä¬ÒÔΪconfig/app.php£©ÖУ¬Ìí¼ÓÈçÏÂÉèÖÃÏ
'imsdk' => [ 'sdk_app_id' => 'SDKAppID', // Ó¦ÓÃID 'identifier' => 'Identifier', // Óû§±êʶ 'exp_time' => 86400, // Éí·Ýƾ֤ÓÐÓÃʱ¼ä£¨µ¥Î»£ºÃ룩 'private_key' => 'SecretKey', // Ó¦ÓÃÃÜÔ¿ 'public_key' => 'PublicKey', // ¹«Ô¿£¬·Ç±ØÌîÏî 'http_scheme' => 'https', // HTTPSÐÒé 'account_type' => '1', // Õ˺ÅÀàÐÍ£¬·Ç±ØÌîÏî ],
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬sdk_app_idΪÉêÇëIMЧÀÍʱ»ñµÃµÄÓ¦ÓÃID£¬identifierΪÓû§ÔÚÓ¦ÓÃÖеıêʶ£¬exp_timeΪÉí·Ýƾ֤µÄÓÐÓÃʱ¼ä£¬private_keyΪÉêÇëIMЧÀÍʱ»ñµÃµÄÓ¦ÓÃÃÜÔ¿£¬http_schemeΪͨѶÐÒ飬account_typeΪÕ˺ÅÀàÐÍ£¨Ä¬ÒÔΪ1£©¡£
ËÄ¡¢¼¯³ÉIM SDK
ÔÚThinkPHP6ÖпÉÒÔʹÓÃЧÀÍÈÝÆ÷À´¼¯³ÉIM SDK£¬Ê×ÏÈÐèÒªÔÚÏîÄ¿¸ùĿ¼Ï½¨ÉèÒ»¸öServiceĿ¼£¬ÔÚ¸ÃĿ¼ÏÂн¨Ò»¸öIMServiceÀà¡£¸ÃÀàµÄ´úÂëÈçÏ£º
<?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); } }
µÇ¼ºó¸´ÖÆ
Õâ¸öÀàÖ÷ҪʵÏÖÁ˽¨ÉèÒ»¸öIMЧÀÍʵÀý£¬²¢·â×°ÁËһЩIM²Ù×÷£¬ÀýÈ罨ÉèȺ×é¡£
½ÓÏÂÀ´£¬ÔÚconfig/Ŀ¼Ï½¨ÉèÒ»¸öim.phpÎļþ£¬ÓÃÓÚÉèÖÃЧÀÍÈÝÆ÷µÄ°ó¶¨£¬´úÂëÈçÏ£º
<?php use appserviceIMService; return [ 'im' => IMService::class, ];
µÇ¼ºó¸´ÖÆ
Õâ¶Î´úÂ뽫IMServiceÀàÓëЧÀÍÈÝÆ÷ÖеÄÃû³Æim¾ÙÐа󶨡£
×îºó£¬ÔÚÐèҪʹÓÃIMЧÀ͵ĵط½£¬ÀýÈç¿ØÖÆÆ÷ÖУ¬¿ÉÒÔͨ¹ýÒÀÀµ×¢ÈëʹÓÃÒѰ󶨵ÄЧÀÍ£¬ÈçÏ£º
<?php namespace appcontroller; use appserviceIMService; use thinkacadeRequest; 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'], ], ]); } }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëͨ¹ýÒÀÀµ×¢ÈëIMServiceЧÀÍ£¬ÔÚ¿ØÖÆÆ÷ÖÐʹÓøÃЧÀͽ¨ÉèÁËÒ»¸öÌÚѶÔÆIMµÄȺ×飬·µ»ØÁËȺ×éID¡£¿ÉÒÔƾ֤ÐèÒª¾ÙÐÐÆäËûIM²Ù×÷¡£
Îå¡¢½áÓï
±¾ÎÄÏÈÈÝÁËÔõÑùÔÚThinkPHP6ÖÐʹÓÃÌÚѶÔÆIM¾ÙÐÐʵʱͨѶ²Ù×÷¡£Í¨¹ýÉèÖᢼ¯³ÉºÍÒÀÀµ×¢È룬¿ÉÒÔÇáËɵØʹÓÃÌÚѶÔÆIM¾ÙÐÐÖÖÖÖʵʱͨѶ²Ù×÷£¬ÀýÈ罨ÉèȺ×é¡¢·¢ËÍÐÂÎŵȡ£±ðµÄ£¬»¹¿ÉÒÔƾ֤ÏêϸÐèÒª¾ÙÐиü¶àµÄIM¹¦Ð§¿ª·¢£¬ÀýÈçʵÏÖ¼´Ê±Í¨Ñ¶¡¢ÊÓƵͨ»°µÈ¡£
ÒÔÉϾÍÊÇÔõÑùÔÚThinkPHP6ÖÐʹÓÃÌÚѶÔÆIM¾ÙÐÐʵʱͨѶ²Ù×÷£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡