Wechat(WeiXin) Web Api Based On Laravel PHP Framework
中文文档:基于Laravel PHP框架的微信网页版Api
Features
7*24
hours no-end running, easy to use, support sending/receiving multi type messages graceful system architecture, supportcross platform
development and flexible customextensions
supportRestful Api
,async message processing
andmessage event broadcasting
based onPhp
, the best language in The World! :)
System Architecture
This System is composed of 3 independent components TheMiddle
component is core of The System, doing qrcode scan & message listening; I do a lot of work for pupose of robust. Meantime, as a connector, it supports message-sending ability for theLeft
component, and pushs simple-formated message to theRight
component. Run commandphp artisan wechat:listen
to make it working. TheLeft
component is used for sending message, the user can send message to any friends in his code. classAppConsoleCommandsWechatSend
is a sample, run commandphp artisan wechat:send
to make it working. TheRight
component is used for processing message, the user can do anything for the coming message. The jobAppJobsProcessWechatMessage
process each message into more formatted message(text,share,image,voice,file, etc). Next, It fires theAppEventsWechatMessageEvent
event to the subscribers, who can do some custom things with the message. classAppListenersSaveWechatMessageListener
is a sample that saving the message into the DB.Advantage
: The three components are 3 independent Process. this can grantee the non-ending running of theMiddle
part, at the same time, the user can do any extensions in theLeft
andRight
part without disturbing theMiddle
part.
Prerequisite
php 5.6 or more php composer redis (optional) mysql (optional)
Usage
1. Installation
git clone https://github.com/mistcheng/sowechat.git cd sowechat composer install
2. Configration
2.1 config/wechat.php
is wechat config file
<?php return [ 'debug' => env('WECHAT_DEBUG', false), // debug mode 'web_api' => [ 'connect_timeout' => 30, // http request timeout 'max_attempts' => 10, // max request attempts in half minutes ], 'job' => [ 'connection' => env('QUEUE_DRIVER', 'database'), // wehcat message queu engine, recommend database|redis 'queue' => env('JOB_QUEUE', 'default'), // queue name ], ];
2.2 Queue Configuration (Recommend database or redis, never use async)
In section
2.1
, the optionwechat.job.connection
should be configured in the fileconfig/database.php
.
2.2.1 if set wechat.job.connection
withdatabase
, the option database.connections.mysql
must be configured correctly
'connections' => [ 'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', 'localhost'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => '', 'strict' => true, 'engine' => null, ], ],
2.2.2 if set wechat.job.connection
withredis
, the option database.redis
should be configured properly.
'redis' => [ 'cluster' => false, 'default' => [ 'host' => env('REDIS_HOST', 'localhost'), 'password' => env('REDIS_PASSWORD', null), 'port' => env('REDIS_PORT', 6379), 'database' => 0, ], ],
2.3 Running
2.3.1 First time, init DB script
cd sowechat php artisan migrate
2.3.2 Run the Middle
component for new
php artisan wechat:listen --new
There will be a new qrcode in the folder
storage/app/wechat
, use your wechat scaning it to login.
2.3.3 Run the Middle
component without re-login
php artisan wechat:listen
Run the command without passing argument --new
2.3.4 Processing wechat message
php artisan queue:work
See Job
AppJobsProcessWechatMessage
and sample classAppListenersSaveWechatMessageListener
for more detail.
2.3.5 Sending wechat message (Console)
php artisan wechat:send
See class
AppConsoleCommandsWechatSend
for more detail.
2.3.6 Sending wechat message (Web Api)
php artisan wechat:serve --port=your_port
When you call the api, you must start the web server. The easiest way is start a mini server with the command above. Also, you can deploy you code use
Apache
Nginx
etc
there has some examples, this will return a json response {'ret':0, 'message':'xxx'}
, success if the ret
equals 0
# send text, POST request, need params `to` and `content` curl -H 'Accept:application/json' --data "to=$to_user_name&content=$your_content" http://localhost:$your_port/api/wechat/messages/text
# send image, POST request, need params `to` and `path` curl -H 'Accept:application/json' --data "to=$to_user_name&path=$image_path" http://localhost:$your_port/api/wechat/messages/image
# send emotion, POST request, need params `to` and `path` curl -H 'Accept:application/json' --data "to=$to_user_name&path=$emotion_path" http://localhost:$your_port/api/wechat/messages/emotion
# send file, POST request, need params `to` and `path` curl -H 'Accept:application/json' --data "to=$to_user_name&path=$file_path" http://localhost:$your_port/api/wechat/messages/file
OpenSource
MIT
Statement
This software shall not be used for commercial purposes only
版权声明:
1、该文章(资料)来源于互联网公开信息,我方只是对该内容做点评,所分享的下载地址为原作者公开地址。2、网站不提供资料下载,如需下载请到原作者页面进行下载。
3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考学习用!
4、如文档内容存在违规,或者侵犯商业秘密、侵犯著作权等,请点击“违规举报”。