LiveRoom.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace app\common\library\easywechat\wx;
  3. /**
  4. * 直播房间
  5. */
  6. class LiveRoom extends WxBase
  7. {
  8. /**
  9. * 同步小程序直播房间
  10. */
  11. public function syn()
  12. {
  13. $apiUrl = "https://api.weixin.qq.com/wxa/business/getliveinfo?access_token=%ACCESS_TOKEN%";
  14. $params = json_encode(['start' => 0, 'limit' => 100], JSON_UNESCAPED_UNICODE);
  15. return $this->generl($apiUrl, $params);
  16. }
  17. /**
  18. * 获取小程序直播房间播放记录
  19. */
  20. public function getReplay($roomId)
  21. {
  22. $apiUrl = "https://api.weixin.qq.com/wxa/business/getliveinfo?access_token=%ACCESS_TOKEN%";
  23. $params = json_encode(['action' => 'get_replay', 'start' => 0, 'limit' => 100, 'room_id' => $roomId], JSON_UNESCAPED_UNICODE);
  24. return $this->generl($apiUrl, $params);
  25. }
  26. public function generl($apiUrl, $params)
  27. {
  28. // 获取 access token 实例
  29. $accessToken = $this->app->access_token;
  30. $token = $accessToken->getToken();
  31. // // 微信接口url
  32. // $apiUrl = "https://api.weixin.qq.com/wxa/business/getliveinfo?access_token={$token['access_token']}";
  33. // // 请求参数
  34. // $params = json_encode(['start' => 0, 'limit' => 100], JSON_UNESCAPED_UNICODE);
  35. $apiUrl = str_replace('%ACCESS_TOKEN%', $token['access_token'], $apiUrl);
  36. // 执行请求
  37. $result = $this->post($apiUrl, $params);
  38. // 返回结果
  39. $response = $this->jsonDecode($result);
  40. if (!isset($response['errcode'])) {
  41. $this->error = '请求错误';
  42. return false;
  43. }
  44. if ($response['errcode'] != 0) {
  45. if($response['errcode'] == '9410000'){
  46. $this->error = 'empty';
  47. }else{
  48. if($response['errcode'] == 40001){
  49. //防止token过期或更换,重新获取
  50. $accessToken->getToken(true);
  51. }
  52. $this->error = $response['errmsg'];
  53. }
  54. return false;
  55. }
  56. return $response;
  57. }
  58. }