以前没接触过微信公众平台开发,前几天刚找到实习公司就要求我做一个微信公众平台的应用,于是乎开始学习微信公众平台开发接口的调用,看开发文档之后还是不知道从何入手,只好上网找入门资料,终于在方倍工作室找到了完整的SDK,理解了思路之后其实也挺简单的,无非就是进行用户、微信服务器、开发者服务器三者之间的通信。下面是我参考方倍工作室SDK之后之间写的一个SDK,主要是响应文本消息和CLICK按钮消息,获取用户基本信息,当然并没有包含所有的消息类型和事件。更完整的SDK请参考方倍工作室,链接:http://www.cnblogs.com/txw1958/p/weixin-php-sdk.html。
$token = ‘qiduoyun‘;
$appid = 公众号appid;
$appsecret = 公众号appsecret;
$wechat = new Wechat($token,$appid,$appsecret);
if(!isset($_GET[‘echostr‘])) {
$wechat->responseMsg();
} else {
$wechat->valid();
}
class Wechat
{
private $token;
private $appid;
private $appsecret;
public function __construct($token,$appid,$appsecret)
{
$this->token = $token;
$this->appid = $appid;
$this->appsecret = $appsecret;
}
//验证消息真实性
public function valid()
{
$echostr = $_GET[‘echostr‘];
if($this->checkSignature()) {
echo $echostr;
exit;
}
}
//验证签名
private function checkSignature()
{
$token = $this->token;
$timestamp = $_GET[‘timestamp‘];
$nonce = $_GET[‘nonce‘];
$signature = $_GET[‘signature‘];
$tmpArr = array($token,$timestamp,$nonce);
sort($tmpArr,SORT_STRING);
$tmpStr = implode($tmpArr);
$tmpStr = sha1($tmpStr);
if($tmpStr == $signature) {
return true;
} else {
return false;
}
}
//获取access_token
private function get_access_token($appid,$appsecret)
{
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" .
$appid . "&secret=" . $appsecret;
$output = file_get_contents($url);
$jsoninfo = json_decode($output,true);
$access_token = $jsoninfo[‘access_token‘];
return $access_token;
}
//响应消息
public function responseMsg()
{
$postStr = $GLOBALS[‘HTTP_RAW_POST_DATA‘];
if(!empty($postStr)) {
$postObj = simplexml_load_string($postStr,‘SimpleXMLElement‘,LIBXML_NOCDATA);
$RX_TYPE = trim($postObj->MsgType);
switch($RX_TYPE)
{
case ‘text‘:
$result = $this->receiveText($postObj);
break;
case ‘event‘:
$result = $this->receiveEvent($postObj);
break;
default:
$result = "unknown message type: " . $RX_TYPE;
break;
}
echo $result;
} else {
echo ‘‘;
exit;
}
}
//接收文本消息
private function receiveText($object)
{
switch($object->Content)
{
case ‘猜猜我是谁‘: //发送单图文消息:用户基本信息
//获取access_token
$access_token = $this->get_access_token($this->appid,$this->appsecret);
//获取用户信息
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" .
$access_token . "&openid=" . $object->FromUserName . "&lang=zh_CN";
$output = file_get_contents($url);
$jsoninfo = json_decode($output,true);
if($jsoninfo[‘sex‘] == 1) {
$sex = "男";
} else if($jsoninfo[‘sex‘] == 2) {
$sex = "女";
} else {
$sex = "未知";
}
$content = array();
$content[] = array(‘Title‘ => "我知道你是谁!",‘Description‘ => "昵称:" . $jsoninfo[‘nickname‘] .
"\r\n" . "性别:" . $sex . "\r\n" . "国家:" . $jsoninfo[‘country‘] . "\r\n" . "省份:" .
$jsoninfo[‘province‘] . "\r\n" . "城市:" . $jsoninfo[‘city‘],‘PicUrl‘ => $jsoninfo[‘headimgurl‘],
‘Url‘ => ‘‘);
break;
default:
$content = ‘您发送的消息有误!‘;
break;
}
if(is_array($content)) {
if(isset($content[0][‘PicUrl‘])) {
$result = $this->transmitInfo($object,$content);
}
} else {
$result = $this->transmitText($object,$content);
}
return $result;
}
//接收事件推送
private function receiveEvent($object)
{
$content = "";
switch($object->Event)
{
case ‘subscribe‘:
$content = "欢迎关注*****!";
break;
case ‘unsubscribe‘:
$content = "取消关注";
break;
case ‘CLICK‘:
switch($object->EventKey)
{
case ‘我是谁‘:
//获取access_token
$access_token = $this->get_access_token($this->appid,$this->appsecret);
//获取用户信息
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" .
$access_token . "&openid=" . $object->FromUserName . "&lang=zh_CN";
$output = file_get_contents($url);
$jsoninfo = json_decode($output,true);
if($jsoninfo[‘sex‘] == 1) {
$sex = "男";
} else if($jsoninfo[‘sex‘] == 2) {
$sex = "女";
} else {
$sex = "未知";
}
$content = array();
$content[] = array(‘Title‘ => "我知道你是谁!",‘Description‘ => "昵称:" . $jsoninfo[‘nickname‘] .
"\r\n" . "性别:" . $sex . "\r\n" . "国家:" . $jsoninfo[‘country‘] . "\r\n" . "省份:" .
$jsoninfo[‘province‘] . "\r\n" . "城市:" . $jsoninfo[‘city‘],‘PicUrl‘ => $jsoninfo[‘headimgurl‘],
‘Url‘ => ‘‘);
break;
default:
$content = "该按钮暂时尚未添加事件!";
break;
}
break;
default:
$content = "对不起,目前暂不受理此事件!";
break;
}
if(is_array($content)) {
if(isset($content[0][‘PicUrl‘])) {
$result = $this->transmitInfo($object,$content);
}
} else {
$result = $this->transmitText($object,$content);
}
return $result;
}
//发送文本消息
private function transmitText($object,$content)
{
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);
return $result;
}
//发送单图文消息:用户基本信息
private function transmitInfo($object,$infoArray)
{
if(!is_array($infoArray)) {
return;
}
$itemTpl = "<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item> ";
$item_str = "";
foreach ($infoArray as $item){
$item_str .= sprintf($itemTpl, $item[‘Title‘], $item[‘Description‘], $item[‘PicUrl‘],
$item[‘Url‘]);
}
$infoTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<Content><![CDATA[]]></Content>
<ArticleCount>%s</ArticleCount>
<Articles> $item_str</Articles>
</xml>";
$result = sprintf($infoTpl, $object->FromUserName, $object->ToUserName, time(),
count($infoArray));
return $result;
}
}