Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 91.com在线观看,国产免费91,国产精品嫩草影院免费看

          整合營銷服務(wù)商

          電腦端+手機(jī)端+微信端=數(shù)據(jù)同步管理

          免費(fèi)咨詢熱線:

          如何使用開發(fā)者服務(wù)器運(yùn)維微信公眾號

          文章目錄

          很多人都有自己的服務(wù)器,特別是對于大學(xué)生而言,很多服務(wù)器公司都對學(xué)生有優(yōu)惠,例如騰訊云只要1元每月: ,那么我們作為一名程序員,總想把一切掌控在自己手中,,那么如何使用我們開發(fā)者服務(wù)器去管理微信公眾號呢?這就是本文介紹的內(nèi)容。

          本文介紹以下內(nèi)容:

          1 如何把微信公眾號授權(quán)給開發(fā)者服務(wù)器 2 如何使用代碼把開發(fā)者服務(wù)器與微信服務(wù)器進(jìn)行關(guān)聯(lián)

          
          /**
           * 微信公眾號開發(fā)-入門
           *
           * api
           */
          define("TOKEN",'we--xxxx');   //這里和你微信公眾號開放平臺上的tonken填寫一樣的即可
          $weixinApi=new WeixinApi();
          if(isset($_GET["echostr"])){
              $weixinApi->valid();
          }else{
              $weixinApi->responseMsg();
          }
          class WeixinApi{
              //驗(yàn)證接口
              public function valid(){
                  $echoStr = $_GET["echostr"];//從微信用戶端獲取一個隨機(jī)字符賦予變量echostr
                  if($this->checkSignature()){
                      echo $echoStr;
                      exit;
                  }
              }
              //檢查簽名
              private function checkSignature(){
                  //1 接受微信服務(wù)器get請求發(fā)送過來的4個參數(shù)
                  $signature = $_GET["signature"];//從用戶端獲取簽名賦予變量signature
                  $timestamp = $_GET["timestamp"];//從用戶端獲取時間戳賦予變量timestamp
                  $nonce = $_GET["nonce"];    //從用戶端獲取隨機(jī)數(shù)賦予變量nonce
                  //2 加密和校驗(yàn)請求
                  //2.1 將token、timestamp、nonce三個參數(shù)進(jìn)行字典序排序
                  $tmpArr = array(TOKEN, $timestamp, $nonce);//簡歷數(shù)組變量tmpArr
                  sort($tmpArr, SORT_STRING);//新建排序
                  //2.2 將三個參數(shù)字符串拼接成一個字符串進(jìn)行sha1加密
                  $tmpStr = implode($tmpArr);//數(shù)組轉(zhuǎn)字符串
                  $tmpStr = sha1($tmpStr);//shal加密
                  //2.3 開發(fā)者獲得加密后的字符串可與signature對比,標(biāo)識該請求來源于微信
                  if ($tmpStr == $signature) {
                      return true;
                  } else {
                      return false;
                  }
              }
              //回復(fù)消息
              public function responseMsg(){
                  //3 以下代碼接受消息
                  //3.1 接受微信服務(wù)器發(fā)送過來的原生的POST的數(shù)據(jù)包
          //        $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 處理數(shù)據(jù)包
                  $xmlObj = simplexml_load_string($postData, "SimpleXMLElement", LIBXML_NOCDATA);
                  $msgType = $xmlObj->MsgType;
                  //4 根據(jù)消息類型進(jìn)行業(yè)務(wù)處理
                  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');//該消息用戶其實(shí)是看不到的,取消訂閱事件一般用來清除數(shù)據(jù)庫記錄
                          break;
                  }
              }
              //處理接收的文本消息
              private function disposeText($xmlObj){
                  $text=trim($xmlObj->Content);
                  //包含關(guān)鍵字都不做處理
                  if (!(
                      strstr($text,'違規(guī)')    //這里對違規(guī)的關(guān)鍵字做排除,不予理睬
                  )){
                      switch ($text){
                          case '你好':
                              $this->sendText($xmlObj,'Hi 我是開發(fā)者服務(wù)器');
                              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));   //圖靈機(jī)器人
                      }
                  }
          
              }
              //處理接收的圖片消息
              private function disposeImage($xmlObj){    //一般情況下,不會去處理用戶發(fā)送的圖片
                  $this->sendImage($xmlObj,$xmlObj->PicUrl,$xmlObj->MediaId);
              }
              //發(fā)送文本的方法
              private function sendText($xmlObj,$content){
                  $replyTextMsg="
                                  
                                  
                                  %s
                                  
                                  
                              ";
                  echo sprintf($replyTextMsg,$xmlObj->FromUserName,$xmlObj->ToUserName,time(),$content);
              }
              //發(fā)送圖片的方法
              private function sendImage($xmlObj,$mediaId){
                  $replyImageMsg="
                                  
                                  
                                  %s
                                  
                                  
                                      
                                  
                              ";
                  echo sprintf($replyImageMsg,$xmlObj->FromUserName,$xmlObj->ToUserName,time(),$mediaId);
              }
              //發(fā)送圖文的方法
              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){//這是是使用圖靈機(jī)器人
                  $tuTonken='2d8aaa17141c443----xxx---fsa';   //請去圖靈網(wǎng)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 設(shè)置傳輸選項(xiàng)
                  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); //設(shè)置為 POST 請求
                      curl_setopt($ch, CURLOPT_POSTFIELDS, $data); //設(shè)置POST的請求數(shù)據(jù)
                  }
                  //3 執(zhí)行curl請求
                  $outopt=curl_exec($ch);
                  $outoptArr=json_decode($outopt,true);
                  //4 關(guān)閉curl
                  curl_close($ch);
                  return $outoptArr;
              }
          }
          ?>
          

          //獲取access_token
              private function getAccessToken(){
                  //獲取微信接口憑證
                  $appid="wxb4----xxx";//請?jiān)诘谝徽碌?小節(jié)的圖片中看
                  $appsecret="21d---xxx";//請?jiān)诘谝徽碌?小節(jié)的圖片中看
                  $data=json_decode(file_get_contents('./access_token.json'));
                  if ($data->expires_time <time()){
                      $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";
                      $outoptArr=$this->htts_request($url,array(),false);
                      $access_token=$outoptArr['access_token'];
                      if (!empty($access_token)){
          
                          //把a(bǔ)ccess_token寫入文件
                          $data->access_token=$outoptArr['access_token'];
                          $data->expires_time=time()+7000;
                          $fp=fopen('access_token.json','w');
                          fwrite($fp,json_encode($data));
                          fclose($fp);
                      }else{
                          echo '請求access_token錯誤';
                      }
                  }else{
                      $access_token=$data->access_token;
                  }
          //        echo $access_token;
                  return $access_token;
              }
          //實(shí)現(xiàn)自定義菜單
              public function menu_create(){
                  $access_token=$this->getAccessToken();
                  $url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token={$access_token}";
                  $data='{
          		"button": [
          			{
          				"type": "click",
          				"name": "java",
          				"key": "learn_java"
          			},
          			{
          				"name":"chengxu",
          				"sub_button":[
          				{
          					"type": "view",
          					"name": "CSDN",
          					"url": "http://blog.csdn.net/tiandixuanwuliang/"
          				},
          				{
          					"type": "view",
          					"name": "Github",
          					"url": "https://github.com/wllfengshu/"
          				},
          				{
          					"type": "view",
          					"name": "jianshu",
          					"url": "https://www.jianshu.com/users/4d12e03d0a5f/timeline/"
          				},
          				{
          					"type": "view",
          					"name": "kaifazhe",
          					"url": "https://toutiao.io/u/431066/"
          				},
          				{
          					"type": "view",
          					"name": "yuyan",
          					"url": "http://www.baidu.com"
          				}]
          			},
          			{
          				"name":"jiaoliu",
          				"sub_button":[
          				{
          					"type": "view",
          					"name": "shuji",
          					"url": "http://blog.csdn.net/tiandixuanwuliang/"
          				},
          				{
          					"type": "view",
          					"name": "ziyuan",
          					"url": "https://github.com/wllfengshu/"
          				},
          				{
          					"type": "view",
          					"name": "sucai",
          					"url": "https://www.jianshu.com/users/4d12e03d0a5f/timeline/"
          				},
          				{
          					"type": "view",
          					"name": "daxuesheng",
          					"url": "https://toutiao.io/u/431066/"
          				},
          				{
          					"type": "click",
          					"name": "zuozhe",
          					"key": "about_author"
          				}]
          			}
          		]
          	}';
                  echo $url." / ".$data;
                  $outoptArr=$this->htts_request($url,json_decode($data,true),true);
                  echo '***';
                  print_r($outoptArr);
              }
          

              //網(wǎng)頁授權(quán)-base型
              public function snsapi_base($redirect_uri){
                  //以下是測試賬號
                  $appid="wxb4----xxx";//請?jiān)诘谝徽碌?小節(jié)的圖片中看
                  $appsecret="21da56-----xxx";//請?jiān)诘谝徽碌?小節(jié)的圖片中看
                  //準(zhǔn)備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);
              }
          


          主站蜘蛛池模板: 日韩精品一区在线| 好吊妞视频一区二区| 中文字幕日韩一区二区三区不卡| 国产一区二区在线观看视频| 国产成人精品无码一区二区三区 | 国产综合无码一区二区三区| 久久精品国产第一区二区| 2021国产精品视频一区| 中文激情在线一区二区| 韩国福利一区二区三区高清视频| av在线亚洲欧洲日产一区二区| 久久精品国产一区二区三区| 国产成人无码精品一区在线观看| 国产在线观看91精品一区| 一区二区高清在线观看| 国产一区二区在线观看麻豆| 精品人妻无码一区二区色欲产成人| 性色av无码免费一区二区三区| 国产精品久久久久一区二区| 伊人色综合网一区二区三区 | 麻豆文化传媒精品一区二区| 狠狠色婷婷久久一区二区三区| 日韩视频一区二区三区| 国产午夜毛片一区二区三区| 香蕉视频一区二区三区| 日韩人妻无码一区二区三区久久99| 亚洲一区AV无码少妇电影☆| 中文字幕无码一区二区三区本日| 久久久久人妻一区精品色 | 亚洲综合在线成人一区| 亚洲午夜电影一区二区三区 | 国产乱码精品一区二区三区四川人| 国产精品成人一区无码| 日本道免费精品一区二区| 手机看片福利一区二区三区| 红杏亚洲影院一区二区三区 | 中文字幕一区二区三区永久| 无码人妻一区二区三区在线| 国产精品视频免费一区二区三区| 国产主播一区二区| 日韩精品无码一区二区三区|