欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP微信API接口類

 更新時間:2016年08月22日 17:10:17   作者:shyhero  
很全面詳細的PHP微信API接口類,幫助大家更好的進行php微信開發(fā),感興趣的小伙伴們可以參考一下

本文實例為大家分享了PHP微信接口類,供大家參考,具體內(nèi)容如下

<?php
/**
 * wechat php test
 */
 
//define your token
//定義TOKEN秘鑰
define("TOKEN", "weixin");
 
//實例化微信對象
$wechatObj = new wechatCallbackapiTest();
//驗證成功后注釋valid方法
//$wechatObj->valid();
//開啟自動回復功能
$wechatObj->responseMsg();
 
 
//定義類文件
class wechatCallbackapiTest
{
  //實現(xiàn)valid驗證方法:實現(xiàn)對接微信公眾平臺
  public function valid()
  {
    //接受隨機字符串
    $echoStr = $_GET["echostr"];
 
    //valid signature , option
    //進行用戶數(shù)字簽名驗證
    if($this->checkSignature()){
      //如果成功,則返回接受到的隨機字符串
      echo $echoStr;
      //退出
      exit;
    }
  }
  //定義自動回復功能
  public function responseMsg()
  {
    //get post data, May be due to the different environments
    //接受用戶端發(fā)送過來的xml數(shù)據(jù)
    $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
 
    //extract post data
    //判斷xml數(shù)據(jù)是否為空
    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);
        //通過simplexml進行xml解析
        $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
        //接受微信的手機端
        $fromUsername = $postObj->FromUserName;
        //微信公眾平臺
        $toUsername = $postObj->ToUserName;
        //接受用戶發(fā)送的關鍵詞
        $keyword = trim($postObj->Content);
        //1.接受用戶消息類型
        $msgType = $postObj -> MsgType;
        //時間戳
        $time = time();
        //文本發(fā)送模板
        $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>"; 
        //////////////////////////////////////////////////////////////////////////////////
        //如果用戶發(fā)送的是文本類型文件,執(zhí)行以下
        if($msgType == 'text'){
          if(!empty( $keyword ))
          {
            /*這是一個實例
              //如果發(fā)送文本信息
              $msgType = "text";
              //回復內(nèi)容
              if($keyword == "李楠"){
                $contentStr = "叫我干嘛";
              }else{
                $contentStr = "叫我干嘛";
              }
              //格式化xml模板,參數(shù)與上面的模板是一一對應的.fromUsername和頭Username是相反的,只寫帶%s的
              $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
 
              //將xml信息返回給客戶端
              echo $resultStr;
            */
            if($keyword == "?" || $keyword == "?"){
              $msgType = "text";
              $contentStr = "1.特種服務號碼\n2.通訊服務號碼";
              $resultStr = sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);
              echo $resultStr;
            }elseif($keyword == 1){
              $msgType = "text";
              $contentStr = "1.匪警:110\n2.火警:119\n3.急救:120";
              $resultStr = sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);
              echo $resultStr;
            }elseif($keyword == 2){
              $msgType = "text";
              $contentStr = "1.中國移動:10086\n2.中國聯(lián)通:10010";
              $resultStr = sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);
              echo $resultStr;
            }
          }else{
            echo "不能不說話";
          }
        }
        ////////////////////////////////////////////////////////////////////////////////////
        //接受圖片信息
        if($msgType == "image"){
            //如果發(fā)送文本信息
            $msgType = "text";
            //回復內(nèi)容
            $contentStr = "你發(fā)送的是圖片文件";
            //格式化字符串
            $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
            //將xml信息返回給客戶端
            echo $resultStr;
        }
        ////////////////////////////////////////////////////////////////////////////////////
        if($msgType == "voice"){
            //如果發(fā)送文本信息
            $msgType = "text";
            //回復內(nèi)容
            $contentStr = "你發(fā)送的是語音文件";
            //格式化字符串
            $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
            //將xml信息返回給客戶端
            echo $resultStr;
        }
        ////////////////////////////////////////////////////////////////////////////////////
        if($msgType == "video"){
            //如果發(fā)送文本信息
            $msgType = "text";
            //回復內(nèi)容
            $contentStr = "你發(fā)送的是視頻文件";
            //格式化字符串
            $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
            //將xml信息返回給客戶端
            echo $resultStr;
        }
        ////////////////////////////////////////////////////////////////////////////////////
        if($msgType == "shortvideo"){
            //如果發(fā)送文本信息
            $msgType = "text";
            //回復內(nèi)容
            $contentStr = "你發(fā)送的是小視頻文件";
            //格式化字符串
            $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
            //將xml信息返回給客戶端
            echo $resultStr;
        }
        ////////////////////////////////////////////////////////////////////////////////////
        if($msgType == "location"){
            //如果發(fā)送文本信息
            $msgType = "text";
            //回復內(nèi)容
            $contentStr = "你發(fā)送的是地理位置文件";
            //格式化字符串
            $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
            //將xml信息返回給客戶端
            echo $resultStr;
        }
        ////////////////////////////////////////////////////////////////////////////////////
        if($msgType == "link"){
            //如果發(fā)送文本信息
            $msgType = "text";
            //回復內(nèi)容
            $contentStr = "你發(fā)送的是連接文件";
            //格式化字符串
            $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
            //將xml信息返回給客戶端
            echo $resultStr;
        }
        ////////////////////////////////////////////////////////////////////////////////////
        /*
        //判斷用戶發(fā)送關鍵詞是否為空      
         
        if(!empty( $keyword ))
        {
          //如果發(fā)送文本信息
          $msgType = "text";
          //回復內(nèi)容
          $contentStr = "大家好,我是hero";
          //格式化字符串
          $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
          //將xml信息返回給客戶端
          echo $resultStr;
        }else{
          echo "Input something...";
        }
        */
    }else {
      echo "";
      exit;
    }
  }
     
  private function checkSignature()
  {
    // you must define TOKEN by yourself
    //判斷是否定義了TOKEN,如果沒有就拋出一個異常
    if (!defined("TOKEN")) {
      throw new Exception('TOKEN is not defined!');
    }
     
    $signature = $_GET["signature"];//接受微信加密簽名
    $timestamp = $_GET["timestamp"];//接受時間戳
    $nonce = $_GET["nonce"];//接受隨機數(shù)
         
    $token = TOKEN;//把TOKEN常量賦值給$token
    //把相關參數(shù)組裝成數(shù)組
    $tmpArr = array($token, $timestamp, $nonce);
    // use SORT_STRING rule
    //排序
    sort($tmpArr, SORT_STRING);
    //把排序后的數(shù)組轉換成字符串
    $tmpStr = implode( $tmpArr );
    //通過哈希算法加密
    $tmpStr = sha1( $tmpStr );
    //與加密簽名進行對比
    if( $tmpStr == $signature ){
      //相同返回true
      return true;
    }else{
      //不同返回false
      return false;
    }
  }
}
 
