php微信公眾號(hào)開發(fā)之圖片回復(fù)
本文實(shí)例為大家分享了php微信公眾號(hào)開發(fā)之圖片回復(fù)的具體代碼,供大家參考,具體內(nèi)容如下
圖片回復(fù)
隨機(jī)函數(shù):
rand(1,10)

核心代碼:
$tyep= $postObj->MsgType;
$textTpl = "
<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
if ($tyep=="image")
{
$a=rand(1,3);
switch ($a)
{
case "1";
$b="健康好人緣,婚姻幸福美滿";
break;
case "2";
$b="貴人相助化險(xiǎn)為夷的好運(yùn)紋";
break;
default;
$b="才華橫溢好研究,知識(shí)豐富事業(yè)有成";
}
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time,$b);
echo $resultStr;
}
index.php代碼如下:
<?php
/**
* wechat php test
*/
//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->responseMsg();
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
//valid signature , option
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
public function responseMsg()
{
//get post data, May be due to the different environments
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
//extract post data
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$time = time();
$tyep= $postObj->MsgType;
$textTpl = "
<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
if ($tyep=="image")
{$a=rand(1,3);
switch ($a)
{case "1";
$b="健康好人緣,婚姻幸福美滿";
break;
case "2";
$b="貴人相助化險(xiǎn)為夷的好運(yùn)紋";
break;
default;
$b="才華橫溢好研究,知識(shí)豐富事業(yè)有成";
}
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time,$b);
echo $resultStr;
}
}else {
echo "";
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;
}
}
}
?>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- php微信公眾號(hào)開發(fā)模式詳解
- PHP實(shí)現(xiàn)微信公眾號(hào)驗(yàn)證Token的示例代碼
- php實(shí)現(xiàn)微信公眾號(hào)創(chuàng)建自定義菜單功能的實(shí)例代碼
- PHP實(shí)現(xiàn)的微信公眾號(hào)掃碼模擬登錄功能示例
- 微信公眾平臺(tái)開發(fā)教程③ PHP實(shí)現(xiàn)微信公眾號(hào)支付功能圖文詳解
- php微信公眾號(hào)開發(fā)之快遞查詢
- php微信公眾號(hào)開發(fā)之校園圖書館
- php微信公眾號(hào)開發(fā)之歡迎老朋友
- php微信公眾號(hào)開發(fā)之關(guān)鍵詞回復(fù)
- 基于PHP的微信公眾號(hào)的開發(fā)流程詳解
相關(guān)文章
php中實(shí)現(xiàn)簡(jiǎn)單的ACL 完結(jié)篇
終于寫完了 呵呵 簡(jiǎn)單的一個(gè)實(shí)現(xiàn),學(xué)習(xí)php的朋友可以參考下。2011-09-09
Codeigniter整合Tank Auth權(quán)限類庫(kù)詳解
相交其他CodeIgniter的類庫(kù),tank_auth,配置簡(jiǎn)單,使用也簡(jiǎn)單,并且作者也一直在更新。這篇文章主要介紹了Codeigniter整合Tank Auth權(quán)限類庫(kù)詳解,需要的朋友可以參考下2014-06-06
PHP二維數(shù)組排序的3種方法和自定義函數(shù)分享
這篇文章主要介紹了PHP二維數(shù)組排序的3種方法和自定義函數(shù)分享,需要的朋友可以參考下2014-04-04
php基于Redis消息隊(duì)列實(shí)現(xiàn)的消息推送的方法
這篇文章主要介紹了php基于Redis消息隊(duì)列實(shí)現(xiàn)的消息推送的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-11-11
Laravel創(chuàng)建數(shù)據(jù)庫(kù)表結(jié)構(gòu)的例子
今天小編就為大家分享一篇Laravel創(chuàng)建數(shù)據(jù)庫(kù)表結(jié)構(gòu)的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10

