PHP code example of tegic / hyperf-wechat

1. Go to this page and download the library: Download tegic/hyperf-wechat 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/ */

    

tegic / hyperf-wechat example snippets


Router::addRoute(['GET', 'POST', 'HEAD'], '/wechat', 'App\Controller\WeChatController@serve');



declare(strict_types=1);

namespace App\Controller;

use EasyWeChat\Kernel\Exceptions\BadRequestException;
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
use Naixiaoxin\HyperfWechat\EasyWechat;
use Naixiaoxin\HyperfWechat\Helper;
use ReflectionException;

class WeChatController extends AbstractController
{
    /**
     * 处理微信的请求消息
     *
     * @return string
     * @throws BadRequestException
     * @throws InvalidArgumentException
     * @throws InvalidConfigException
     * @throws ReflectionException
     */
    public function serve()
    {

        $app = EasyWechat::officialAccount();
        $app->server->push(function ($message) {
            return "欢迎关注 EasyWechat!";
        });
        // 一定要用Helper::Response去转换
        return Helper::Response($app->server->serve());
    }
}

  use \Naixiaoxin\HyperfWechat\EasyWechat;
  $officialAccount = EasyWechat::officialAccount(); // 公众号
  $work = EasyWechat::work(); // 企业微信
  $payment = EasyWechat::payment(); // 微信支付
  $openPlatform = EasyWechat::openPlatform(); // 开放平台
  $miniProgram = EasyWechat::miniProgram(); // 小程序
  
  // 均支持传入配置账号名称以及配置
  EasyWeChat::officialAccount('foo',[]); // `foo` 为配置文件中的名称,默认为 `default`。`[]` 可覆盖账号配置
  //...