123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php
- namespace app\api\controller;
- use app\common\model\Config;
- use think\Db;
- use think\Request;
- use app\common\model\User as Users;
- use think\facade\Validate;
- use app\common\controller\Api;
- class Wx extends Api
- {
- //微信授权消息
- public function sendmessage(){
- if (isset($_GET['echostr'])) { //校验服务器地址URL
- self::valid();
- }
- $img = Db::name('config')->where('name','bot_img')->value('value');
- $img = str_replace('http://'.$_SERVER['SERVER_NAME'],'',$img);
- $postStr = file_get_contents('php://input'); //此处推荐使用file_get_contents('php://input')获取后台post过来的数据
- if (!empty($postStr) && is_string($postStr)) {
- $postArr = json_decode($postStr, true);
- if ($postArr['MsgType'] == 'event' && $postArr['Event'] == 'user_enter_tempsession') { //用户进入客服后马上回复,现在已失效,需要用户先发过消息
- $fromUsername = $postArr['FromUserName']; //发送者openid
- $imgurl = $img; //公众号二维码,相对路径,修改为自己的
- $media_id = self::getMediaId($imgurl); //获取图片消息的media_id
- $data = array(
- "touser" => $fromUsername,
- "msgtype" => "image",
- "image" => array("media_id" => $media_id)
- );
- $json = json_encode($data, JSON_UNESCAPED_UNICODE); //php5.4+
- self::requestAPI($json);
- }elseif ($postArr['MsgType'] !== 'event') { //用户发送其他内容,引导加客服
- $fromUsername = $postArr['FromUserName']; //发送者openid
- $imgurl = $img; //公众号二维码,相对路径,修改为自己的
- $media_id = self::getMediaId($imgurl); //获取图片消息的media_id
- $data = array(
- "touser" => $fromUsername,
- "msgtype" => "image",
- "image" => array("media_id" => $media_id)
- );
- $json = json_encode($data, JSON_UNESCAPED_UNICODE); //php5.4+
- self::requestAPI($json);
- }
- } else {
- echo "empty";
- exit;
- }
- }
- public static function valid()
- {
- $echoStr = $_GET["echostr"];
- if (self::checkSignature()) {
- header('content-type:text');
- echo $echoStr;
- exit;
- } else {
- echo $echoStr . '+++' . 'k8h5qu8znxxxxxxjazm76';
- exit;
- }
- }
- public static function checkSignature()
- {
- $signature = $_GET["signature"];
- $timestamp = $_GET["timestamp"];
- $nonce = $_GET["nonce"];
- $token = 'k8h5qu8znxxxxxxjazm76';
- $tmpArr = array($token, $timestamp, $nonce);
- sort($tmpArr, SORT_STRING);
- $tmpStr = implode($tmpArr);
- $tmpStr = sha1($tmpStr);
- if ($tmpStr == $signature) {
- return true;
- } else {
- return false;
- }
- }
- public static function requestAPI($json)
- {
- $access_token = self::get_accessToken();
- /*
- * POST发送https请求客服接口api
- */
- $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" . $access_token;
- //以'json'格式发送post的https请求
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
- if (!empty($json)) {
- curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
- }
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- //curl_setopt($curl, CURLOPT_HTTPHEADER, $headers );
- $output = curl_exec($curl);
- if (curl_errno($curl)) {
- echo 'Errno' . curl_error($curl);//捕抓异常
- }
- curl_close($curl);
- if ($output == 0) {
- echo 'success';
- exit;
- }
- }
- /* 调用微信api,获取access_token,有效期7200s*/
- public static function get_accessToken()
- {
- $appid = Config::get_values('wechat_appid');
- $secret = Config::get_values('wechat_appsecret');
- $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret.""; //替换成自己的小程序id和secret
- @$weixin = file_get_contents($url);
- @$jsondecode = json_decode($weixin);
- @$array = get_object_vars($jsondecode);
- $token = $array['access_token'];
- return $token;
- }
- //获取上传图片的media_id
- public static function getMediaId($imgurl)
- {
- $token = self::get_accessToken();
- $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token={$token}&type=image";
- // echo $url;
- $ch1 = curl_init();
- $timeout = 10;
- $real_path = "{$_SERVER['DOCUMENT_ROOT']}$imgurl";//自动转成图片文件绝对路径,如果图片发送失败,检查PHP的$_SERVER['DOCUMENT_ROOT'的配置
- // echo $real_path;
- $data = array("media" => new \CURLFile("{$real_path}"));//php5.6(含)以上版本使用此方法
- // print_r($data);
- curl_setopt($ch1, CURLOPT_URL, $url);
- curl_setopt($ch1, CURLOPT_POST, 1);
- curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch1, CURLOPT_CONNECTTIMEOUT, $timeout);
- curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($ch1, CURLOPT_POSTFIELDS, $data);
- $result = curl_exec($ch1);
- //print_r( $result);
- curl_close($ch1);
- if ($result) {
- $result = json_decode($result, true);
- return $result['media_id'];
- } else {
- return null;
- }
- }
- }
|