开始
申请一个注册号,也可以自己去申请公众号
附上网址:微信公众平台 (qq.com)
去认证
验证通过才可正常使用,token可以自己填写
写响应路由:
写一个路由去响应微信发送的token验证
//配置的路由
Route::any('first',[\App\Http\Controllers\WechatController::class,'first']);
//对应的token认证方法
public function first(){
$config = [
'app_id' => 'wx713771e6c65fXXXXXXXX',
'secret' => '3e3827fa5292dbf032176bXXXXXXXXX',
'response_type' => 'array',
];
$app = Factory::officialAccount($config);
$response = $app->server->serve();
$response->send();exit; // Laravel 里请使用:return $response;
}
写功能:
响应成功,就可以开发了,这里举个栗子(公众号被关注)
// //关注提示
$app = Factory::officialAccount($config);
$response = $app->server->serve();
$app->server->push(function ($message) {
return "welcome for my channel~";
});
$response = $app->server->serve();
$app->auto_reply->current();
以上