PHP code example of meshell / shrimp-wechat-sdk

1. Go to this page and download the library: Download meshell/shrimp-wechat-sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

meshell / shrimp-wechat-sdk example snippets




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;
}




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;
}





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



    ...
    function (\Shrimp\Event\ResponseEvent $responseEvent)
    {
        $mediaId = 123;
        $responseEvent->setResponse(
            new \Shrimp\Response\ImageResponse(
                $responseEvent->getMessageSource(), $mediaId
            )
        );
    }
...



    ...
    public static function getSubscribedEvents()
    {
        // TODO: Implement getSubscribedEvents() method.

        return [
            Event::EVENT_SUBSCRIBE => 'autoSubscribeRespond',
        ];
    }
    ...