php开发微信公众号(1)
最近找了个方向提升自己的技术站,发现微信是一个不错的选择
1.入门
申请公众号这些都不说了,另外你必须需要php、h5、MySQL等基本知识才能看懂此文章,还需要有一个服务器及域名,由于我遇见问题也会查询大v的文章,遇见相同的不要骂我呦
’参考开发文档:
首先可以了解微信不同公众号的差距,个人只能申请订阅号,而订阅号会有许多的限制,包括大量的权限限制,所以在开发的时候新手建议选择公众平台测试账号进行开发
点击进入后,我们会看见让你用微信登录,然后配置接口信息,填写他给你的APPID和appsecret,然后使用自带的网页调试工具查询access_token
这里的token必须和你服务器的保持一致
填完之后点击提交,会发现提升token验证失败,不要急,因为此时我们还没有在服务端进行操作
贴上服务端代码
index.php
<?php
// 该代码块用于接收用户消息,根据用户输入的消息类型进行判断,文本,图片,视频,位置,链接,语音等,并取得值,处理后给予响应。
// 接收用户消息
// 微信公众账号接收到用户的消息类型判断
//
define(“TOKEN”, “thfythme”);
$wechatObj = new wechatCallbackapiTest();
if (!isset($_GET[\’echostr\’])) {
$wechatObj->responseMsg();
}else{
$wechatObj->valid();
}
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET[“echostr”];
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 = $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 “image”: //图片消息
$result = $this->receiveImage($postObj);
break;
case “voice”: //语音消息
$result = $this->receiveVoice($postObj);
break;
case “video”: //视频消息
$result = $this->receiveVideo($postObj);
break;
case “location”://位置消息
$result = $this->receiveLocation($postObj);
break;
case “link”: //链接消息
$result = $this->receiveLink($postObj);
break;
default:
$result = “unknow msg type: “.$RX_TYPE;
break;
}
echo $result;
}else {
echo “”;
exit;
}
}
/*
* 接收文本消息
*/
private function receiveText($object)
{
$content = “你发送的是文本,内容为:”.$object->Content;
$result = $this->transmitText($object, $content);
return $result;
}
/*
* 接收图片消息
*/
private function receiveImage($object)
{
$content = “你发送的是图片,地址为:”.$object->PicUrl;
$result = $this->transmitText($object, $content);
return $result;
}
/*
* 接收语音消息
*/
private function receiveVoice($object)
{
$content = “你发送的是语音,媒体ID为:”.$object->MediaId;
$result = $this->transmitText($object, $content);
return $result;
}
/*
* 接收视频消息
*/
private function receiveVideo($object)
{
$content = “你发送的是视频,媒体ID为:”.$object->MediaId;
$result = $this->transmitText($object, $content);
return $result;
}
/*
* 接收位置消息
*/
private function receiveLocation($object)
{
$content = “你发送的是位置,纬度为:”.$object->Location_X.”;经度为:”.$object->Location_Y.”;缩放级别为:”.$object->Scale.”;位置为:”.$object->Label;
$result = $this->transmitText($object, $content);
return $result;
}
/*
* 接收链接消息
*/
private function receiveLink($object)
{
$content = “你发送的是链接,标题为:”.$object->Title.”;内容为:”.$object->Description.”;链接地址为:”.$object->Url;
$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;
}
//点击菜单跳转时
}
?>
这时点击关注测试公众号,就会有惊喜哦