abstract:<?phpnamespace app\index\controller;use think\Controller;use think\Session;use think\Db;use think\Config;use app\index\util\Wxtg;use app\index\util\Wxcode;/** * */class Index extend
<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Db;
use think\Config;
use app\index\util\Wxtg;
use app\index\util\Wxcode;
/**
*
*/
class Index extends Controller
{
private $_appid;
private $_appsecret;
private $_token;
public function __construct(){
$this->_appid = Config::get('appID');
$this->_appsecret = Config::get('appsecret');
$this->_token = Config::get('token');
}
public function index(){
$this->valid();
}
//微信驗(yàn)證
public function valid(){
if(isset($_GET['echostr'])){
$echoStr = $_GET["echostr"];
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = Config::get('token');
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature){
echo $echoStr;
exit;
}
}else{
$this->responseMsg();
}
}
public function code(){
$Wxtg = new Wxtg();
return $Wxtg->getCode();
}
public function wxcode(){
$Wxcode = new Wxcode();
return $Wxcode->getEwm(1300);
}
public function responseMsg()
{
//get post data, May be due to the different environments
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
//extract post data
if (!empty($postStr)){
/* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
the best way is to check the validity of xml by yourself */
libxml_disable_entity_loader(true);
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$time = time();
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
if(!empty( $keyword ) && trim($keyword)=='活動(dòng)')
{
//$msgType = "text";
//$contentStr = "Welcome to wechat world!";
$contentStr = "歡迎關(guān)注研學(xué)出行";
// $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
// echo $resultStr;
$this->responseText($postObj,$contentStr);
}else{
echo "Input something...";
}
}else {
echo "";
exit;
}
}
// 回復(fù)單文本
public function responseText($postObj,$contentStr){
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
//注意模板中的中括號(hào) 不能少 也不能多
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$time = time();
$msgType = "text";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}
// public function code(){
// header("Content-type: image/jpeg");
// $wxtgobj = new Wxtg();
// return $wxtgobj->getCode();
// }
// public function responseMsg()
// {
// //get post data, May be due to the different environments
// $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
// //extract post data
// if (!empty($postStr)){
// /* libxml_disable_entity_loader is to prevent XML eXternal Entity Injection,
// the best way is to check the validity of xml by yourself */
// libxml_disable_entity_loader(true);
// $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
// $fromUsername = $postObj->FromUserName; //用戶oppId
// $toUsername = $postObj->ToUserName;
// $eventkey = $postObj->EventKey;
// $keyword = trim($postObj->Content);
// $time = time();
// //關(guān)鍵詞回復(fù)
// if(!empty( $keyword ) && trim($keyword)=="活動(dòng)")
// {
// $contentStr = "歡迎關(guān)注研學(xué)出行";
// //單文本回復(fù)
// $this->responseText($postObj,$contentStr);
// }
// //關(guān)注事件回復(fù)
// if( strtolower($postObj->MsgType) == 'event'){
// $eventkeystr = substr($eventkey,0,8);
// $shjphone = substr($eventkey,8);
// if(strtolower($postObj->Event)=='subscribe' && $eventkeystr=='qrscene_'){
// if(strtolower($postObj->EventKey)){
// $contentStr = "歡迎關(guān)注研學(xué)出行,上級(jí)號(hào)碼".$shjphone;
// $user = Db::name('wxuser')->where('own_openid',$fromUsername)->find();
// if(!$user){
// $data['own_openid'] = $fromUsername;
// $data['phone'] = $shjphone;
// $data['create_time'] = time();
// Db::name('wxuser')->insert($data);
// }
// $this->responseText($postObj,$contentStr);
// //多圖文回復(fù)
// //$Weixinutil->responseNews($postObj,$arr);
// }
// }
// if(strtolower($postObj->Event)=='subscribe'){
// $contentStr = "歡迎關(guān)注研學(xué)出行";
// //單文本回復(fù)
// $this->responseText($postObj,$contentStr);
// //多圖文回復(fù)
// //$Weixinutil->responseNews($postObj,$arr);
// }
// //點(diǎn)擊推送活動(dòng)
// if(strtolower($postObj->Event) == 'click'){
// if(strtolower($postObj->EventKey) =='item_one'){
// $contentStr = "這是點(diǎn)擊活動(dòng)推送";
// //單文本回復(fù)
// $this->responseText($postObj,$contentStr);
// }
// }
// //如果是重掃二維碼
// if(strtolower($postObj->Event) == 'scan'){
// $contentStr = "這是重新掃碼";
// $this->responseText($postObj,$contentStr);
// }
// }
// }
// }
//回復(fù)圖片消息
public function responseImg($postObj,$contentStr){
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Image>
<MediaId><![CDATA[%s]]></MediaId>
</Image>
</xml>";
//注意模板中的中括號(hào) 不能少 也不能多
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$time = time();
$msgType = "image";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}
//回復(fù)多圖文類型的微信消息
public function responseNews($postObj ,$arr){
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>".count($arr)."</ArticleCount>
<Articles>";
foreach($arr as $k=>$v){
$textTpl .="<item>
<Title><![CDATA[".$v['title']."]]></Title>
<Description><![CDATA[".$v['description']."]]></Description>
<PicUrl><![CDATA[".$v['picUrl']."]]></PicUrl>
<Url><![CDATA[".$v['url']."]]></Url>
</item>";
}
$textTpl .="</Articles>
</xml> ";
//注意模板中的中括號(hào) 不能少 也不能多
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$time = time();
$msgType = "text";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time);
echo $resultStr;
}
private function requestCurl($curl, $https=true, $method='get', $data = null){
$ch = curl_init(); //初始化
curl_setopt($ch, CURLOPT_URL, $curl); //設(shè)置訪問URL
curl_setopt($ch, CURLOPT_HEADER, false); //設(shè)置不需要頭部信息
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //只獲取頁面內(nèi)容,但不輸出
if($https){
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //不做服務(wù)器認(rèn)證
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); //不做客戶端認(rèn)證
}
if($method=='post'){
curl_setopt($ch, CURLOPT_POST, true); //設(shè)置請(qǐng)求時(shí)POST方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //設(shè)置POST請(qǐng)求時(shí)的數(shù)據(jù)
}
$str = curl_exec($ch);
curl_close($ch);
return $str;
}
//調(diào)用token 存儲(chǔ)token
public function getWxAccessToken(){
echo $this->_appid;
echo "<br />";
echo $this->_appsecret;
echo "<br />";
// if(Session::get('access_token') && Session::get('expire_time')>time()){
// //如果session沒有過期
// return Session::get('access_token');
// }else{
//重新獲取access_token
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->_appid."&secret=".$this->_appsecret;
$res = $this->requestCurl($url);
$res = json_decode($res);
//$res = $this->http_curl($url,'get','json');
$access_token = $res->access_token;
Session::set('access_token',$access_token);
Session::set('expire_time',time()+7000);
return $access_token;
// }
}
public function getTicket($expire_seconds = 604800, $type = 'temp', $scene = '1300'){
if($type=='temp'){
$access_token = $this->getWxAccessToken();
$url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$access_token;
$data = '{"expire_seconds": '.$expire_seconds.', "action_name": "QR_STR_SCENE", "action_info": {"scene": {"scene_str": "'.$scene.'""}}}';
$res = $this->requestCurl($url,true,'post',$data);
return $res;
}
}
//獲取二維碼
public function getCode(){
$content = $this->getTicket(604800, 'temp', 13002570521);
$content = json_decode($content);
//var_dump($content) ;
$ticket = $content->ticket;
//echo $url = $content->url;
//echo "<br />";
//Session::set('access_token',$access_token);
$url = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.urlencode($ticket);
//.urlencode($ticket)
return $qrcode_img=$this->DownLoadQr($url,time());
//echo '<img src="'.$url.'" />';
//echo "<br />";
//$image = $this->requestCurl($url);
//return $image;
}
//將二維碼保存到本地 根目錄新建 upload/qrcode 這個(gè)目錄
function DownLoadQr($url,$filestring){
if($url == ""){
return false;
}
$filename = $filestring.'.jpg';
ob_start();
echo readfile($url);
$img=ob_get_contents();
ob_end_clean();
$size=strlen($img);
$fp2=fopen('/public/upload/'.$filename,"a");
if(fwrite($fp2,$img) === false){
return '';
}
fclose($fp2);
return '/public/upload/'.$filename;
}
}
?>
l老師微信公眾號(hào)二維碼怎么保存???我的顯示出錯(cuò)保存不了
Correcting teacher:查無此人Correction time:2019-06-10 09:44:24
Teacher's summary:完成的不錯(cuò)。有問題可以提交工單,這里回答后,你無法追問。繼續(xù)加油。