PHP微信服务器端代码


<?php
//引入微信提供的包省的找依赖库
include_once "wxBizMsgCrypt.php";

//你自己的token、分配给你的appid和密码
define("TOKEN", "yourtoken");
define("APPID", "wxb21b4b3b11111111");
define("APPSECRET", "se8c89e213c8efab31d53fc98s6d2222");

$wechatObj = new wechatCallbackapiTest();

//验证服务器
if(isset($_GET["openid"])){

//检查token
$access_token = $wechatObj->getToken();
$wechatObj->responseMsg();

}else if (isset($_GET["echostr"])){
//服务器token验证
$wechatObj->valid();
}else{
echo "empty";
}


class wechatCallbackapiTest
{
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;
}
}

public function responseMsg()
{

$postStr = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : file_get_contents("php://input");
//$fp = fopen("./post_data.txt", "w+");
//fwrite($fp, $postStr);
//fclose($fp);

$postObj = simplexml_load_string($postStr,'SimpleXMLElement', LIBXML_NOCDATA);
$RX_TYPE = trim($postObj->MsgType);

switch($RX_TYPE)
{
case "text":
$resultStr = $this->handleText($postObj);
break;
case "event":
$resultStr = $this->handleEvent($postObj);
break;
default:
$resultStr = "Unknow msg type: ".$RX_TYPE;
break;
}
echo $resultStr;
}

//文本消息
public function handleText($postObj)
{
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$time = time();
$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>";
if(!empty( $keyword ))
{
$msgType = "text";
$contentStr = "你好啊^_^";
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else{
echo "Input something...";
}
}

public function handleEvent($object)
{
$contentStr = "";
switch ($object->Event)
{
//关注事件
case "subscribe":
$contentStr = "感谢您关注【我爱财】"."\n"."微信号:happysoul0";
break;
default :
$contentStr = "Unknow Event: ".$object->Event;
break;
}
$resultStr = $this->responseText($object, $contentStr);
return $resultStr;
}

//输出返回信息
public function responseText($object, $content, $flag=0)
{
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>%d</FuncFlag>
</xml>";
$resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $flag);
return $resultStr;
}


//获取token并写入本地
function getToken(){
//写入本地的文件名,虽然这样不安全,暂时可以这么调试
$fileName = "./happysoul_access_token.json";

$appid=APPID;
$appsecret=APPSECRET;
if(!is_file($fileName)){
$f = fopen($fileName, "w");
fclose($f);
}
$file = file_get_contents($fileName,true);
$result = json_decode($file,true);
if (time() > $result['expires']){
$data = array();
$data['access_token'] = $this->getNewToken($appid,$appsecret);
$data['expires']=time()+7000;
$jsonStr = json_encode($data);
$fp = fopen($fileName, "w");
fwrite($fp, $jsonStr);
fclose($fp);
return $data['access_token'];
}else{
return $result['access_token'];
}
}
function getNewToken($appid,$appsecret){
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$appsecret}";
$access_token_Arr = $this->https_request($url);
return $access_token_Arr['access_token'];
}
function https_request ($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$out = curl_exec($ch);
curl_close($ch);
return json_decode($out,true);
}

}

?>


附件是微信提供的包,包含 java php c++ c# python


公众平台 - 开发 - 基本配置 (服务器配置)记得验证好了服务器之后要点启用
关注了你的人给你公众号发消息才能发送到你的服务器上
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值