<?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();
}
//微信验证
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)=='活动')
{
//$msgType = "text";
//$contentStr = "Welcome to wechat world!";
$contentStr = "欢迎关注研学出行";
// $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
// echo $resultStr;
$this->responseText($postObj,$contentStr);
}else{
echo "Input something...";
}
}else {
echo "";
exit;
}
}
// 回复单文本
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>";
//注意模板中的中括号 不能少 也不能多
$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();
// //关键词回复
// if(!empty( $keyword ) && trim($keyword)=="活动")
// {
// $contentStr = "欢迎关注研学出行";
// //单文本回复
// $this->responseText($postObj,$contentStr);
// }
// //关注事件回复
// 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 = "欢迎关注研学出行,上级号码".$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);
// //多图文回复
// //$Weixinutil->responseNews($postObj,$arr);
// }
// }
// if(strtolower($postObj->Event)=='subscribe'){
// $contentStr = "欢迎关注研学出行";
// //单文本回复
// $this->responseText($postObj,$contentStr);
// //多图文回复
// //$Weixinutil->responseNews($postObj,$arr);
// }
// //点击推送活动
// if(strtolower($postObj->Event) == 'click'){
// if(strtolower($postObj->EventKey) =='item_one'){
// $contentStr = "这是点击活动推送";
// //单文本回复
// $this->responseText($postObj,$contentStr);
// }
// }
// //如果是重扫二维码
// if(strtolower($postObj->Event) == 'scan'){
// $contentStr = "这是重新扫码";
// $this->responseText($postObj,$contentStr);
// }
// }
// }
// }
//回复图片消息
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>";
//注意模板中的中括号 不能少 也不能多
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$time = time();
$msgType = "image";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}
//回复多图文类型的微信消息
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> ";
//注意模板中的中括号 不能少 也不能多
$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); //设置访问URL
curl_setopt($ch, CURLOPT_HEADER, false); //设置不需要头部信息
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //只获取页面内容,但不输出
if($https){
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //不做服务器认证
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); //不做客户端认证
}
if($method=='post'){
curl_setopt($ch, CURLOPT_POST, true); //设置请求时POST方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //设置POST请求时的数据
}
$str = curl_exec($ch);
curl_close($ch);
return $str;
}
//调用token 存储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 这个目录
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老师微信公众号二维码怎么保存啊?我的显示出错保存不了