meshell / shrimp-wechat-sdk
小虾米微信公众平台SDK
v2.0.3
2021-01-08 08:26 UTC
Requires
- php: >=7.3
- ext-curl: *
- ext-dom: *
- ext-fileinfo: *
- ext-json: *
- ext-libxml: *
- ext-openssl: *
- ext-simplexml: *
- guzzlehttp/guzzle: ^7.2.0
- symfony/event-dispatcher: ^5.1
Requires (Dev)
- friendsofphp/php-cs-fixer: ^v2.16.7
- php-coveralls/php-coveralls: ^v2.4.2
- phpunit/phpunit: ^9.4
- squizlabs/php_codesniffer: ^3.2.3
This package is auto-updated.
Last update: 2025-03-20 00:00:13 UTC
README
小虾米微信SDK是一个针对微信公众平台接口的封装。目前已实现了用户、素材、消息、菜单相关的接口,还有自动回复的处理。
Installation
composer require meshell/shrimp-wechat-sdk
Usage
所有api
的返回结果,都使用微信官方结果为标准。具体可以查看https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1445241432
创建菜单
<?php use Shrimp\ShrimpWechat; $sdk = new ShrimpWechat('wxed1cc1b0e241ff74', '434ca4dfc791853b9ef36ebf24a3ce02'); try { $array = $sdk->menu->create(["type" => "click", "name" => "测试三", "key" => "V1001_TODAY_VIEW"]); } catch(Exception $e) { throw $e; }
上传图片
<?php use Shrimp\ShrimpWechat; use Shrimp\File\MediaFile; $sdk = new ShrimpWechat('wxed1cc1b0e241ff74', '434ca4dfc791853b9ef36ebf24a3ce02'); try { $file = $sdk->material->add(new MediaFile(dirname(__DIR__) . '/content-image.png')); } catch(Exception $e) { throw $e; }
自动回复
自动回复目前支持,文本,语音,视频,图片,图文类型。
- 文本消息
<?php use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Shrimp\Message\Event; use Shrimp\ShrimpWechat; use Shrimp\Event\ResponseEvent; class TestController implements EventSubscriberInterface { /** * @var null | ShrimpWechat */ public $shrimp = null; public function __construct() { $this->shrimp = new ShrimpWechat('wxed1cc1b0e241ff74', '434ca4dfc791853b9ef36ebf24a3ce02'); $this->shrimp->getDispatcher()->addSubscriber($this); } /** * 自动回复 * * @return string */ public function auto() { return $this->shrimp->send(); } private function autoRespond(ResponseEvent $responseEvent) { $responseEvent->setResponse("hello world"); } public static function getSubscribedEvents() { // TODO: Implement getSubscribedEvents() method. return [ Event::TEXT => 'autoRespond', ]; } } (new TestController())->auto(); // 输出标准的XML
- 回复图片
<?php ... function (\Shrimp\Event\ResponseEvent $responseEvent) { $mediaId = 123; $responseEvent->setResponse( new \Shrimp\Response\ImageResponse( $responseEvent->getMessageSource(), $mediaId ) ); } ...
- 订阅关注
<?php ... public static function getSubscribedEvents() { // TODO: Implement getSubscribedEvents() method. return [ Event::EVENT_SUBSCRIBE => 'autoSubscribeRespond', ]; } ...
License
The MIT License (MIT). Please see LICENSE for more information.