### [小程序实例:小程序客服发送图片信息的实现代码](https://www.prosaas.cn/article/17085) **Published:** 2018-08-21T09:19:36 **Author:** 5201314 **Excerpt:** 本篇文章给大家带来的内容是关于小程序实例:小程序客服发送图片信息的实现代码,有一定的参考价值,有需要的朋友可以… 本篇文章给大家带来的内容是关于小程序实例:小程序客服发送图片信息的实现代码,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。 /\*\* \* 发送客服消息接口 \*/ public function actionCustomService() { $data = file\_get\_contents('php://input'); $data = json\_decode($data,true); $qrcode\_url = Yii::$app->params\['gzh\_qrcode'\]; $openId = $data\['FromUserName'\]; $access\_token = self::getAccessToken(); $url = 'https://api.weixin.qq.com/cgi-bin/message/custom/send?access\_token='.$access\_token; $appid = Yii::$app->params\['miniAppid'\]; $media\_id = self::getRedis("wx\_material:$appid", 'media\_id'); if (!$media\_id) { $material = self::uploadImg($qrcode\_url,$access\_token); if ($material) { $redis = Yii::$app->redis; $redis->hmset("wx\_material:$appid", 'type', $material\['type'\], 'media\_id', $material\['media\_id'\], 'created\_at', $material\['created\_at'\]); $redis->expire("wx\_material:$appid", 7200); } $media\_id = $material\['media\_id'\]; } $data1 = \[ 'touser' => $openId, 'msgtype' => 'image', 'image' => \[ 'media\_id' => $media\_id, \], \]; $data1 = json\_encode($data1,JSON\_UNESCAPED\_UNICODE); $result = self::https\_request($url,$data1); echo 'success'; } //上传素材到微信 public static function uploadImg($img,$access\_token) { $url = 'https://api.weixin.qq.com/cgi-bin/media/upload?access\_token='.$access\_token.'&type=image'; $fileName = time() . '.jpg'; $source = file\_get\_contents($img); file\_put\_contents('./images/material/'.$fileName,$source); if (class\_exists('\\CURLFile')) { $josn = array('media' => new \\CURLFile(realpath("images/material/" . $fileName))); } else { $josn = array('media' => '@' . realpath("images/material/" . $fileName)); } unlink('./images/material/'.$fileName); $result = self::curl\_post($url,$josn); $result = json\_decode($result,true); return $result; } 下面的都是通用代码 //获取access\_token public static function getAccessToken() { $appid = Yii::$app->params\['miniAppid'\]; $appsecret = Yii::$app->params\['miniSecret'\]; $url = 'https://api.weixin.qq.com/cgi-bin/token?grant\_type=client\_credential&appid='.$appid.'&secret='.$appsecret; $result = self::httpGet($url); $access\_token = json\_decode($result,true)\['access\_token'\]; return $access\_token; } //get方法获取内容 private static function httpGet($url) { $curl = curl\_init(); curl\_setopt($curl, CURLOPT\_RETURNTRANSFER, true); curl\_setopt($curl, CURLOPT\_TIMEOUT, 500); curl\_setopt($curl, CURLOPT\_SSL\_VERIFYPEER, 2); curl\_setopt($curl, CURLOPT\_SSL\_VERIFYHOST, 2); curl\_setopt($curl, CURLOPT\_URL, $url); $res = curl\_exec($curl); curl\_close($curl); return $res; } //http请求 public static function https\_request($url,$data=null) { $curl = curl\_init(); curl\_setopt($curl,CURLOPT\_URL,$url); curl\_setopt($curl,CURLOPT\_SSL\_VERIFYPEER,FALSE); curl\_setopt($curl,CURLOPT\_SSL\_VERIFYHOST,FALSE); if(!empty($data)){ curl\_setopt($curl,CURLOPT\_POST,1); curl\_setopt($curl,CURLOPT\_POSTFIELDS,$data); } curl\_setopt($curl,CURLOPT\_RETURNTRANSFER,1); $output = curl\_exec($curl); curl\_close($curl); return $output; } 相关推荐: 小程序中授权相册的解决办法(附代码) 微信小程序实例:点击事件和长按事件的代码实现 以上就是小程序实例:小程序客服发送图片信息的实现代码的详细内容,更多请关注知企PROSAAS其它相关文章! **Categories:** 后端开发 ---