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'])) {
- 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');
- if (!empty($postStr) && is_string($postStr)) {
- $postArr = json_decode($postStr, true);
- if ($postArr['MsgType'] == 'event' && $postArr['Event'] == 'user_enter_tempsession') {
- $fromUsername = $postArr['FromUserName'];
- $imgurl = $img;
- $media_id = self::getMediaId($imgurl);
- $data = array(
- "touser" => $fromUsername,
- "msgtype" => "image",
- "image" => array("media_id" => $media_id)
- );
- $json = json_encode($data, JSON_UNESCAPED_UNICODE);
- self::requestAPI($json);
- }elseif ($postArr['MsgType'] !== 'event') {
- $fromUsername = $postArr['FromUserName'];
- $imgurl = $img;
- $media_id = self::getMediaId($imgurl);
- $data = array(
- "touser" => $fromUsername,
- "msgtype" => "image",
- "image" => array("media_id" => $media_id)
- );
- $json = json_encode($data, JSON_UNESCAPED_UNICODE);
- 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();
-
- $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" . $access_token;
-
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_POST, 1);
- 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);
-
- $output = curl_exec($curl);
- if (curl_errno($curl)) {
- echo 'Errno' . curl_error($curl);
- }
- curl_close($curl);
- if ($output == 0) {
- echo 'success';
- exit;
- }
- }
-
- 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."";
- @$weixin = file_get_contents($url);
- @$jsondecode = json_decode($weixin);
- @$array = get_object_vars($jsondecode);
- $token = $array['access_token'];
- return $token;
- }
- public static function getMediaId($imgurl)
- {
- $token = self::get_accessToken();
- $url = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token={$token}&type=image";
-
- $ch1 = curl_init();
- $timeout = 10;
- $real_path = "{$_SERVER['DOCUMENT_ROOT']}$imgurl";
- $data = array("media" => new \CURLFile("{$real_path}"));
-
- 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);
-
- curl_close($ch1);
- if ($result) {
- $result = json_decode($result, true);
- return $result['media_id'];
- } else {
- return null;
- }
- }
- }
|