您现在的位置是:网站首页 > 心得笔记
微信sdk的实现
简介微信sdk就是把一些公用的方法封装起来,方便了代码的管理,保证了我们代码的整洁性
1、微信sdk的介绍
sdk就是一个方法的封装集
2、 sdk的特性
1.便捷:你把你经常用到的方法写到sdk中,你可以在其他项目开发过程中,引入sdk,就可以调用相应的方法,就不需要再其他项目开发中再去写相同的代码
2.可重复利用性:同上讲解
3、 编写自己的sdk
参照PHP实现微信公众平台开发—提升篇,继续讲解微信sdk
3.1、将TestController中的单文本回复、多图文回复以及关注事件消息回复抽空封装成sdk
public function api()
{
$echoStr = isset($_GET["echostr"]) ? $_GET["echostr"] : '';
if (!empty($echoStr) && $this->checkSignature()) {
echo $echoStr;
exit;
} else {
$postStr = file_get_contents("php://input", 'r');
//写入日志 在同级目录下建立php_log.txt
error_log(var_export($postStr,1),3,'php_log.txt');
if (!empty($postStr)){
libxml_disable_entity_loader(true);
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
//1、订阅时间——SDK
$test = New Test();//前提是use App\Model\Admin\Test;
if($postObj->Event=="subscribe") {
$test->responseSubscribe($postObj);
}
//订阅事件——源代码(非sdk)
// if($postObj->Event=="subscribe") {
// $template = "<xml>
// <ToUserName><![CDATA[%s]]></ToUserName>
// <FromUserName><![CDATA[%s]]></FromUserName>
// <CreateTime>%s</CreateTime>
// <MsgType><![CDATA[%s]]></MsgType>
// <Content><![CDATA[%s]]></Content>
// </xml>";
//
// $msgType = "text";
// $contentStr = "欢迎关注楠辕贝辙,微信Y348033046";
// $resultStr = sprintf($template, $fromUsername,$toUsername, $time, $msgType, $contentStr);
// echo $resultStr;
// }
//2、多图文事件——SDK
if( strtolower($postObj->MsgType) == 'text' && trim($postObj->Content)=='tuwen' ) {
$arr = array(//从数据库中得到或自定义
array(
'title' => 'imooc',
'description' => "imooc is very cool",
'picUrl' => 'https://www.chuangkit.com/ruanjian/assets/i/002.jpg',
'url' => 'http://www.imooc.com',
),
array(
'title' => 'hao123',
'description' => "hao123 is very cool",
'picUrl' => 'https://www.baidu.com/img/bdlogo.png',
'url' => 'http://www.hao123.com',
),
array(
'title' => 'qq',
'description' => "qq is very cool",
'picUrl' => 'http://www.imooc.com/static/img/common/logo.png',
'url' => 'http://www.qq.com',
),
);
$test->responseNews($postObj, $arr);
}
//多图文事件——源代码(非sdk)
// if( strtolower($postObj->MsgType) == 'text' && trim($keyword)=='tuwen' ){
// $arr = array(
// array(
// 'title'=>'imooc',
// 'description'=>"imooc is very cool",
// 'picUrl'=>'https://www.chuangkit.com/ruanjian/assets/i/002.jpg',
// 'url'=>'http://www.imooc.com',
// ),
// array(
// 'title'=>'hao123',
// 'description'=>"hao123 is very cool",
// 'picUrl'=>'https://www.baidu.com/img/bdlogo.png',
// 'url'=>'http://www.hao123.com',
// ),
// array(
// 'title'=>'qq',
// 'description'=>"qq is very cool",
// 'picUrl'=>'http://www.imooc.com/static/img/common/logo.png',
// 'url'=>'http://www.qq.com',
// ),
// );
// $template = "<xml>
// <ToUserName><![CDATA[%s]]></ToUserName>
// <FromUserName><![CDATA[%s]]></FromUserName>
// <CreateTime>%s</CreateTime>
// <MsgType><![CDATA[%s]]></MsgType>
// <ArticleCount>".count($arr)."</ArticleCount>
// <Articles>";
// foreach ($arr as $key => $value) {
// $template .= "<item>
// <Title><![CDATA[".$value['title']."]]></Title>
// <Description><![CDATA[".$value['description']."]]></Description>
// <PicUrl><![CDATA[".$value['picUrl']."]]></PicUrl>
// <Url><![CDATA[".$value['url']."]]></Url>
// </item>";
// }
// $template .= "</Articles>
// </xml>";
// $msgType = "news";
// echo sprintf($template, $fromUsername,$toUsername, $time, $msgType);
// }
//3、单文本自动回复——SDK
$test->responseText($postObj);
//单文本自动回复——源代码(非sdk)
// if(!empty( $postObj->Content )) {
// switch ( trim($postObj->Content) ) {
// case 1:
// $content = '您输入的数字是1';
// break;
// case 2:
// $content = '您输入的数字是2';
// break;
// case 3:
// $content = '您输入的数字是3';
// break;
// case 4:
// $content = '您输入的数字是4';
// break;
// case 'imooc':
// $content = '<a href="http://www.imooc.com">imooc</a> is cool';
// break;
// case 5:
// $content = '微信sdk is very useful';
// break;
// default:
// $content = '未找到相关内容';
// break;
// }
// $template = "<xml>
// <ToUserName><![CDATA[%s]]></ToUserName>
// <FromUserName><![CDATA[%s]]></FromUserName>
// <CreateTime>%s</CreateTime>
// <MsgType><![CDATA[%s]]></MsgType>
// <Content><![CDATA[%s]]></Content>
// </xml>";
// $msgType = "text";
// $content = $content;
// $resultStr = sprintf($template, $fromUsername, $toUsername, $time, $msgType, $content);
// echo $resultStr;
// } else {
// echo "Input something...";
// }
} else {
echo "";
exit;
}
}
}3.2、新建并编写Test.php模型
<?php
namespace App\Model\Admin;
use Illuminate\Database\Eloquent\Model;
class Test extends Model
{
//回复多图文类型的微信消息
public function responseNews ($postObj, $arr) {
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$time = time();
$template = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<ArticleCount>".count($arr)."</ArticleCount>
<Articles>";
foreach ($arr as $key => $value) {
$template .= "<item>
<Title><![CDATA[".$value['title']."]]></Title>
<Description><![CDATA[".$value['description']."]]></Description>
<PicUrl><![CDATA[".$value['picUrl']."]]></PicUrl>
<Url><![CDATA[".$value['url']."]]></Url>
</item>";
}
$template .= "</Articles>
</xml>";
$msgType = "news";
echo sprintf($template, $fromUsername,$toUsername, $time, $msgType);
}
//回复单文本的微信消息
public function responseText ($postObj) {
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$time = time();
switch ( trim($postObj->Content) ) {
case 1:
$content = '您输入的数字是1';
break;
case 2:
$content = '您输入的数字是2';
break;
case 3:
$content = '您输入的数字是3';
break;
case 4:
$content = '您输入的数字是4';
break;
case 'imooc':
$content = '<a href="http://www.imooc.com">imooc</a> is cool';
break;
case 5:
$content = '微信sdk is very useful';
break;
default:
$content = '未找到相关内容';
break;
}
$template = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$msgType = "text";
$content = $content;
$resultStr = sprintf($template, $fromUsername, $toUsername, $time, $msgType, $content);
echo $resultStr;
}
//关注事件的微信消息
public function responseSubscribe ($postObj) {
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$time = time();
$template = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$msgType = "text";
$content = "欢迎关注楠辕贝辙,微信Y348033046";
$resultStr = sprintf($template, $fromUsername,$toUsername, $time, $msgType, $content);
echo $resultStr;
}
}在微信公众号上执行下效果图:
