微信公众号开发者认证及简单功能点实现

其他不说,直接上代码。。

<?php
/**
* wechat php test
*/
//define your token
define("TOKEN", "cxb521900");

//首次验证成为开发者
$wechatObj = new wechatCallbackapiTest();
if(isset($_GET["echostr"])){ #验证过token,成为开发者之后,可以直接$wechatObj->responseMsg();
$wechatObj->valid();
}else{
//$wechatObj->responseMsg();
}
//获取二维码0
//echo $wechatObj->Qrcode('1231');
// 调用回复信息
$wechatObj->responseMsg();




//微信接口
class wechatCallbackapiTest
{
//appid
protected $appid = 'wx715491939ec58761';
//secret
protected $secret = '1021f2658f7b17d5ec7d2dd1fa28cea4';
//封装调用url的方法
private function url($url){
$content = file_get_contents($url);
return $content;
}
//模拟curl
function curl($url,$method,$data=array(),$setcooke=false,$cookie_file='1.txt'){
$ch = curl_init(); //1.初始化
curl_setopt($ch, CURLOPT_URL, $url); //2.请求地址
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式
//4.参数如下 绕过服务器端SSL的验证
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
//伪装请求来源,绕过防盗
//curl_setopt($ch,CURLOPT_REFERER,"http://wthrcdn.etouch.cn/");
//curl_setopt($ch,CURLOPT_REFERER,"http://upload1.techweb.com.cn");

//配置curl解压缩方式(默认的压缩方式)
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Encoding:gzip'));
curl_setopt($ch, CURLOPT_ENCODING, "gzip");
//配置代理
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0'); //指明以哪种方式进行访问
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
if($method=="POST"){//5.post方式的时候添加数据
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
//模拟登陆
if($setcooke==true){
//如果设置要请求的cookie,那么把cookie值保存在指定的文件中
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
}else{
//就从文件中读取cookie的信息,并验证
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
}
//不直接输出内容
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//执行
$tmpInfo = curl_exec($ch);

if (curl_errno($ch)) {
return curl_error($ch);
}
//释放资源
curl_close($ch);
//返回获取的信息
return $tmpInfo;
}

//获取access_token
public function getAccesstoken(){
$file = 'access_token';
if(file_exists($file)&&time()-filemtime($file)<7200){
return file_get_contents($file);
}else{
$access_token = $this->url("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appid}&secret={$this->secret}");
$access_token = json_decode($access_token,true)['access_token'];
file_put_contents($file,$access_token);
return $access_token;
}
}
//验证开发者
public function valid()
{
$echoStr = $_GET["echostr"];
//valid signature , option
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
///----------------------------------------------------------------------------------
private function checkSignature() #这个函数验证过之后就可以删除了
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}

//创建二维码的票据
private function ticket($secne_id,$type=0,$expire = 604800){
$access_token = $this->getAccesstoken();
$url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$access_token;
if($type==0){
$data = '{"expire_seconds": %s, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": %s}}}';
$data = sprintf($data,$expire,$secne_id);
}
elseif($tmp==1){
$data = '{"expire_seconds": %s, "action_name": "QR_STR_SCENE", "action_info": {"scene": {"scene_str": "%s"}}}';
$data = sprintf($data,$expire,$secne_id);
}
elseif($tmp==2){
$data = '{"action_name": "QR_LIMIT_STR_SCENE", "action_info": {"scene": {"scene_str": "%s"}}}';
$data = sprintf($data,$secne_id);
}
else{
$data = '{"action_name": "QR_LIMIT_SCENE", "action_info": {"scene": {"scene_id": %s}}}';
$data = sprintf($data,$secne_id);
}
//提交数据
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
$output = curl_exec($ch);
curl_close($ch);
$ticket = json_decode($output,true)['ticket'];
return $ticket;
}

//获取二维码
public function Qrcode($secne_id,$type=0,$expire=604800){
$ticket = $this->ticket($secne_id);
$url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".urlencode($ticket);
$result = $this->curl($url,"GET");
//return $result;
header("content-type:image/jpg");
return $result;
}

//接受消息
public function responseMsg(){
//php得高版本中用此接受
//$postStr = file_get_contents("PHP://input");
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
libxml_disable_entity_loader(true);
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$msgType = $postObj->MsgType;

if($msgType == 'text'){
$this->toText($postObj);
}elseif($msgType == 'event'){
$event = $postObj->Event;
if($event=='subscribe'){
$this->toEvent($postObj);
}
}
}
//扫描二维码关注时时间推送
private function toEvent($postObj){
$toUser = $postObj->FromUserName;
$fromUser = $postObj->ToUserName;
$time = time();
$content = '谢谢您的惠顾';

$event = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$event = sprintf($event,$toUser,$fromUser,$time,$content);
echo $event;
}
//回复文本消息
private function toText($postObj){
$toUser = $postObj->FromUserName;
$fromUser = $postObj->ToUserName;
$time = time();
$content = '你好,此刻为你服务';
$text = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$text = sprintf($text,$toUser,$fromUser,$time,$content);
echo $text;
}
}


?>












  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值