PHP微信API接口類
更新時(shí)間:2016年08月22日 17:10:17 作者:shyhero
很全面詳細(xì)的PHP微信API接口類,幫助大家更好的進(jìn)行php微信開發(fā),感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了PHP微信接口類,供大家參考,具體內(nèi)容如下
<?php
/**
* wechat php test
*/
//define your token
//定義TOKEN秘鑰
define("TOKEN", "weixin");
//實(shí)例化微信對(duì)象
$wechatObj = new wechatCallbackapiTest();
//驗(yàn)證成功后注釋valid方法
//$wechatObj->valid();
//開啟自動(dòng)回復(fù)功能
$wechatObj->responseMsg();
//定義類文件
class wechatCallbackapiTest
{
//實(shí)現(xiàn)valid驗(yàn)證方法:實(shí)現(xiàn)對(duì)接微信公眾平臺(tái)
public function valid()
{
//接受隨機(jī)字符串
$echoStr = $_GET["echostr"];
//valid signature , option
//進(jìn)行用戶數(shù)字簽名驗(yàn)證
if($this->checkSignature()){
//如果成功,則返回接受到的隨機(jī)字符串
echo $echoStr;
//退出
exit;
}
}
//定義自動(dòng)回復(fù)功能
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進(jìn)行xml解析
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
//接受微信的手機(jī)端
$fromUsername = $postObj->FromUserName;
//微信公眾平臺(tái)
$toUsername = $postObj->ToUserName;
//接受用戶發(fā)送的關(guān)鍵詞
$keyword = trim($postObj->Content);
//1.接受用戶消息類型
$msgType = $postObj -> MsgType;
//時(shí)間戳
$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 ))
{
/*這是一個(gè)實(shí)例
//如果發(fā)送文本信息
$msgType = "text";
//回復(fù)內(nèi)容
if($keyword == "李楠"){
$contentStr = "叫我干嘛";
}else{
$contentStr = "叫我干嘛";
}
//格式化xml模板,參數(shù)與上面的模板是一一對(duì)應(yīng)的.fromUsername和頭Username是相反的,只寫帶%s的
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
//將xml信息返回給客戶端
echo $resultStr;
*/
if($keyword == "?" || $keyword == "?"){
$msgType = "text";
$contentStr = "1.特種服務(wù)號(hào)碼\n2.通訊服務(wù)號(hào)碼";
$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.中國(guó)移動(dòng):10086\n2.中國(guó)聯(lián)通:10010";
$resultStr = sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);
echo $resultStr;
}
}else{
echo "不能不說話";
}
}
////////////////////////////////////////////////////////////////////////////////////
//接受圖片信息
if($msgType == "image"){
//如果發(fā)送文本信息
$msgType = "text";
//回復(fù)內(nèi)容
$contentStr = "你發(fā)送的是圖片文件";
//格式化字符串
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
//將xml信息返回給客戶端
echo $resultStr;
}
////////////////////////////////////////////////////////////////////////////////////
if($msgType == "voice"){
//如果發(fā)送文本信息
$msgType = "text";
//回復(fù)內(nèi)容
$contentStr = "你發(fā)送的是語音文件";
//格式化字符串
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
//將xml信息返回給客戶端
echo $resultStr;
}
////////////////////////////////////////////////////////////////////////////////////
if($msgType == "video"){
//如果發(fā)送文本信息
$msgType = "text";
//回復(fù)內(nèi)容
$contentStr = "你發(fā)送的是視頻文件";
//格式化字符串
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
//將xml信息返回給客戶端
echo $resultStr;
}
////////////////////////////////////////////////////////////////////////////////////
if($msgType == "shortvideo"){
//如果發(fā)送文本信息
$msgType = "text";
//回復(fù)內(nèi)容
$contentStr = "你發(fā)送的是小視頻文件";
//格式化字符串
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
//將xml信息返回給客戶端
echo $resultStr;
}
////////////////////////////////////////////////////////////////////////////////////
if($msgType == "location"){
//如果發(fā)送文本信息
$msgType = "text";
//回復(fù)內(nèi)容
$contentStr = "你發(fā)送的是地理位置文件";
//格式化字符串
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
//將xml信息返回給客戶端
echo $resultStr;
}
////////////////////////////////////////////////////////////////////////////////////
if($msgType == "link"){
//如果發(fā)送文本信息
$msgType = "text";
//回復(fù)內(nèi)容
$contentStr = "你發(fā)送的是連接文件";
//格式化字符串
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
//將xml信息返回給客戶端
echo $resultStr;
}
////////////////////////////////////////////////////////////////////////////////////
/*
//判斷用戶發(fā)送關(guān)鍵詞是否為空
if(!empty( $keyword ))
{
//如果發(fā)送文本信息
$msgType = "text";
//回復(fù)內(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,如果沒有就拋出一個(gè)異常
if (!defined("TOKEN")) {
throw new Exception('TOKEN is not defined!');
}
$signature = $_GET["signature"];//接受微信加密簽名
$timestamp = $_GET["timestamp"];//接受時(shí)間戳
$nonce = $_GET["nonce"];//接受隨機(jī)數(shù)
$token = TOKEN;//把TOKEN常量賦值給$token
//把相關(guān)參數(shù)組裝成數(shù)組
$tmpArr = array($token, $timestamp, $nonce);
// use SORT_STRING rule
//排序
sort($tmpArr, SORT_STRING);
//把排序后的數(shù)組轉(zhuǎn)換成字符串
$tmpStr = implode( $tmpArr );
//通過哈希算法加密
$tmpStr = sha1( $tmpStr );
//與加密簽名進(jìn)行對(duì)比
if( $tmpStr == $signature ){
//相同返回true
return true;
}else{
//不同返回false
return false;
}
}
}
?>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- PHP開發(fā)api接口安全驗(yàn)證操作實(shí)例詳解
- PHP開發(fā)api接口安全驗(yàn)證的實(shí)例講解
- PHP API接口必備之輸出json格式數(shù)據(jù)示例代碼
- PHP實(shí)現(xiàn)的同步推薦操作API接口案例分析
- PHP微信紅包API接口
- PHP下使用CURL方式POST數(shù)據(jù)至API接口的代碼
- PHP本地進(jìn)行API接口測(cè)試的實(shí)例
- 支付寶服務(wù)窗API接口開發(fā)php版本
- php curl操作API接口類完整示例
- PHP如何使用JWT做Api接口身份認(rèn)證的實(shí)現(xiàn)
- PHP開發(fā)API接口簽名生成及驗(yàn)證操作示例
相關(guān)文章
基于Discuz security.inc.php代碼的深入分析
本篇文章是對(duì)Discuz security.inc.php的代碼進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
Codeigniter實(shí)現(xiàn)智能裁剪圖片的方法
這篇文章主要介紹了Codeigniter實(shí)現(xiàn)智能裁剪圖片的方法,可以做到裁剪后不失真,盡可能保留圖片主題含義。需要的朋友可以參考下2014-06-06
Symfony2實(shí)現(xiàn)在doctrine中內(nèi)置數(shù)據(jù)的方法
這篇文章主要介紹了Symfony2實(shí)現(xiàn)在doctrine中內(nèi)置數(shù)據(jù)的方法,結(jié)合實(shí)例形式分析了在doctrine中內(nèi)置數(shù)據(jù)的具體步驟與相關(guān)技巧,需要的朋友可以參考下2016-02-02
php設(shè)計(jì)模式之代理模式分析【星際爭(zhēng)霸游戲案例】
這篇文章主要介紹了php設(shè)計(jì)模式之代理模式,結(jié)合星際爭(zhēng)霸游戲案例形式分析了PHP設(shè)計(jì)模式中代理模式的相關(guān)原理、使用方法與操作注意事項(xiàng),需要的朋友可以參考下2020-03-03
Yii2針對(duì)指定url的生成及圖片等的引入方法小結(jié)
這篇文章主要介紹了Yii2針對(duì)指定url的生成及圖片等的引入方法,針對(duì)常用的URL格式及圖片路徑操作結(jié)合實(shí)例進(jìn)行了對(duì)比分析,需要的朋友可以參考下2016-07-07
windows安裝composer并更換國(guó)內(nèi)鏡像的過程詳解
這篇文章主要給大家介紹了windows安裝composer并更換國(guó)內(nèi)鏡像的過程詳解,文中有詳細(xì)的流程介紹,通過圖文結(jié)合講解的非常詳細(xì),需要的朋友可以參考下2023-11-11

