ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
``` <?php namespace Uzhu\Api\Controller; use Uzhu\Core\ULog\Logger; use Uzhu\Core\Weixin\WxXYX; class weixinKeFuController extends ApiController { private $encodingAesKey = "axm2PwFW9BpQtVBr14Ym1mXKMFpBJ7rzrcMqHnvNtjh"; private $access_token = ''; private $token = ''; private $wx; private $pc; public function init() { $this->wx = new WxXYX(); $this->access_token =$this->wx->getAccessToken(); } // 校验服务器地址URL public function checkAction() { $echostr = $this->GET->getString('echostr'); try{ if (!empty($echostr)) { $this->validAction(); } else { $this->sendCustomerMessageAction(); } } catch (\Exception $e) { $this->jsonFailed($e->getMessage()); } } public function validAction() { $echoStr = $this->GET->getString('echostr');; if ($this->checkSignatureAction()) { $logger = Logger::getInstance("wx_kefu"); $logger->info("2哈哈"); header('content-type:text'); echo $echoStr; $logger->info("微信发送的echostr".var_export($echoStr, true)); die; } else { echo $echoStr . '+++' . $this->token; die; } } //微信验证规则 private function checkSignatureAction() { $signature = $this->GET->getInt("signature"); $timestamp = $this->GET->getInt("timestamp"); $nonce = $this->GET->getInt("nonce"); $token = $this->token; $tmpArr = array ( $token, $timestamp, $nonce ); sort ( $tmpArr, SORT_STRING ); $tmpStr = implode ( $tmpArr ); $tmpStr = sha1 ( $tmpStr ); if ($tmpStr == $signature) { return true; } else { return false; } } public function customerTypingAction() { $url = "POST https://api.weixin.qq.com/cgi-bin/message/custom/typing?access_token={$this->access_token}"; $res = $this->wx->request($url, $data = null, $credit = null); $this->wx->request($url, $data); } //获取客服获取的临时素材,仅支持下载图片文件 public function getTempMediaAction() { $media_id = ''; $url = "https://api.weixin.qq.com/cgi-bin/media/get?access_token={$this->access_token}&media_id={$media_id}"; } //发送客服消息给用户 public function sendCustomerMessageAction() { $postStr = file_get_contents("php://input"); if (!empty ($postStr) && is_string($postStr)) { $postArr = json_decode($postStr, true); if (!empty ($postArr ['MsgType']) && $postArr ['MsgType'] == 'text') { // 文本消息 $toUserName = $postArr ['ToUserName']; // 小程序id $fromUsername = $postArr ['FromUserName']; // 发送者openid $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://'; $content = '您好,有什么能帮助你?'; $data = array( "touser" => $fromUsername, "msgtype" => "link", "link" => array( "title" => "签到领大奖", "description" => "免费! 免费! 免费!", "url" => "http://gcj.xyxsaler.zhangliediaozhatian.com/fllow.html", "thumb_url" => "" ) ); $json = json_encode($data, JSON_UNESCAPED_UNICODE); // php5.4+ $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$this->access_token}"; $this->wx->request($url, $json); exit(); } // elseif (! empty ( $postArr ['MsgType'] ) && $postArr ['MsgType'] == 'image') { // 图文消息 // $fromUsername = $postArr ['FromUserName']; // 发送者openid // $toUserName = $postArr ['ToUserName']; // 小程序id // $textTpl = array ( // "ToUserName" => $fromUsername, // "FromUserName" => $toUserName, // "CreateTime" => time (), // "MsgType" => "transfer_customer_service" // ); // $json = json_encode ( $textTpl, JSON_UNESCAPED_UNICODE); // $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$this->access_token}"; // $this->wx->request($url, $json); // exit (); // } elseif ($postArr ['MsgType'] == 'event' && $postArr ['Event'] == 'user_enter_tempsession') { // 进入客服动作 // $fromUsername = $postArr ['FromUserName']; // 发送者openid // $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://'; // $content = '您好,有什么能帮助你?'; // $data = array ( // "touser" => $fromUsername, // "msgtype" => "link", // "link" => array ( // "title" => "签到领大奖", // "description" => "免费! 免费! 免费!", // "url" => "http://gcj.xyxsaler.zhangliediaozhatian.com/fllow.html", // "thumb_url" => "" // ) // ); // $json = json_encode ( $data, JSON_UNESCAPED_UNICODE ); // php5.4+ // $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$this->access_token}"; // $this->wx->request($url, $json); // // } else { // exit ( '' ); // } // } else { // echo ""; // exit (); // } // $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token={$this->access_token}"; // $data = urldecode(json_encode($data)); // $this->wx->request($url, $data); } } public function uploadTempMediaAction() { $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token={$this->access_token}&type=TYPE"; } } ```