YII框架微信公众号

前端之家收集整理的这篇文章主要介绍了YII框架微信公众号前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

<?PHP
namespace backend\controllers;

use yii\db\Query;
use yii\web\Controller;
use Yii;
class Exam2Controller extends Controller{
public $enableCsrfValidation=false;
function actionMssage(){

$echostr=Yii::$app->request->get(‘echostr‘);
if(isset($echostr)){
$res=$this->check();
if($res){
echo $echostr;
}
}else{
$this->mess();
}
}

function check(){
$signature=$_GET["signature"];
$timestamp=$_GET["timestamp"];
$nonce=$_GET["nonce"];
$token=‘hello‘;

$tmpArr = array($timestamp,$nonce,$token);
sort($tmpArr,SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );

if( $tmpStr==$signature ){
return true;
}else{
return false;
}
}
function mess(){
$str=file_get_contents("PHP://input");
$obj=simplexml_load_string($str,‘SimpleXMLElement‘,LIBXML_NOCDATA);

$type=$obj->MsgType;
if($type=="event"){
if($obj->Event==‘subscribe‘){
$openide=$obj->FromUserName;
$sql="insert into usern(openid,leg) values(‘$openide‘,0)";
file_put_contents("/test/ccc.txt",$sql);
yii::$app->db->createCommand($sql)->execute();

$token=$this->token();
$openid=$obj->FromUserName;
$url="https://api.weixin.qq.com/cgi-bin/user/info?access_token=$token&openid=$openid&lang=zh_CN";
$urls=file_get_contents($url);
$json=json_decode($urls,true);
$nickname=$json[‘nickname‘];
$city=$json[‘city‘];
$province=$json[‘province‘];
$sex=$json[‘sex‘]?‘男‘:‘女‘;
$conte="你好$nickname,我猜你是{$province}省{$city}市的$sex";
$this->info($obj,$conte);
}
else if($obj->Event==‘CLICK‘){

if($obj->EventKey=="qiandao"){
$time=time();
$openid=$obj->FromUserName;
$data=Yii::$app->db->createCommand("select*from qiandao where openid=‘$openid‘")->queryOne();
if(!$data){
$time=time();
Yii::$app->db->createCommand("update usern set leg=leg+10 where openid=‘$openid‘")->execute();
Yii::$app->db->createCommand("insert into qiandao(openid,addtime)values(‘$openid‘,‘$time‘)")->execute();
$content="签到成功";
$this->info($obj,$content);
}else if($data){

$d1=date(‘Y-m-d‘,$time);
$d2=date(‘Y-m-d‘,$data[‘addtime‘]);
if($d1==$d2){
$content="您已经签过到";
$this->info($obj,$content);
}else{
Yii::$app->db->createCommand("update usern set leg=leg+10 where openid=‘$openid‘")->execute();
Yii::$app->db->createCommand("update qiandao set addtime=$time where openid=‘$openid‘")->execute();
$content="签到成功";
$this->info($obj,$content);
}
}
}else if($obj->EventKey=="tuwen"){
$data=[
[‘title1‘=>‘今天星期四‘,
‘description1‘=>‘又是好的一天‘,
‘picurl‘=>‘http://b391.photo.store.qq.com/psb?/V11XFINC03DLi5/DNhOQga*ySbNPg6XQ7IiSEToI0g8UljpgVnj*yzEYB0!/b/dDKZGOkhIAAA&bo=wAOAAgAAAAABB2E!&rf=viewer_4‘,
‘url‘=>‘http://www.baidu.com‘

],
[‘title1‘=>‘今天星期四‘,
];
$this->dtw($obj,$data);


}
}
}else if($type==‘text‘){
if($obj->Content==‘图片‘){

$titles =(new Query())->select(‘filename‘)->from(‘filed‘)->indexBy(‘filename‘)->column();
$medio_id=array_rand($titles,1);
$this->doimg($obj,$medio_id);
}
}
}

function dtw($obj,$data){
$xml=‘<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%d</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>3</ArticleCount>
<Articles>%s</Articles>
</xml>‘;
$t1=‘<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>‘;
$str=‘‘;
foreach ($data as $k=>$v){
$str.=sprintf($t1,$v[‘title1‘],$v[‘description1‘],$v[‘picurl‘],$v[‘url‘]);
}
$res=sprintf($xml,$obj->FromUserName,$obj->ToUserName,time(),$str);
echo $res;

}
function doimg($obj,$medio_id){
$xml="<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%d</CreateTime>
<MsgType><![CDATA[image]]></MsgType>
<Image>
<MediaId><![CDATA[%s]]></MediaId>
</Image>
</xml>";

$str=sprintf($xml,$medio_id);
echo $str;
}
function info($obj,$conte){
$xml=‘<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%d</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>‘;
$res=sprintf($xml,$conte);
echo $res;
}

function token(){
$appid="wx56bd4d1b3747f03b";
$appsecret=‘cec1de114ad6d7a555f2fb141cd3855a‘;
$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret";
$urls=file_get_contents($url);
$json=json_decode($urls,true);
$tok=$json[‘access_token‘];
return $tok;
}
function medio($tet){
$token=$this->token();
$url="https://api.weixin.qq.com/cgi-bin/media/upload?access_token=$token&type=image";

$upload=new \CURLFile($tet);
$arr=array(‘meido‘=>$upload);
$med=$this->cur($url,true,‘post‘,$arr);
[email protected]_decode($med,true);
$media=$json[‘media_id‘];

return $media;

}

function actionFile(){
return $this->render(‘file‘);
}
function actionDofil(){

 


$file=$_FILES[‘file‘];
$tmp_name=$file[‘tmp_name‘];
$tet="/test/".$file[‘name‘];
move_uploaded_file($tmp_name,$tet);
$medio_id=$this->medio($tet);
$time=time();
$data=Yii::$app->db->createCommand("insert into filed(filename,creat)values(‘$medio_id‘,‘$time‘)")->execute();

}
function cur($url,$https=false,$methou=‘get‘,$arr){
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,"$url");
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
if($https==true){
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
}
if($methou==‘post‘){
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$arr);
}

$res=curl_exec($ch);
return $res;

 

}
}

 

//model层文件上传

<?PHP
namespace backend\models;

use yii\db\ActiveRecord;

class Dg extends ActiveRecord
{
/**
* @var UploadedFile
*/
// public $imageFile;

public function rules()
{
return [

[[‘imageFile‘],‘file‘,‘skipOnEmpty‘ => true,‘extensions‘ => ‘png,jpg‘],
[[‘leav‘],‘required‘],

];
}

public function upload()
{
if ($this->validate()) {
$this->imageFile->saveAs(‘uploads/‘ . $this->imageFile->baseName . ‘.‘ . $this->imageFile->extension);
$upl=‘uploads/‘ . $this->imageFile->baseName . ‘.‘ . $this->imageFile->extension;
return $upl;
} else {
return false;
}
}
}

 

//model层处理递归

<?PHP
namespace backend\models;

use yii\db\ActiveRecord;

class Cont extends ActiveRecord
{
public function rules()
{
return [
// name,email,subject 和 body 属性必须有值
[[‘name‘,‘pid‘],

// email 属性必须是一个有效的电子邮箱地址

];
}

function gettree($data,$pid){
static $str=[];
foreach ($data as $k=>$v){

if($v[‘pid‘]==$pid){ $str[$k]=$v; $str[$k][‘name‘]=str_repeat("--|",$v[‘leav‘]-1).$v[‘name‘]; $this->gettree($data,$v[‘id‘]); } } return $str; }}

猜你在找的微信公众号相关文章