?>

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • PHP讓數(shù)組中有相同值的組成新的數(shù)組實例

    PHP讓數(shù)組中有相同值的組成新的數(shù)組實例

    下面小編就為大家分享一篇PHP讓數(shù)組中有相同值的組成新的數(shù)組實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2017-12-12
  • php實現(xiàn)QQ小程序發(fā)送模板消息功能

    php實現(xiàn)QQ小程序發(fā)送模板消息功能

    QQ小程序群里有伙伴要發(fā)送模板消息的代碼,所以今天給大家分享QQ小程序模板消息發(fā)布,絕對一步一步帶著大家走,每個細節(jié)都講到,感興趣的朋友跟隨小編一起看看吧
    2019-09-09
  • yii2.0之GridView自定義按鈕和鏈接用法

    yii2.0之GridView自定義按鈕和鏈接用法

    這篇文章主要介紹了yii2.0之GridView自定義按鈕和鏈接用法,是非常實用的使用GridView進行表單操作技巧,需要的朋友可以參考下
    2014-12-12
  • php中hashtable實現(xiàn)示例分享

    php中hashtable實現(xiàn)示例分享

    對php內(nèi)核有一定了解的人應該都知道php的精髓就是HashTable,這篇文章主要介紹了php中hashtable實現(xiàn)示例,需要的朋友可以參考下
    2014-02-02
  • php處理靜態(tài)頁面:頁面設置緩存時間實例

    php處理靜態(tài)頁面:頁面設置緩存時間實例

    本篇文章主要介紹了php處理靜態(tài)頁面:頁面設置緩存時間實例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • 創(chuàng)建無限極分類樹型結構的簡單方法

    創(chuàng)建無限極分類樹型結構的簡單方法

    下面小編就為大家?guī)硪黄獎?chuàng)建無限極分類樹型結構的簡單方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • PHP常見算法合集代碼實例

    PHP常見算法合集代碼實例

    這篇文章主要介紹了PHP常見算法合集代碼實例,有對算法不太了解的同學可以借鑒參考下
    2021-01-01
  • thinkphp中空模板與空模塊的用法實例

    thinkphp中空模板與空模塊的用法實例

    這篇文章主要介紹了thinkphp中空模板與空模塊的用法,以實例形式演示了空模板與空模塊具體實現(xiàn)方法,一般空模塊多用于系統(tǒng)找不到指定模塊的情況,如定位錯誤頁面與URL優(yōu)化等情況,具有一定的實用價值,需要的朋友可以參考下
    2014-11-11
  • PHP 范圍解析操作符(::)用法分析【訪問靜態(tài)成員和類常量】

    PHP 范圍解析操作符(::)用法分析【訪問靜態(tài)成員和類常量】

    這篇文章主要介紹了PHP 范圍解析操作符(::)用法,結合具體實例形式分析了PHP使用范圍解析操作符(::)訪問靜態(tài)成員和類常量相關操作技巧與注意事項,需要的朋友可以參考下
    2020-04-04
  • smarty半小時快速上手入門教程

    smarty半小時快速上手入門教程

    這篇文章主要介紹了smarty半小時快速上手入門教程,以實例的形式分析了smarty在實際使用過程中的屬性、方法及具體用法,需要的朋友可以參考下
    2014-10-10

最新評論