GithubHelp home page GithubHelp logo

tianliangzhou / shrimp-wechat-sdk Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 1.0 189 KB

微信公众号,服务号SDK

License: MIT License

PHP 100.00%
wechat wechat-sdk weixin-sdk weixin sdk shrimp-wechat-sdk

shrimp-wechat-sdk's Introduction

Shrimp-wechat-sdk

Build Status Coverage Status Maintainability License

小虾米微信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.

shrimp-wechat-sdk's People

Contributors

tianliangzhou avatar

Watchers

 avatar  avatar

Forkers

scott219

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.