Tenim.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace app\common\library;
  3. use getusersig\getusersig;
  4. use tencentim\tencentim;
  5. class Tenim
  6. {
  7. /**
  8. * 发送消息给某人-接口调用
  9. */
  10. public function sendToUser() {
  11. $from_user = '2';// 发送者
  12. $to_user = '294';// 接收者
  13. $message = 'hello许犇';// 接收者
  14. if(!$from_user || !$to_user || !$message) $this->error("参数缺失!");
  15. $this->sendMessageToUser($from_user,$to_user,$message);
  16. }
  17. /**
  18. * 发送消息给某人
  19. */
  20. //https://console.tim.qq.com/v4/openim/sendmsg?sdkappid=88888888&identifier=admin&usersig=xxx&random=99999999&contenttype=json
  21. public function sendMessageToUser($from_user,$to_user,$message) {
  22. $random = rand(10000000,99999999);
  23. $usersig = $this->usersig("administrator");
  24. // 获取配置信息
  25. $config = config("tencent_im");
  26. $url = "https://console.tim.qq.com/v4/openim/sendmsg";
  27. $url .= "?sdkappid=".$config["sdkappid"];
  28. $url .= "&identifier=administrator";
  29. $url .= "&usersig=".$usersig;
  30. $url .= "&random=".$random;
  31. $url .= "&contenttype=json";
  32. $tencentObj = new tencentim($url);
  33. $data = [];
  34. $data["SyncOtherMachine"] = 1;
  35. $data["From_Account"] = (string)$from_user;
  36. $data["To_Account"] = (string)$to_user;
  37. $data["MsgRandom"] = rand(1000000,9999999);
  38. $data["MsgTimeStamp"] = time();
  39. $data["MsgBody"][] = [
  40. "MsgType" => "TIMTextElem",
  41. "MsgContent" => [
  42. "Text"=> $message
  43. ],
  44. ];
  45. $tencentObj->toSend($data);
  46. }
  47. /**
  48. * 获取usersig签名-具体操作
  49. */
  50. public function usersig($user_id) {
  51. // 获取配置信息
  52. $config = config("tencent_im");
  53. $usersigObj = new getusersig($config["sdkappid"],$config["key"]);
  54. $usersig = $usersigObj->genUserSig($user_id);
  55. return $usersig;
  56. }
  57. }