在CakePHP项目中集成EasyWeChat
来源:互联网 发布:天猫魔盒如何下载软件 编辑:程序博客网 时间:2024/06/03 21:19
EasyWeChat(Overtrue) 是一个开源的微信开发SDK,也是一个标准的Composer包,不受框架限制,因此可以很方便地集成到基于CakePHP的项目中,主要用来处理微信服务器把用户消息转到自有服务器(下图虚线返回部分)后的处理过程。
使用composer安装overtrue/wechat
$ composer require overtrue/wechat:~3.3.1 -vvv
安装成功后,会在项目目录下生成/vendor/overtrue/目录。
配置bootstrap.php
打开项目目录下的/config/bootstrap.php文件,添加如下代码,加载EasyWechat包。
//Load EasyWechat/Overtrueif (Configure::read('debug')) { Configure::load('wechat_test', 'default', false);}else{ Configure::load('wechat', 'default', false);}
创建wechat.php和wechat_test.php
在项目/config/目录下,创建EasyWeChat的配置文件,分别用于debug模式和发布模式,大致格式如下:
return [ 'base_url' => 'http://www.abc.com', //微信开发者模式配置的域名 'wx_config' => [ 'debug' => true, 'app_id' => '', 'secret' => '', 'token' => '', //'aes_key' => null, // 可选 'log' => [ 'level' => 'debug', 'file' => 'D:\tmp\easywechat.log' //Windows下使用绝对路径 ], 'oauth' => [ 'scopes' => ['snsapi_base'], 'callback' => 'http://www.abc.com/oauth_callback' ] ], 'wx_buttons' => [ [ 'type' => 'view', 'name' => '测试菜单', 'url' => '' ] ]];
创建组件
在项目src/Controller/Component目录下,创建WechatComponent.php组件文件,封装对EasyWeChat的常用调用方法。
namespace App\Controller\Component;use Cake\Controller\Component;use Cake\Core\Configure;use Cake\ORM\TableRegistry;use Composer\Config;use EasyWeChat\Foundation\Application;class WechatComponent extends Component{ private $controller; private $wxConfig; private $wxButtons; private $application; //初始化 public function initialize(array $config) { parent::initialize($config); $this->controller = $controller = $this->_registry->getController(); //微信配置参数 $this->baseURL = Configure::read('base_url'); $this->wxConfig = Configure::read('wx_config'); $this->wxButtons = Configure::read('wx_buttons'); $this->application = new Application($this->wxConfig); } //连接验证 public function join(){ $app = $this->application; $response = $app->server->serve(); $response->send(); exit(); } //设置菜单 public function setMenu(){ $app = $this->application; $menu = $app->menu; $menu->add($this->wxButtons); exit(); } //判断是否为微信浏览器访问 public function isWeChat(){ if(strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) { return true; }else return false; }}
配置路由routes.php
打开项目/config/目录下的routes.php文件,修改根目录路由设置,用于与微信的连接校验。
$routes->connect('/', ['controller' => 'Wechat', 'action' => 'join']);
创建相应的Controller
根据上面的路由配置,在项目src/Controller目录下创建WechatController.php文件,并调用WechatComponent.php定义的方法。
<?phpnamespace App\Controller;use App\Controller\AppController;/** * Wechat Controller * * @property */class WechatController extends AppController{ /** * 初始化 */ public function initialize() { parent::initialize(); $this->loadComponent('Wechat'); } /** * 公众号连接 */ public function index() { $this->Wechat->join(); } /** * 设置公众号菜单 */ public function setMenu() { $this->Wechat->setMenu(); }}
在公众号管理后台完成对自有服务器的校验
在微信公众号管理后台,在 开发->基本配置->服务器配置 选项中填写与上面wechat.php中一致的配置信息,点击提交。如果成功,则表明EasyWeChat包已成功集成到当前的CakePHP项目中。
服务器校验成功之后,可以根据自己的实际业务需要,重新配置路由。
设置微信菜单
在前面的WechatController.php中,我们已经定义了设置微信菜单的方式setMenu,在浏览器中访问类似如下格式的地址:http://www.abc.com/wechat/set-menu 调用该方法设置微信菜单,执行成功后则可以在自己的公众号中显示出来。
可能遇到的坑
如果你使用的是Windows系统的服务器,那可能会遇到 cURL error 60: SSL certificate problem: unable to get local issuer certificate 的错误,解决方法可参考EasyWeChat给出的方案:https://easywechat.org/zh-cn/docs/troubleshooting.html#curl-60-SSL-certificate-problem-unable-to-get-local-issuer-certificate 。
最新cacert.pem文件下载地址:http://curl.haxx.se/ca/cacert.pem
另外,需要WampServer正确开启php_curl扩展,具体可参考:http://www.hangge.com/blog/cache/detail_1420.html
注意:如果是64位的系统,还可能需要下载64位的curl.dll文件,覆盖WampServer目录下\bin\php\php5.6.25\ext 目录下的 同名文件,然后重启WAMP。
可能遇到的第二个坑
如果你用的是PHP5.6+的版本,还可能遇到 Use of undefined constant CURLOPT_CLOSEPOLICY 的错误,这是因为PHP5.6中去掉了这个变量。
具体到EasyWeChat这个包,注释掉项目目录下vendor/overtrue/wechat/src/Core/Http.php文件中的如下代码,问题解决。
protected static $defaults = [// 'curl' => [// CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4,// ], ];
- 在CakePHP项目中集成EasyWeChat
- 在CakePHP中使用swfObject
- cakephp: 在ide中执行cakephp test case
- thinkphp5使用easywechat集成微信扫码支付
- cakephp: 在条件中使用sql函数
- [CakePHP] 在Controller中使用Helper
- CakePHP: 在控制器中动态切换数据库连接
- 如何在CakePHP中使用Ajax请求
- 在CakePHP中实现文件下载
- 在项目中集成Pentaho Report Designer
- 在SSH项目中集成DWR
- 在java web项目中集成webservice
- 在web项目中集成Spring
- 在Android项目中集成AndroidAnnotations注解
- 在java web项目中集成webservice
- 在C/C++项目中集成Lua
- (十九)在现有项目中集成ReactNaitve
- 在项目中集成jetty server
- linux中ls源码简单分析
- Win64 驱动内核编程-20.UnHook SSDT
- Unreal 入门-EQS
- Spring之IoC
- 数据平台
- 在CakePHP项目中集成EasyWeChat
- python 网络爬虫入门(四)— 破解pexels高清原图
- Hibernate常用API
- Source Insight-常用快捷键
- android studio toolbar遮挡住下面控件内容的解决方法
- CSS3响应式 Web 设计(三)@media 查询
- performSelector:target:argument:order:modes:和cancelPerformSelector:target:argument:的使用
- 杭电2003求绝对值
- Class Diagram-UML类图几种关系总结