mistcheng/sowechat

基于Laravel PHP框架的微信网页版Api架构 & 基于Laravel PHP框架的微信(WeiXin) Web Api

安装: 9

依赖项: 0

建议者: 0

安全: 0

星标: 14

关注者: 2

分支: 4

公开问题: 0

类型:项目

v1.1 2016-10-14 07:51 UTC

This package is not auto-updated.

Last update: 2024-09-14 21:03:35 UTC


README

中文文档:基于Laravel PHP框架的微信网页版Api

Build Status Total Downloads Latest Stable Version Latest Unstable Version License

功能

  1. 7*24小时不间断运行,
  2. 易于使用,支持发送/接收多种消息
  3. 优雅的系统架构,支持跨平台开发和灵活的自定义扩展
  4. 支持Restful Api异步消息处理消息事件广播
  5. 基于Php,世界上最棒的语言! :)

系统架构

System Architecture

  1. 本系统由3个独立组件组成
  2. 中间组件是系统的核心,负责二维码扫描和消息监听;我为此做了大量工作以确保其健壮性。同时,作为连接器,它支持向左侧组件发送消息的能力,并将简单格式的消息推送到右侧组件。运行php artisan wechat:listen命令使其工作。
  3. 左侧组件用于发送消息,用户可以向其代码中的任何朋友发送消息。类App\Console\Commands\WechatSend是一个示例,运行php artisan wechat:send命令使其工作。
  4. 右侧组件用于处理消息,用户可以对即将到来的消息做任何处理。作业App\Jobs\ProcessWechatMessage将每条消息处理成更规范的消息(文本、分享、图片、语音、文件等)。然后,它触发App\Events\WechatMessageEvent事件到订阅者,他们可以对消息做一些自定义操作。类App\Listeners\SaveWechatMessageListener是一个示例,它将消息保存到数据库中。
  5. 优势:这三个组件是3个独立的进程。这可以保证中间部分的持续运行,同时,用户可以在左侧右侧部分进行任何扩展,而不会干扰到中间部分。

先决条件

  1. php 5.6或更高版本
  2. php composer
  3. redis(可选)
  4. mysql(可选)

用法

1. 安装

git clone https://github.com/mistcheng/sowechat.git
cd sowechat
composer install

2. 配置

2.1 config/wechat.php是微信配置文件

<?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 队列配置(推荐数据库或redis,切勿使用异步)

2.1部分,选项wechat.job.connection应在文件config/database.php中进行配置。

2.2.1 如果将wechat.job.connection设置为database,则必须在文件config/database.php中正确配置选项database.connections.mysql
'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 如果将wechat.job.connection设置为redis,则必须正确配置选项database.redis
'redis' => [

        'cluster' => false,

        'default' => [
            'host' => env('REDIS_HOST', 'localhost'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => 0,
        ],

    ],

2.3 运行

2.3.1 首次运行,初始化数据库脚本
cd sowechat
php artisan migrate
2.3.2 运行新的中间组件
php artisan wechat:listen --new

storage/app/wechat文件夹中将出现一个新的二维码,使用您的微信扫描它进行登录。

2.3.3 不重新登录运行中间组件
php artisan wechat:listen

运行命令时不要传递参数--new

2.3.4 处理微信消息
php artisan queue:work

有关详细信息,请参阅作业App\Jobs\ProcessWechatMessage和示例类App\Listeners\SaveWechatMessageListener

2.3.5 通过控制台发送微信消息
php artisan wechat:send

有关详细信息,请参阅类App\Console\Commands\WechatSend

2.3.6 通过Web Api发送微信消息
php artisan wechat:serve --port=your_port

当您调用API时,必须启动Web服务器。最简单的方法是使用上述命令启动一个迷你服务器。您还可以使用ApacheNginx等部署您的代码。

这里有一些示例,这将返回一个JSON响应 {'ret':0, 'message':'xxx'},如果 ret 等于0则为成功

# send text, POST request, need params `to` and `content`
curl -H 'Accept:application/json' --data "to=$to_user_name&content=$your_content" https://:$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" https://:$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" https://:$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" https://:$your_port/api/wechat/messages/file

开源

MIT许可

声明

本软件不得仅用于商业目的