123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- <?php
- namespace wx\miniprogram\msg;
- use ErrorException;
- use wx\Base;
- /**
- * 客服消息
- *
- * @method json getTempMedia() static 获取客服消息内的临时素材
- * @method json send() static 发送客服消息给用户
- * @method json setTyping() static 添加订阅消息模板
- * @method json uploadTempMedia() static 删除订阅消息模板
- */
- class CustomerServiceMessage extends Base
- {
- private $requestUrl = [
- 'send' => 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=',
- 'setTyping' => 'https://api.weixin.qq.com/cgi-bin/message/custom/typing?access_token=',
- 'uploadTempMedia' => 'https://api.weixin.qq.com/cgi-bin/media/upload?access_token='
- ];
- public function __call($method, $arguments)
- {
- if (!isset($this->requestUrl[$method])) {
- throw new ErrorException('Method ' . $method . ' not exists');
- }
- $num = count($arguments);
- if ($num != 2) {
- throw new ErrorException('Method ' . $method . ' needs two arguments. ' . $num . ' arguments given.');
- }
- $url = $this->requestUrl[$method] . $arguments[0];
- $data = $this->curl($url, json_encode($arguments[1], JSON_UNESCAPED_UNICODE));
- return $data;
- }
- public function uploadTempMedia($token, $data)
- {
- $url = $this->requestUrl['uploadTempMedia'] . $token.'&type=image';
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($curl, CURLOPT_HEADER, false);
- $data = curl_exec($curl);
- curl_close($curl);
- return $data;
- }
- }
|