行业动态

了解最新公司动态及行业资讯

当前位置:首页>新闻中心>行业动态
全部 4048 公司动态 912 行业动态 3136

如何把微信公众号授权给开发者服务器2与微信服务器进行

时间:2022-08-06   访问量:2107

文章目录

很多人都有自己的服务器。尤其是对大学生来说服务器运维服务器运维,很多服务器公司对中学生都是有利的。比如腾讯云每个月只要1块钱:所以作为程序员,我们总是想自己掌控一切。在手,如何使用我们的开发者服务器管理陌陌公众号?这就是本文所涵盖的内容。

本文介绍了以下内容:

1 如何将陌陌公众号授权给开发者服务器 2 如何使用代码将开发者服务器与陌陌服务器关联


/**
 * 微信公众号开发-入门
 *
 * api
 */
define("TOKEN",'we--xxxx');   //这里和你微信公众号开放平台上的tonken填写一样的即可
$weixinApi=new WeixinApi();
if(isset($_GET["echostr"])){
    $weixinApi->valid();
}else{
    $weixinApi->responseMsg();
}
class WeixinApi{
    //验证接口
    public function valid(){
        $echoStr = $_GET["echostr"];//从微信用户端获取一个随机字符赋予变量echostr
        if($this->checkSignature()){
            echo $echoStr;
            exit;
        }
    }
    //检查签名
    private function checkSignature(){
        //1 接受微信服务器get请求发送过来的4个参数
        $signature = $_GET["signature"];//从用户端获取签名赋予变量signature
        $timestamp = $_GET["timestamp"];//从用户端获取时间戳赋予变量timestamp
        $nonce = $_GET["nonce"];    //从用户端获取随机数赋予变量nonce
        //2 加密和校验请求
        //2.1 将token、timestamp、nonce三个参数进行字典序排序
        $tmpArr = array(TOKEN, $timestamp, $nonce);//简历数组变量tmpArr
        sort($tmpArr, SORT_STRING);//新建排序
        //2.2 将三个参数字符串拼接成一个字符串进行sha1加密

服务器运维_运维服务口号大全_运维服务服务器网络设备日常巡检报告

$tmpStr = implode($tmpArr);//数组转字符串 $tmpStr = sha1($tmpStr);//shal加密 //2.3 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信 if ($tmpStr == $signature) { return true; } else { return false; } } //回复消息 public function responseMsg(){ //3 以下代码接受消息 //3.1 接受微信服务器发送过来的原生的POST的数据包 // $postData = $GLOBALS["HTTP_RAW_POST_DATA"]; $postData = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] :file_get_contents("php://input"); //3.2 处理数据包 $xmlObj = simplexml_load_string($postData, "SimpleXMLElement", LIBXML_NOCDATA); $msgType = $xmlObj->MsgType; //4 根据消息类型进行业务处理 switch ($msgType) { //接受事件消息 case 'event': $this->disposeEvent($xmlObj); break; //接受文本消息 case 'text': $this->disposeText($xmlObj); break; //接受图片消息 case 'image': $this->disposeImage($xmlObj); break; } } //处理接收的事件消息 private function disposeEvent($xmlObj){ switch ($xmlObj->Event){ case 'subscribe'://订阅事件 $this->sendText('欢迎您的订阅'); break; case 'unsubscribe'://取消订阅事件 $this->sendText('good-bye');//该消息用户其实是看不到的,取消订阅事件一般用来清除数据库记录 break; } } //处理接收的文本消息 private function disposeText($xmlObj){ $text=trim($xmlObj->Content); //包含关键字都不做处理 if (!( strstr($text,'违规') //这里对违规的关键字做排除,不予理睬 )){ switch ($text){ case '你好': $this->sendText($xmlObj,'Hi 我是开发者服务器'); break; case 'new': $newsArr=array( array( "title"=>"看到这条消息,你可以买彩票了", "description"=>"本公众号有许多小彩蛋,欢迎您的探索。", "picUrl"=>"http://img.mp.itc.cn/upload/20170610/03d69e8df0524b8cb59fd16dc2fec989.jpg", "url"=>"http://www.baidu.com" ) ); $this->sendNews($xmlObj,$newsArr); break; default: $this->tuling123($xmlObj,trim($xmlObj->Content)); //图灵机器人 } } } //处理接收的图片消息 private function disposeImage($xmlObj){ //一般情况下,不会去处理用户发送的图片 $this->sendImage($xmlObj,$xmlObj->PicUrl,$xmlObj->MediaId); } //发送文本的方法 private function sendText($xmlObj,$content){ $replyTextMsg=" %s "; echo sprintf($replyTextMsg,$xmlObj->FromUserName,$xmlObj->ToUserName,time(),$content); } //发送图片的方法 private function sendImage($xmlObj,$mediaId){ $replyImageMsg=" %s "; echo sprintf($replyImageMsg,$xmlObj->FromUserName,$xmlObj->ToUserName,time(),$mediaId); } //发送图文的方法 private function sendNews($xmlObj,$newsArr){ $newsTplHead = " %s %s "; $newsTplBody = " <![CDATA[%s]]> "; $newsTplFoot = " %s "; $replyNewsMsg = sprintf($newsTplHead, $xmlObj->FromUserName, $xmlObj->ToUserName, time(),count($newsArr)); foreach($newsArr as $key => $value){ $replyNewsMsg .= sprintf($newsTplBody, $value['title'], $value['description'], $value['picUrl'], $value['url']); } $replyNewsMsg .= sprintf($newsTplFoot, 0); echo $replyNewsMsg; } public function tuling123($xmlObj,$message){//这是是使用图灵机器人 $tuTonken='2d8aaa17141c443----xxx---fsa'; //请去图灵网http://www.tuling123.com/自己申请一个tonken $tuUrl='http://www.tuling123.com/openapi/api?key='.$tuTonken.'&info='.$message.'&userid='.$xmlObj->FromUserName; $tuData='{ "key": "'.$tuTonken.'", "info": "'.$message.'", "userid": "'.$xmlObj->FromUserName.'" }'; $results = $this->htts_request($tuUrl,$tuData); // print_r($results); if ($results['code']==100000){ $text=$results['text']; $this->sendText($xmlObj,$text); }else{ $this->sendText($xmlObj,'有问题,请输入“帮助”'); } } //https请求(get和post) private function htts_request($url,$data=array()){

运维服务服务器网络设备日常巡检报告_服务器运维_运维服务口号大全

//1 初始化curl $ch=curl_init(); //2 设置传输选项 curl_setopt($ch,CURLOPT_URL,$url); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//把页面以文件流的形式返回 if (!empty($data)) { curl_setopt($ch, CURLOPT_POST, true); //设置为 POST 请求 curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //设置POST的请求数据 } //3 执行curl请求 $outopt=curl_exec($ch); $outoptArr=json_decode($outopt,true); //4 关闭curl curl_close($ch); return $outoptArr; } } ?>

    //网页授权-base型
    public function snsapi_base($redirect_uri){
        //以下是测试账号
        $appid="wxb4----xxx";//请在第一章第5小节的图片中看
        $appsecret="21da56-----xxx";//请在第一章第5小节的图片中看
        //准备scope
        $snsapi_base_url="https://open.weixin.qq.com/connect/oauth2/authorize?appid={$appid}&redirect_uri={$redirect_uri}&response_type=code&scope=SCOPE&state=123#wechat_redirect";
        $code=$_GET['code'];
        //获取code
        if (!isset($code)){
            header("Location:{$snsapi_base_url}");
        }
        //获取access_token
        $url="https://api.weixin.qq.com/sns/oauth2/access_token?appid={$appid}&secret={$appsecret}&code={$code}&grant_type=authorization_code";
        return $this->htts_request($url);
    }

上一篇:提升IT服务支持水平的IT外包服务公司所运用的措施

下一篇:pc外包 上班两周被辞退,被开除后我紧急找了3份Offer

发表评论:

评论记录:

未查询到任何数据!

在线咨询

点击这里给我发消息 售前咨询专员

点击这里给我发消息 售后服务专员

在线咨询

免费通话

24小时免费咨询

请输入您的联系电话,座机请加区号

免费通话

微信扫一扫

微信联系
返回顶部