Tenim.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. return true;
  23. $random = rand(10000000,99999999);
  24. $usersig = $this->usersig("administrator");
  25. // 获取配置信息
  26. $config = config("tencent_im");
  27. $url = "https://console.tim.qq.com/v4/openim/sendmsg";
  28. $url .= "?sdkappid=".$config["sdkappid"];
  29. $url .= "&identifier=administrator";
  30. $url .= "&usersig=".$usersig;
  31. $url .= "&random=".$random;
  32. $url .= "&contenttype=json";
  33. $tencentObj = new tencentim($url);
  34. $data = [];
  35. $data["SyncOtherMachine"] = 1;
  36. $data["From_Account"] = (string)$from_user;
  37. $data["To_Account"] = (string)$to_user;
  38. $data["MsgRandom"] = rand(1000000,9999999);
  39. $data["MsgTimeStamp"] = time();
  40. $data["MsgBody"][] = [
  41. "MsgType" => "TIMTextElem",
  42. "MsgContent" => [
  43. "Text"=> $message
  44. ],
  45. ];
  46. $tencentObj->toSend($data);
  47. }
  48. /**
  49. * 获取usersig签名-具体操作
  50. */
  51. public function usersig($user_id) {
  52. // 获取配置信息
  53. $config = config("tencent_im");
  54. $usersigObj = new getusersig($config["sdkappid"],$config["key"]);
  55. $usersig = $usersigObj->genUserSig($user_id);
  56. return $usersig;
  57. }
  58. }