easyswoole、swoole如何用,怎么用easywechat?

示例:在App目录下创建Component,然后创建Request.php 去 继承 easywechat里用的SymfonyRequest类

  1. <?php
  2. namespace App\Component;
  3. use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
  4. class Request extends SymfonyRequest
  5. {
  6. /**
  7. * @param bool $asResource
  8. * @return string
  9. */
  10. public function getContent($asResource = false)
  11. {
  12. parent::getContent($asResource); // TODO: Change the autogenerated stub
  13. if (null == $this->content || false === $this->content) {
  14. $this->content = \Core\Http\Request::getInstance()->getBody()->__toString();
  15. }
  16. return $this->content;
  17. }
  18. }

再创建个Wechat.php,我是为了方便管理,具体你们习惯放哪就放哪。

  1. <?php
  2. namespace App\Component;
  3. use EasyWeChat\Foundation\Application;
  4. class Wechat
  5. {
  6. protected static $instance;
  7. protected $application;
  8. static function getInstance(){
  9. if(!isset(self::$instance)){
  10. self::$instance = new static();
  11. }
  12. return self::$instance;
  13. }
  14. public function __construct($config = null)
  15. {
  16. if(empty($config)){
  17. $config = \Conf\Config::getInstance()->getConf('wechat');
  18. }
  19. $application = new Application($config);
  20. $application->server->setRequest(\App\Component\Request::createFromGlobals());
  21. $this->application = $application;
  22. }
  23. public function getApplication(){
  24. return $this->application;
  25. }
  26. }

然后创建个控制器,给预定义变量$_GET$_POST等赋值,记得啊老铁,这是常驻内存,请求完毕记得清理这些预定义变量,建议在Controller的afterAction里去清理。

  1. <?php
  2. namespace App\Controller;
  3. use fashop\Controller;
  4. use EasyWeChat\Foundation\Application;
  5. class Index extends Controller
  6. {
  7. public function index()
  8. {
  9. $_GET = isset( $request->getSwooleRequest()->get ) ? $request->getSwooleRequest()->get : [];
  10. $_POST = isset( $request->getSwooleRequest()->post ) ? $request->getSwooleRequest()->post : [];
  11. $_COOKIE = isset( $request->getSwooleRequest()->cookie ) ? $request->getSwooleRequest()->cookie : [];
  12. $_FILES = isset( $request->getSwooleRequest()->files ) ? $request->getSwooleRequest()->files : [];
  13. $server = $request->getSwooleRequest()->server;
  14. $_SERVER = [];
  15. if( isset( $server ) ){
  16. foreach( $server as $key => $value ){
  17. $_SERVER[strtoupper( $key )] = $value;
  18. }
  19. }
  20. if(!isset($_GET['openid'])){
  21. $timestamp = $_GET['timestamp'];
  22. $nonce = $_GET['nonce'];
  23. $token = "hanwenbo";
  24. $signature = $_GET['signature'];
  25. $array = [$timestamp, $nonce, $token];
  26. sort( $array );
  27. $tmpstr = implode( '', $array );
  28. $tmpstr = sha1( $tmpstr );
  29. if( $tmpstr == $signature ){
  30. $this->response()->write( $_GET['echostr'] );
  31. $this->response()->end();
  32. }
  33. }else{
  34. $app = new Application(\Conf\Config::getInstance()->getConf('wechat'));
  35. $server = $app->server;
  36. $server->setRequest(\App\Component\Request::createFromGlobals());
  37. $server->setMessageHandler(function($message){
  38. switch ($message->MsgType) {
  39. case 'event':
  40. return '收到事件消息';
  41. break;
  42. case 'text':
  43. return "收到了文字111";
  44. break;
  45. case 'image':
  46. return '收到图片消息';
  47. break;
  48. case 'voice':
  49. return '收到语音消息';
  50. break;
  51. case 'video':
  52. return '收到视频消息';
  53. break;
  54. case 'location':
  55. return '收到坐标消息';
  56. break;
  57. case 'link':
  58. return '收到链接消息';
  59. break;
  60. // ... 其它消息
  61. default:
  62. return '收到其它消息';
  63. break;
  64. }
  65. });
  66. $response = $server->serve();
  67. $content = $response->getContent();
  68. $this->response()->write($content);
  69. }
  70. }
  71. public function onRequest( $actionName )
  72. {
  73. // TODO: Implement onRequest() method.
  74. }
  75. }