前段时间做了微信开放平台的项目,一下为此项目重点,其实就是源代码啦~ 当然删掉了一些数据库的操作以及敏感信息. 并感谢前人共享在网上的资料.更多的信息还请参考官方手册.
creatMenu();//创建菜单
$wechatObj = new wechatCallbackapi();
$wechatObj->weixin_run(); //执行接收器方法
function getCurl($url){//get https的内容
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//不输出内容
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$result = curl_exec($ch);
curl_close ($ch);
return $result;
}
function dataPost($post_string, $url) {//POST方式提交数据
$context = array (‘http’ => array (‘method’ => “POST”, ‘header’ => “User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) \r\n Accept: /“, ‘content’ => $post_string ) );
$stream_context = stream_context_create ( $context );
$data = file_get_contents ( $url, FALSE, $stream_context );
return $data;
}
class wechatCallbackapi
{
private $fromUsername;
private $toUsername;
private $times;
private $keyword;
private $keyevent;
private $event;
private $default_string_inbuilding = ‘建设中…’;
public function AutomaticReplyFromMenu($k){
//do some CURD
return $r;
}
public function AutomaticReplyFromKeywordDynamic($k){
//do some CURD
return $r;
}
public function weixin_run(){
$this->responseMsg();
// $arr\[\]=array("title1","Description1","http://www.chuangen.com/pic/hj/s_4.png","http://www.smesc.gov.cn/");
// $arr\[\]=array("title2","Description2","http://www.chuangen.com/pic/hj/s_4.png","http://www.smesc.gov.cn/");
// $arr\[\]=array("title3","Description3","http://www.chuangen.com/pic/hj/s_4.png","http://www.smesc.gov.cn/");
// $this->create_xml("news",$arr,array(2,0));
$default_string="
这里的信息可以
这样设置
你返回的微信信息也可以换行
拉啦拉啦这里是新的一行";
if($this->keyword){
// $arr\[\]="$this->keyword";
// $this->create_xml("text",$arr,array(2,0));
//$this->create_xml("news",$arr,array(2,0));
//判断关键词自动回复
//do something...
switch ($this->keyword) {
case '1001':
$r=$this->AutomaticReplyFromMenu(1001);
$this->create_xml("news",$r,array(2,0));
break;
case '1002':
//按照发送的关键词处理信息
case '?':
//keyword default.
break;
default:
$arr\[\]= $default_string;
$this->create_xml("text",$arr,array(2,0));
break;
}
}elseif(!$this->keyword){
if ($this->event=='CLICK') {//按照自定义菜单的值处理并生成返回信息.
switch ($this->keyevent){
case '1001':
$r=$this->AutomaticReplyFromMenu(1001);
$this->create_xml("news",$r,array(2,0));
break;
case '1002':
//do some by your keyword.
default:
//default.
} else {
$arr\[\]=$default_string;
$this->create_xml("text",$arr,array(2,0));
}
//根据自定义菜单点击事件判断关键词自动回复
}else{
$arr\[\]=$default_string;
$this->create_xml("text",$arr,array(2,0));
}
}
// 初始化信息
// fromUsername
// toUsername
// keyword
// times
public function responseMsg()// 初始化信息
{
$postStr = $GLOBALS\["HTTP\_RAW\_POST_DATA"\];
if (!empty($postStr)){
$postObj = simplexml\_load\_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$this->fromUsername = $postObj->FromUserName;
$this->toUsername = $postObj->ToUserName;
$this->keyword = trim($postObj->Content);//收到的内容
$this->times = time();
$this->keyevent = $postObj->EventKey;//事件keyword
$this->event = $postObj->Event;//事件
}else {
echo "this file only for weixin API!
SA“;
exit;
}
}
//微信封装类,
//type: text 文本类型, news 图文类型
//text,array(内容),array(ID)
//news,array(array(标题,介绍,图片,超链接),...小于10条),array(条数,ID)
private function create\_xml($type,$value\_arr,$o_arr=array(0)){
//=================xml header============
$con=" fromUsername}\]\]>
toUsername}\]\]>
{$this->times}
";
//=================type content============
switch($type){
case "text" :
$con.="
{$o_arr}";
break;
case "news" :
$con.="{$o_arr\[0\]}";
foreach($value_arr as $id=>$v){
if($id>=$o_arr\[0\]) break; else null; //判断数组数不超过设置数
$con.=" <!\[CDATA\[{$v\[0\]}\]\]> ";
}
$con.="{$o_arr\[1\]}";
break;
} //end switch
//=================end return============
echo $con."";
}
//===========default DO NOT MODIFY======//
public function valid()
{
$echoStr = $_GET\["echostr"\];
//valid signature , option
if($this->checkSignature()){
echo $echoStr;
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;
}
}
//===========default DO NOT MODIFY======//
}
class WechatMenu
{
private function getAccessToken() //获取access_token
{
$url = “https://api.weixin.qq.com/cgi-bin/token?grant\_type=client\_credential&appid=".AppId."&secret=".AppSecret;
$data = getCurl($url);//通过自定义函数getCurl得到https的内容
$resultArr = json_decode($data, true);//转为数组
return $resultArr[“access_token”];//获取access_token
}
public function creatMenu()//创建菜单
{
$accessToken = $this->getAccessToken();//获取access_token
$menuPostString = '
{
"button": \[
{
"name": "资讯",
"sub_button": \[
{
"type": "click",
"name": "通知公告",
"key": "1001"
},
{
"type": "click",
"name": "政策法规",
"key": "1002"
},
{
"type": "click",
"name": "服务体系",
"key": "1003"
},
{
"type": "click",
"name": "手机报",
"key": "1004"
}
\]
},
{
"name": "E服务",
"sub_button": \[
{
"type": "click",
"name": "服务产品",
"key": "2001"
},
{
"type": "click",
"name": "服务需求",
"key": "2002"
},
{
"type": "click",
"name": "服务专家",
"key": "2003"
},
{
"type": "click",
"name": "订单服务",
"key": "2004"
}
\]
},
{
"name": "互动",
"sub_button": \[
{
"type": "click",
"name": "调查问卷",
"key": "3001"
},
{
"type": "click",
"name": "联系我们",
"key": "3002"
}
\]
}
\]
}
';
$menuPostUrl = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$accessToken;//POST的url
$menu = dataPost($menuPostString, $menuPostUrl);//将菜单结构体POST给微信服务器
}
}
class sqltool
{
//do some sql.
}
?>