19
19
*/
20
20
class Widget_Service extends Widget_Abstract_Options implements Widget_Interface_Do
21
21
{
22
+ /**
23
+ * 异步请求
24
+ *
25
+ * @var array
26
+ */
27
+ public $ asyncRequests = array ();
28
+
22
29
/**
23
30
* 发送pingback实现
24
31
*
@@ -31,7 +38,13 @@ public function sendPingHandle()
31
38
$ this ->user ->pass ('contributor ' );
32
39
33
40
/** 忽略超时 */
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
+ }
35
48
36
49
/** 获取post */
37
50
$ post = $ this ->widget ('Widget_Archive ' , "type=post " , "cid= {$ this ->request ->cid }" );
@@ -138,7 +151,7 @@ public function sendPing($cid, array $trackback = NULL)
138
151
$ client ->setCookie ('__typecho_uid ' , Typecho_Cookie::get ('__typecho_uid ' ))
139
152
->setCookie ('__typecho_authCode ' , Typecho_Cookie::get ('__typecho_authCode ' ))
140
153
->setHeader ('User-Agent ' , $ this ->options ->generator )
141
- ->setTimeout (3 )
154
+ ->setTimeout (2 )
142
155
->setData ($ input )
143
156
->send (Typecho_Common::url ('/action/service ' , $ this ->options ->index ));
144
157
@@ -148,6 +161,78 @@ public function sendPing($cid, array $trackback = NULL)
148
161
}
149
162
}
150
163
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
+
151
236
/**
152
237
* 异步请求入口
153
238
*
@@ -157,5 +242,6 @@ public function sendPing($cid, array $trackback = NULL)
157
242
public function action ()
158
243
{
159
244
$ this ->on ($ this ->request ->is ('do=ping ' ))->sendPingHandle ();
245
+ $ this ->on ($ this ->request ->is ('do=async ' ))->asyncHandle ();
160
246
}
161
247
}
0 commit comments