yoozi / weixin
此包已被废弃,不再维护。未建议替代包。
PHP库,用于与微信API交互。
1.0.1
2015-12-17 03:02 UTC
Requires
- php: >=5.3.0
- illuminate/support: ~4.0
- rmccue/requests: >=1.0
This package is not auto-updated.
Last update: 2023-05-13 08:51:32 UTC
README
此库已弃用且不再维护,请使用overtrue/wechat代替。
此库是Golem项目的一部分,更多信息请见yoozi/golem。
内容
安装
您可以通过Composer安装此库
$ composer require yoozi/weixin --save
或在composer.json
中声明
{ "require": { "yoozi/weixin": "dev-master" } }
然后安装它
$ composer install
设置
配置
在开始使用此包之前,您必须通过以下方式发布配置文件
$ php artisan config:publish yoozi/weixin
它将在your-project/app/config/packages/yoozi/weixin/
中创建一个配置文件。
您应该在weixin.php
中设置您的微信账户信息
名称 | 描述 |
---|---|
token | 服务器端token(必需) |
app_id | 您的微信账户app id(必需) |
app_secret | 您的微信账户app secret密钥(必需) |
end_point | 微信服务器事件接收端点(必需) |
服务 & 门面
为了启用此包,您应该在config/app.php
中添加以下行
return array( 'providers' => array( // Illumniate stuffs... 'Illuminate\Translation\TranslationServiceProvider', 'Illuminate\Validation\ValidationServiceProvider', 'Illuminate\View\ViewServiceProvider', 'Illuminate\Workbench\WorkbenchServiceProvider', // Add weixin provider here: 'Yoozi\Weixin\WeixinServiceProvider', ), 'aliases' => array( 'Request' => 'Illuminate\Support\Facades\Request', // Be sure change the Response facade to weixin's: 'Response' => 'Yoozi\Weixin\Facades\Response', 'Route' => 'Illuminate\Support\Facades\Route', // Illuminate stuffs... 'URL' => 'Illuminate\Support\Facades\URL', 'Validator' => 'Illuminate\Support\Facades\Validator', 'View' => 'Illuminate\Support\Facades\View', // Add weixin facades here: 'WeixinInput' => 'Yoozi\Weixin\Facades\WeixinInput', 'WeixinRouter' => 'Yoozi\Weixin\Facades\WeixinRouter', 'WeixinMessage' => 'Yoozi\Weixin\Facades\WeixinMessage', 'OAuthClient' => 'Yoozi\Weixin\Facades\OAuthClient', 'WeixinClient' => 'Yoozi\Weixin\Facades\WeixinClient', ), );
使用
微信将为您提供
- 一个用于绑定服务器事件回调的路由器
- 一个用于与
api.weixin.qq.com
交互的微信客户端 - 一个用于执行OAuth登录的OAuth客户端
绑定事件
在routes.php
中
// Bind a text event. WeixinRouter::bind('text', 'MyWeixinEventHandler@text'); // Bind a music event. WeixinRouter::bind('music', 'MyWeixinEventHandler@music'); // Bind a subscribe event. WeixinRouter::bindEvent('subscribe', 'MyWeixinEventHandler@subscribe'); // This is equivalent to: WeixinRouter::bind('event:subscribe', 'MyWeixinEventHandler@subscribe'); // Bind a click event. WeixinRouter::bindClick('a_key', 'MyWeixinEventHandler@clickAKey'); // This is equivalent to: WeixinRouter::bindEvent('click:a_key', 'MyWeixinEventHandler@subscribe'); // And: WeixinRouter::bind('event:click:a_key', 'MyWeixinEventHandler@subscribe'); // Bind a view event. WeixinRouter::bindView('http://google.com', 'MyWeixinEventHandler@visitGoogle'); // Bind a default event. WeixinRouter::bindDefault('MyWeixinEventHandler@defaultEvent');
在MyWeixinEventHandler.php
中
class MyWeixinEventHandler { public function text() { $sender = WeixinInput::get('tousername'); $receiver = WeixinInput::get('fromusername'); $messge = WeixinMessage::text($receiver, $sender, 'Hello, world!'); return Response::xml($message); } // Handle other events... }
微信客户端
微信客户端可用于
- 获取access token
- 通过openid获取用户的简要资料
示例
// Notes: // You should store this access token in the cache or somewhere else // for reuse later. $accessToken = WeixinClient::getAccessToken(); $openId = 'an_user_openid'; var_dump(WeixinClient::getUserInfo($openId, $accessToken));
OAuth客户端
微信OAuth客户端为您提供
- 带有回调的授权url生成
- 使用OAuth代码获取access token
- 使用access token获取用户的简要资料
示例
// Redirect user to $authorizeUrl and receive the OAuth code ($code) $authorizeUrl = OAuthClient::getAccessUrl($codeReceiveUrl); // Get access token from code $accessTokenAndOpenId = OAuthClient::getAccessToken($code); $accessToken = $accessTokenAndOpenId['access_token']; $openId = $accessTokenAndOpenId['openid']; // Get user profile var_dump(OAuthClient::getUserInfo($openId, $accessToken));