easyswoole、swoole如何用,怎么用easywechat?
示例:在App目录下创建Component,然后创建Request.php 去 继承 easywechat里用的SymfonyRequest类
<?php
namespace App\Component;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
class Request extends SymfonyRequest
{
/**
* @param bool $asResource
* @return string
*/
public function getContent($asResource = false)
{
parent::getContent($asResource); // TODO: Change the autogenerated stub
if (null == $this->content || false === $this->content) {
$this->content = \Core\Http\Request::getInstance()->getBody()->__toString();
}
return $this->content;
}
}
再创建个Wechat.php,我是为了方便管理,具体你们习惯放哪就放哪。
<?php
namespace App\Component;
use EasyWeChat\Foundation\Application;
class Wechat
{
protected static $instance;
protected $application;
static function getInstance(){
if(!isset(self::$instance)){
self::$instance = new static();
}
return self::$instance;
}
public function __construct($config = null)
{
if(empty($config)){
$config = \Conf\Config::getInstance()->getConf('wechat');
}
$application = new Application($config);
$application->server->setRequest(\App\Component\Request::createFromGlobals());
$this->application = $application;
}
public function getApplication(){
return $this->application;
}
}
然后创建个控制器,给预定义变量$_GET
、$_POST
等赋值,记得啊老铁,这是常驻内存,请求完毕记得清理这些预定义变量,建议在Controller的afterAction里去清理。
<?php
namespace App\Controller;
use fashop\Controller;
use EasyWeChat\Foundation\Application;
class Index extends Controller
{
public function index()
{
$_GET = isset( $request->getSwooleRequest()->get ) ? $request->getSwooleRequest()->get : [];
$_POST = isset( $request->getSwooleRequest()->post ) ? $request->getSwooleRequest()->post : [];
$_COOKIE = isset( $request->getSwooleRequest()->cookie ) ? $request->getSwooleRequest()->cookie : [];
$_FILES = isset( $request->getSwooleRequest()->files ) ? $request->getSwooleRequest()->files : [];
$server = $request->getSwooleRequest()->server;
$_SERVER = [];
if( isset( $server ) ){
foreach( $server as $key => $value ){
$_SERVER[strtoupper( $key )] = $value;
}
}
if(!isset($_GET['openid'])){
$timestamp = $_GET['timestamp'];
$nonce = $_GET['nonce'];
$token = "hanwenbo";
$signature = $_GET['signature'];
$array = [$timestamp, $nonce, $token];
sort( $array );
$tmpstr = implode( '', $array );
$tmpstr = sha1( $tmpstr );
if( $tmpstr == $signature ){
$this->response()->write( $_GET['echostr'] );
$this->response()->end();
}
}else{
$app = new Application(\Conf\Config::getInstance()->getConf('wechat'));
$server = $app->server;
$server->setRequest(\App\Component\Request::createFromGlobals());
$server->setMessageHandler(function($message){
switch ($message->MsgType) {
case 'event':
return '收到事件消息';
break;
case 'text':
return "收到了文字111";
break;
case 'image':
return '收到图片消息';
break;
case 'voice':
return '收到语音消息';
break;
case 'video':
return '收到视频消息';
break;
case 'location':
return '收到坐标消息';
break;
case 'link':
return '收到链接消息';
break;
// ... 其它消息
default:
return '收到其它消息';
break;
}
});
$response = $server->serve();
$content = $response->getContent();
$this->response()->write($content);
}
}
public function onRequest( $actionName )
{
// TODO: Implement onRequest() method.
}
}