教程大纲
准备工作
介入方式
开发示范
从两方面来说
1 应用介入
2 插件介入
easywechat 具体使用方式请看 官方文档
微信 wechat-php-sdk类库直接看readname 或者代码
应用介入
找到thinkcmf 根目录 composer
composer require overtrue/wechat:~3.1 -vvv
然后就可以在thinkcmf使用
use EasyWeChat\Foundation\Application;
$options = [
'debug' => true,
'app_id' => 'your-app-id',
'secret' => 'you-secret',
'token' => 'easywechat',
// 'aes_key' => null,
// 可选'log' => ['level' => 'debug','file' => '/tmp/easywechat.log', // XXX: 绝对路径!!!!],//...
];
$app = new Application($options);
$response = $app->server->serve();// 将响应输出$response->send();
如果是自己写的类库 或者 github下的
请参考thinkcmf手册 类库扩展
插件介入
自己写的sdk类库或者下载 丢到lib目录 就行了
composer 用法 找到一个空目录 (假设目录名为wx)
composer require overtrue/wechat:~3.1 -vvv
写个入口文件index.php wx目录下添加
include__DIR__.'/vendor/autoload.php';
然后把它打包成phar包 压缩体积 (sdk也可以这样但是为了方便不推荐 打包成phar)
如何打包phar
首先需要修改php.ini配置将phar的readonly关闭,默认是不能写phar包的,include是默认开启的。
phar.readonly => On
创建一个phar压缩包
phar.php (index.php上级目录 也就是 wx外添加)
$phar = new Phar('esaywechat.phar');
$phar->buildFromDirectory(__DIR__.'/', '/\.php$/');
$phar->compressFiles(Phar::GZ);
使用一个phar压缩包
放到lib里在lib 创建一个类 然后引入
include_one('esaywechat.phar');
然后像介入应用一样使用
use EasyWeChat\Foundation\Application;
$options = [
'debug' => true,
'app_id' => 'your-app-id',
'secret' => 'you-secret',
'token' => 'easywechat',
// 'aes_key' => null,
// 可选'log' => ['level' => 'debug','file' => '/tmp/easywechat.log', // XXX: 绝对路径!!!!],//...
];
$app = new Application($options);
$response = $app->server->serve();// 将响应输出$response->send();
当然$options 可以改造成 插件配置