CustomerServiceMessage.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace wx\miniprogram\msg;
  3. use ErrorException;
  4. use wx\Base;
  5. /**
  6. * 客服消息
  7. *
  8. * @method json getTempMedia() static 获取客服消息内的临时素材
  9. * @method json send() static 发送客服消息给用户
  10. * @method json setTyping() static 添加订阅消息模板
  11. * @method json uploadTempMedia() static 删除订阅消息模板
  12. */
  13. class CustomerServiceMessage extends Base
  14. {
  15. private $requestUrl = [
  16. 'send' => 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=',
  17. 'setTyping' => 'https://api.weixin.qq.com/cgi-bin/message/custom/typing?access_token=',
  18. 'uploadTempMedia' => 'https://api.weixin.qq.com/cgi-bin/media/upload?access_token='
  19. ];
  20. public function __call($method, $arguments)
  21. {
  22. if (!isset($this->requestUrl[$method])) {
  23. throw new ErrorException('Method ' . $method . ' not exists');
  24. }
  25. $num = count($arguments);
  26. if ($num != 2) {
  27. throw new ErrorException('Method ' . $method . ' needs two arguments. ' . $num . ' arguments given.');
  28. }
  29. $url = $this->requestUrl[$method] . $arguments[0];
  30. $data = $this->curl($url, json_encode($arguments[1], JSON_UNESCAPED_UNICODE));
  31. return $data;
  32. }
  33. public function uploadTempMedia($token, $data)
  34. {
  35. $url = $this->requestUrl['uploadTempMedia'] . $token.'&type=image';
  36. $curl = curl_init();
  37. curl_setopt($curl, CURLOPT_URL, $url);
  38. curl_setopt($curl, CURLOPT_POST, 1);
  39. curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
  40. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  41. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  42. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  43. curl_setopt($curl, CURLOPT_HEADER, false);
  44. $data = curl_exec($curl);
  45. curl_close($curl);
  46. return $data;
  47. }
  48. }