1919 */
2020class Widget_Service extends Widget_Abstract_Options implements Widget_Interface_Do
2121{
22+ /**
23+ * 异步请求
24+ *
25+ * @var array
26+ */
27+ public $ asyncRequests = array ();
28+
2229 /**
2330 * 发送pingback实现
2431 *
@@ -31,7 +38,13 @@ public function sendPingHandle()
3138 $ this ->user ->pass ('contributor ' );
3239
3340 /** 忽略超时 */
34- ignore_user_abort (true );
41+ if (function_exists ('ignore_user_abort ' )) {
42+ ignore_user_abort (true );
43+ }
44+
45+ if (function_exists ('set_time_limit ' )) {
46+ set_time_limit (30 );
47+ }
3548
3649 /** 获取post */
3750 $ post = $ this ->widget ('Widget_Archive ' , "type=post " , "cid= {$ this ->request ->cid }" );
@@ -138,7 +151,7 @@ public function sendPing($cid, array $trackback = NULL)
138151 $ client ->setCookie ('__typecho_uid ' , Typecho_Cookie::get ('__typecho_uid ' ))
139152 ->setCookie ('__typecho_authCode ' , Typecho_Cookie::get ('__typecho_authCode ' ))
140153 ->setHeader ('User-Agent ' , $ this ->options ->generator )
141- ->setTimeout (3 )
154+ ->setTimeout (2 )
142155 ->setData ($ input )
143156 ->send (Typecho_Common::url ('/action/service ' , $ this ->options ->index ));
144157
@@ -148,6 +161,78 @@ public function sendPing($cid, array $trackback = NULL)
148161 }
149162 }
150163
164+ /**
165+ * 请求异步服务
166+ *
167+ * @param $method
168+ * @param mixed $params
169+ */
170+ public function requestService ($ method , $ params = NULL )
171+ {
172+ static $ called ;
173+
174+ if (!$ called ) {
175+ $ self = $ this ;
176+
177+ Typecho_Response::addCallback (function () use ($ self ) {
178+ if (!empty ($ self ->asyncRequests ) && $ client = Typecho_Http_Client::get ()) {
179+ try {
180+ $ client ->setHeader ('User-Agent ' , $ this ->options ->generator )
181+ ->setTimeout (2 )
182+ ->setData (array (
183+ 'do ' => 'async ' ,
184+ 'requests ' => Json::encode ($ self ->asyncRequests ),
185+ 'token ' => Typecho_Common::timeToken ($ this ->options ->secret )
186+ ))
187+ ->setMethod (Typecho_Http_Client::METHOD_POST )
188+ ->send (Typecho_Common::url ('/action/service ' , $ this ->options ->index ));
189+
190+ } catch (Typecho_Http_Client_Exception $ e ) {
191+ return ;
192+ }
193+ }
194+ });
195+
196+ $ called = true ;
197+ }
198+
199+ $ this ->asyncRequests [] = array ($ method , $ params );
200+ }
201+
202+ /**
203+ * 执行回调
204+ *
205+ * @throws Typecho_Widget_Exception
206+ */
207+ public function asyncHandle ()
208+ {
209+ /** 验证权限 */
210+ $ token = $ this ->request ->token ;
211+
212+ if (!Typecho_Common::timeTokenValidate ($ token , $ this ->options ->secret , 3 )) {
213+ throw new Typecho_Widget_Exception (_t ('禁止访问 ' ), 403 );
214+ }
215+
216+ /** 忽略超时 */
217+ if (function_exists ('ignore_user_abort ' )) {
218+ ignore_user_abort (true );
219+ }
220+
221+ if (function_exists ('set_time_limit ' )) {
222+ set_time_limit (30 );
223+ }
224+
225+ $ requests = Json::decode ($ this ->request ->requests , true );
226+ $ plugin = Typecho_Plugin::factory (__CLASS__ );
227+
228+ if (!empty ($ requests )) {
229+ foreach ($ requests as $ request ) {
230+ list ($ method , $ params ) = $ request ;
231+ $ plugin ->{$ method }($ params );
232+ }
233+ }
234+ }
235+
151236 /**
152237 * 异步请求入口
153238 *
@@ -157,5 +242,6 @@ public function sendPing($cid, array $trackback = NULL)
157242 public function action ()
158243 {
159244 $ this ->on ($ this ->request ->is ('do=ping ' ))->sendPingHandle ();
245+ $ this ->on ($ this ->request ->is ('do=async ' ))->asyncHandle ();
160246 }
161247}
0 commit comments