Top Related Projects
📦 一个 PHP 微信 SDK
可能是我用过的最优雅的 Alipay/WeChat/Unipay 的 laravel 支付扩展包了
A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)
Laravel wrapper around OAuth 1 & OAuth 2 libraries.
An easy-to-use PHP QrCode generator with first-party support for Laravel.
Quick Overview
Laravel-WeChat is a comprehensive WeChat SDK for Laravel, providing easy integration of WeChat services into Laravel applications. It supports various WeChat APIs, including official accounts, mini programs, and payment services, making it a versatile tool for developers working with WeChat in the Laravel ecosystem.
Pros
- Seamless integration with Laravel, leveraging the framework's features and conventions
- Extensive support for various WeChat APIs, including official accounts, mini programs, and payment services
- Well-documented and actively maintained, with regular updates and community support
- Simplifies complex WeChat API interactions, reducing development time and effort
Cons
- Primarily focused on WeChat services, limiting its usefulness for non-WeChat related projects
- May have a learning curve for developers unfamiliar with WeChat's ecosystem and APIs
- Dependency on both Laravel and WeChat ecosystems may introduce complexity in project maintenance
Code Examples
- Configuring WeChat in Laravel:
// config/wechat.php
return [
'official_account' => [
'app_id' => env('WECHAT_OFFICIAL_ACCOUNT_APPID'),
'secret' => env('WECHAT_OFFICIAL_ACCOUNT_SECRET'),
'token' => env('WECHAT_OFFICIAL_ACCOUNT_TOKEN'),
'aes_key' => env('WECHAT_OFFICIAL_ACCOUNT_AES_KEY'),
],
// ... other configurations
];
- Sending a template message:
use EasyWeChat\Factory;
$app = Factory::officialAccount($config);
$result = $app->template_message->send([
'touser' => 'user-openid',
'template_id' => 'template-id',
'data' => [
'first' => 'Hello!',
'keyword1' => 'Welcome',
'keyword2' => 'to WeChat',
'remark' => 'Thank you for using our service.',
],
]);
- Handling WeChat payment:
use EasyWeChat\Factory;
$app = Factory::payment($config);
$result = $app->order->unify([
'body' => 'Product Name',
'out_trade_no' => 'order-id',
'total_fee' => 1, // Amount in cents
'trade_type' => 'JSAPI',
'openid' => 'user-openid',
]);
Getting Started
-
Install the package via Composer:
composer require overtrue/laravel-wechat
-
Publish the configuration file:
php artisan vendor:publish --provider="Overtrue\LaravelWeChat\ServiceProvider"
-
Configure your WeChat credentials in the
.env
file:WECHAT_OFFICIAL_ACCOUNT_APPID=your_app_id WECHAT_OFFICIAL_ACCOUNT_SECRET=your_app_secret WECHAT_OFFICIAL_ACCOUNT_TOKEN=your_token WECHAT_OFFICIAL_ACCOUNT_AES_KEY=your_aes_key
-
Use the WeChat facade in your Laravel application:
use EasyWeChat\Factory; $app = app('wechat.official_account'); $user = $app->user->get($openId);
Competitor Comparisons
📦 一个 PHP 微信 SDK
Pros of EasyWeChat
- More comprehensive and feature-rich, supporting a wider range of WeChat APIs
- Better documentation and examples, making it easier for developers to get started
- Actively maintained with frequent updates and bug fixes
Cons of EasyWeChat
- Steeper learning curve due to its extensive feature set
- May be overkill for simple WeChat integrations in Laravel projects
- Requires additional configuration when used with Laravel
Code Comparison
Laravel-WeChat:
use Overtrue\LaravelWeChat\Facade as WeChat;
$app = WeChat::officialAccount();
$user = $app->user->get($openId);
EasyWeChat:
use EasyWeChat\Factory;
$config = [
'app_id' => 'wx3cf0f39249eb0exxx',
'secret' => 'f1c242f4f28f735d4687abb469072xxx',
];
$app = Factory::officialAccount($config);
$user = $app->user->get($openId);
While both libraries provide similar functionality, EasyWeChat requires more initial setup but offers greater flexibility. Laravel-WeChat is more tightly integrated with Laravel, making it easier to use out of the box for Laravel projects. EasyWeChat's broader feature set and active development make it a strong choice for complex WeChat integrations, while Laravel-WeChat may be preferable for simpler Laravel-based applications.
可能是我用过的最优雅的 Alipay/WeChat/Unipay 的 laravel 支付扩展包了
Pros of laravel-pay
- Supports multiple payment gateways (Alipay, WeChat Pay, PayPal) in a single package
- Provides a unified API for different payment methods, simplifying integration
- Offers more extensive documentation and examples
Cons of laravel-pay
- Less focused on WeChat-specific features compared to laravel-wechat
- May have a steeper learning curve due to its multi-gateway approach
- Potentially larger package size due to supporting multiple payment systems
Code Comparison
laravel-pay:
use Yansongda\Pay\Pay;
$config = [
'alipay' => [ /* Alipay config */ ],
'wechat' => [ /* WeChat Pay config */ ],
];
$order = [
'out_trade_no' => '1234567890',
'total_amount' => '1',
'subject' => 'test subject',
];
$result = Pay::alipay($config)->web($order);
laravel-wechat:
use EasyWeChat\Factory;
$config = [
'app_id' => 'wx3cf0f39249eb0exx',
'mch_id' => '1483469312',
'key' => 'C91ADVZNRCD1CBAA4ADFC7E332705XX',
];
$app = Factory::payment($config);
$result = $app->order->unify([
'body' => 'subject',
'out_trade_no' => '1234567890',
'total_fee' => 1,
'trade_type' => 'JSAPI',
]);
A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)
Pros of laravel-mongodb
- Provides seamless integration with MongoDB for Laravel applications
- Supports complex queries and aggregations specific to MongoDB
- Offers Eloquent-like syntax for MongoDB operations
Cons of laravel-mongodb
- Limited to MongoDB-specific features, unlike laravel-wechat's broader WeChat integration
- May require more setup and configuration compared to laravel-wechat
- Potential learning curve for developers not familiar with MongoDB
Code Comparison
laravel-mongodb:
use App\Models\User;
$users = User::where('votes', '>', 100)
->take(10)
->get();
laravel-wechat:
use EasyWeChat\Factory;
$app = Factory::officialAccount($config);
$user = $app->user->get($openId);
Key Differences
- laravel-mongodb focuses on database integration, while laravel-wechat provides WeChat API integration
- laravel-mongodb extends Laravel's Eloquent ORM for MongoDB, whereas laravel-wechat offers WeChat-specific methods
- laravel-mongodb is more suitable for projects requiring MongoDB as the primary database, while laravel-wechat is ideal for applications needing WeChat functionality
Use Cases
- Choose laravel-mongodb for projects that require MongoDB as the primary database and need to leverage its unique features
- Opt for laravel-wechat when building Laravel applications that need to interact with WeChat's ecosystem and APIs
Laravel wrapper around OAuth 1 & OAuth 2 libraries.
Pros of Socialite
- Supports multiple OAuth providers (Facebook, Twitter, Google, etc.)
- Seamless integration with Laravel's authentication system
- Regularly maintained and updated by the Laravel team
Cons of Socialite
- Lacks specific WeChat integration features
- May require additional configuration for WeChat-specific OAuth flows
- Less specialized for Chinese social platforms
Code Comparison
Socialite:
return Socialite::driver('wechat')->redirect();
Laravel-WeChat:
$officialAccount = EasyWeChat::officialAccount();
return $officialAccount->oauth->redirect();
Key Differences
- Laravel-WeChat is specifically designed for WeChat integration, offering more WeChat-specific features
- Socialite provides a more generalized approach to social authentication
- Laravel-WeChat includes additional WeChat-specific services beyond just OAuth
Use Cases
- Choose Socialite for multi-provider social authentication in Laravel applications
- Opt for Laravel-WeChat when building WeChat-centric applications or requiring deeper WeChat integration
Community and Support
- Socialite benefits from the larger Laravel ecosystem and community
- Laravel-WeChat has a more focused community for WeChat-specific development
Integration Complexity
- Socialite offers simpler integration for general social auth scenarios
- Laravel-WeChat provides more comprehensive WeChat features but may have a steeper learning curve for WeChat-specific functionality
An easy-to-use PHP QrCode generator with first-party support for Laravel.
Pros of simple-qrcode
- Focused specifically on QR code generation, making it simpler to use for this purpose
- Supports various output formats (PNG, SVG, EPS) and styling options
- Lightweight and easy to integrate into Laravel projects
Cons of simple-qrcode
- Limited to QR code functionality, lacking broader WeChat API integration
- May require additional packages for more complex WeChat-related tasks
- Less frequent updates and smaller community compared to laravel-wechat
Code Comparison
simple-qrcode:
use SimpleSoftwareIO\QrCode\Facades\QrCode;
return QrCode::size(300)->generate('https://example.com');
laravel-wechat:
use EasyWeChat\Factory;
$app = Factory::officialAccount($config);
$result = $app->qrcode->temporary('foo', 6 * 24 * 3600);
While simple-qrcode focuses on generating QR codes with various options, laravel-wechat provides a more comprehensive set of tools for interacting with WeChat's API, including QR code generation as part of its functionality. The choice between the two depends on the specific requirements of your project and the extent of WeChat integration needed.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
EasyWeChat for Laravel
微信 SDK EasyWeChat for Laravelï¼ åºäº w7corp/easywechat
7.x èµ·ä¸åé»è®¤æ¯æ Lumenã
æ¡æ¶è¦æ±
- overtrue/laravel-wechat:^7.0 -> Laravel >= 8.0
- overtrue/laravel-wechat:^6.0 -> Laravel/Lumen >= 7.0
- overtrue/laravel-wechat:^5.1 -> Laravel/Lumen >= 5.1
å®è£
composer require overtrue/laravel-wechat:^7.2
é ç½®
- å建é ç½®æ件ï¼
php artisan vendor:publish --provider="Overtrue\\LaravelWeChat\\ServiceProvider"
- å¯éï¼æ·»å å«å
'aliases' => [
// ...
'EasyWeChat' => Overtrue\LaravelWeChat\EasyWeChat::class,
],
- æ¯ä¸ªæ¨¡ååºæ¬é½æ¯æå¤è´¦å·ï¼é»è®¤ä¸º
default
ã
使ç¨
:rotating_light: å¨ä¸é´ä»¶ App\Http\Middleware\VerifyCsrfToken
æé¤å¾®ä¿¡ç¸å
³çè·¯ç±ï¼å¦ï¼
protected $except = [
// ...
'wechat',
];
å¯¹äº Laravel 11.x å¯ä»¥ä½¿ç¨bootstrap/app.php
ä¸ç$middleware->validateCsrfTokens
æ¹æ³:
->withMiddleware(function (Middleware $middleware) {
$middleware->validateCsrfTokens(except: [
// ...
'wechat',
]);
})
ä¸é¢ä»¥æ¥æ¶æ®éæ¶æ¯ä¸ºä¾åä¸ä¸ªä¾åã
è·¯ç±ï¼
Route::any('/wechat', 'WeChatController@serve');
注æï¼ä¸å®æ¯
Route::any
, å 为微信æå¡ç«¯è®¤è¯çæ¶åæ¯GET
, æ¥æ¶ç¨æ·æ¶æ¯æ¶æ¯POST
ï¼
ç¶åå建æ§å¶å¨ WeChatController
ï¼
<?php
namespace App\Http\Controllers;
use Log;
class WeChatController extends Controller
{
public function serve()
{
Log::info('request arrived.');
$server = app('easywechat.official_account')->getServer();
$server->with(function($message){
return "欢è¿å
³æ³¨ overtrueï¼";
});
return $server->serve();
}
}
OAuth ä¸é´ä»¶
使ç¨ä¸é´ä»¶çæ
åµä¸ app/config/easywechat.php
ä¸ç oauth.callback
å°±é便填åå§(å 为ç¨ä¸çäº :smile:)ã
- å¨
app/Http/Kernel.php
ä¸æ·»å è·¯ç±ä¸é´ä»¶ï¼
protected $routeMiddleware = [
// ...
'easywechat.oauth' => \Overtrue\LaravelWeChat\Middleware\OAuthAuthenticate::class,
];
- å¨è·¯ç±ä¸æ·»å ä¸é´ä»¶ï¼
//...
Route::group(['middleware' => ['web', 'easywechat.oauth']], function () {
Route::get('/user', function () {
$user = session('easywechat.oauth_user.default'); // æ¿å°ææç¨æ·èµæ
dd($user);
});
});
ä¸é´ä»¶æ¯ææå®é
ç½®å称ï¼'easywechat.oauth:default'
ï¼å½ç¶ï¼ä½ ä¹å¯ä»¥å¨ä¸é´ä»¶åæ°æå®å½åç scopes
:
Route::group(['middleware' => ['easywechat.oauth:snsapi_userinfo']], function () {
// ...
});
// æè
æå®è´¦æ·çåæ¶æå® scopes:
Route::group(['middleware' => ['easywechat.oauth:default,snsapi_userinfo']], function () {
// ...
});
ä¸é¢çè·¯ç±å®ä¹äº /user
æ¯éè¦å¾®ä¿¡ææçï¼é£ä¹å¨è¿æ¡è·¯ç±çåè° æ æ§å¶å¨å¯¹åºçæ¹æ³éï¼ ä½ å°±å¯ä»¥ä» session('easywechat.oauth_user.default')
æ¿å°å·²ç»ææçç¨æ·ä¿¡æ¯äºã
模æææ
ææ¶åæ们å¸æå¨æ¬å°å¼åå®æå线ä¸æçå®ç走微信æææµç¨ï¼è¿å°åå°æ们çå¼åææ¬ï¼é£ä¹ä½ éè¦å以ä¸ä¸¤æ¥ï¼
- åå¤æ¨¡æææèµæï¼
use Illuminate\Support\Arr;
use Overtrue\Socialite\User as SocialiteUser;
$user = new SocialiteUser([
'id' => 'mock-openid',
'name' => 'overtrue',
'nickname' => 'overtrue',
'avatar' => 'http://example.com/avatars/overtrue.png',
'email' => null,
'original' => [],
'provider' => 'WeChat',
]);
以ä¸åæ®µå¨ scope 为
snsapi_userinfo
æ¶å°½å¯è½é ç½®é½å ¨å¦ï¼å½ç¶ï¼å¦æä½ ç模å¼åªæ¯snsapi_base
çè¯åªéè¦openid
就好äºã
- å°èµæåå ¥ sessionï¼
注æï¼ä¸å®è¦å¨è°ç¨ OAuth ä¸é´ä»¶ä¹ååå ¥ï¼æ¯å¦ä½ å¯ä»¥å建ä¸ä¸ªå ¨å±ä¸é´ä»¶æ¥å®æè¿ä»¶äºå¿ï¼åªå¨å¼åç¯å¢å¯ç¨å³å¯ã
session(['easywechat.oauth_user.default' => $user]); // åçï¼`default` å¯ä»¥æ´æ¢ä¸ºæ¨å¯¹åºçå
¶å®é
ç½®å
äºä»¶
ä½ å¯ä»¥çå¬ç¸åºçäºä»¶ï¼å¹¶å¯¹äºä»¶åçåæ§è¡ç¸åºçæä½ã
- OAuth ç½é¡µææï¼
Overtrue\LaravelWeChat\Events\WeChatUserAuthorized
// 该äºä»¶æ以ä¸å±æ§
$event->user; // å session('easywechat.oauth_user.default') ä¸æ ·
$event->isNewSession; // æ¯ä¸æ¯æ°çä¼è¯ï¼ç¬¬ä¸æ¬¡å建 session æ¶ä¸º trueï¼
$event->account; // å½åä¸é´ä»¶æ使ç¨çè´¦å·ï¼å¯¹åºå¨é
ç½®æ件ä¸çé
置项å称
å¼æ¾å¹³å°æ¯æ
æ¨å¯ä»¥éç¨å
ç½®ç Overtrue\LaravelWeChat\Traits\HandleOpenPlatformServerEvents
æ¥å¿«éå®æå¼æ¾å¹³å°çæå¡ç«¯éªè¯å·¥ä½ï¼
routes/web.php:
Route::any('/open-platform/server', OpenPlatformController::class);
app/Http/Controllers/OpenPlatformController.php:
<?php
namespace App\Http\Controllers;
use Overtrue\LaravelWeChat\Traits\HandleOpenPlatformServerEvents;
class OpenPlatformController extends Controller
{
use HandleOpenPlatformServerEvents;
public function __invoke(Application $application): \Psr\Http\Message\ResponseInterface
{
$app = app('easywechat.open_platform');
return $this->handleServerEvents($app);
}
}
Tips: é»è®¤ä¼æ ¹æ®å¾®ä¿¡å¼æ¾å¹³å°çæ¨éå 容触åå¦ä¸äºä»¶ï¼ä½ å¯ä»¥çå¬ç¸åºçäºä»¶å¹¶è¿è¡å¤çï¼
- æææ¹æåææï¼
Overtrue\LaravelWeChat\Events\OpenPlatform\Authorized
- æææ¹æ´æ°ææï¼
Overtrue\LaravelWeChat\Events\OpenPlatform\AuthorizeUpdated
- æææ¹åæ¶ææï¼
Overtrue\LaravelWeChat\Events\OpenPlatform\Unauthorized
- å¼æ¾å¹³å°æ¨é VerifyTicketï¼
Overtrue\LaravelWeChat\Events\OpenPlatform\VerifyTicketRefreshed
// äºä»¶æå¦ä¸å±æ§
$message = $event->payload; // å¼æ¾å¹³å°äºä»¶éç¥å
容
é
ç½®å http://example.com/open-platform/server
å为å¼æ¾å¹³å°ç¬¬ä¸æ¹åºç¨è®¾ç½®çææäºä»¶æ¥æ¶ URLã
æ´å¤ SDK çå ·ä½ä½¿ç¨è¯·åèï¼https://www.easywechat.com
:heart: Sponsor me
å¦æä½ å欢æç项ç®å¹¶æ³æ¯æå®ï¼ç¹å»è¿é :heart:
Project supported by JetBrains
Many thanks to Jetbrains for kindly providing a license for me to work on this and other open-source projects.
PHP æ©å±å å¼å
æ³ç¥éå¦ä½ä»é¶å¼å§æ建 PHP æ©å±å ï¼
è¯·å ³æ³¨æçå®æ课ç¨ï¼æä¼å¨æ¤è¯¾ç¨ä¸å享ä¸äºæ©å±å¼åç»éª ââ ãPHP æ©å±å å®ææç¨ - ä»å ¥é¨å°åå¸ã
License
MIT
Top Related Projects
📦 一个 PHP 微信 SDK
可能是我用过的最优雅的 Alipay/WeChat/Unipay 的 laravel 支付扩展包了
A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)
Laravel wrapper around OAuth 1 & OAuth 2 libraries.
An easy-to-use PHP QrCode generator with first-party support for Laravel.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot