PHP code example of pengxuxu / hyperf-easywechat6

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

    

pengxuxu / hyperf-easywechat6 example snippets



declare(strict_types=1);
namespace App\Controller;

use EasyWeChat\Kernel\HttpClient\RequestUtil;
use Symfony\Component\HttpClient\HttpClient as SymfonyClient;

class IndexController extends AbstractController
{
    public function index()
    {
        $user = $this->request->input('user', 'Hyperf');
        $method = $this->request->getMethod();
        $client = SymfonyClient::create(RequestUtil::formatDefaultOptions([]));
        go(function () use ($client) {
            $client->request('GET', 'http://www.baidu.com/');
        });
        go(function () use ($client) {
            $client->request('GET', 'http://www.baidu.com/');
        });
        return [
            'method' => $method,
            'message' => "Hello {$user}.",
        ];
    }
}
在swoole>=4.7.0且开启native curl修改常量SWOOLE_HOOK_ALL,共用一个HttpClient情况下,报错信息:
PHP Fatal error:  Uncaught Swoole\Error: cURL is executing, cannot be operated in /data/project/hyperf-skeleton/vendor/symfony/http-client/Response/CurlResponse.php:366

  $app = new Application($config);

  if (\Hyperf\Context\Context::get(ServerRequestInterface::class) && method_exists($app, 'setRequest')) {
    $app->setRequest(ApplicationContext::getContainer()->get(\Hyperf\HttpServer\Contract\RequestInterface));
  }

  if (method_exists($app, 'setCache')) {
    $app->setCache(ApplicationContext::getContainer()->get(\Psr\SimpleCache\CacheInterface::class)
  }
  

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\RuntimeException;
use Pengxuxu\HyperfWechat\EasyWechat;
use Pengxuxu\HyperfWechat\Helper;
use Hyperf\HttpServer\Contract\ResponseInterface;

class WeChatController
{
    /**
     * 处理微信的请求消息
     *
     * @return ResponseInterface
     * @throws BadRequestException
     * @throws InvalidArgumentException
     * @throws RuntimeException;
     */
    public function serve()
    {
        $app = EasyWechat::officialAccount();
        
        $server = $app->getServer();
        
        $server->with(function ($message, \Closure $next) {
            return '谢谢关注!';
            
            // 你的自定义逻辑
            // return $next($message);
        });
        // 一定要用Helper::Response去转换
        return Helper::Response($server->serve());
    }
}

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