forked from xdash/YuGiOh_Card_for_Wechat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ocg.php
370 lines (261 loc) · 9.73 KB
/
ocg.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<?php
header("content-type:text/html; charset=utf-8");
/**
Plugin Name: 游戏王卡片查询器for微信
Plugin URI: http://www.fanbing.net
Description: 在微信中查询游戏王卡片,技术支持来自ourocg.cn
Version: 1.0
Author: fanbingx@gmail.com
Author URI: http://www.fanbing.net
License: BSD
**/
// ------ Settings ----------
// 设置token
// 必须和微信公众平台中的设置保持一致;设置页面 http://mp.weixin.qq.com/cgi-bin/callbackprofile?t=wxm-callbackapi&type=info&lang=zh_CN
define("TOKEN", "ocgcardfdasfjlsdjou88fa");
// 设置默认图片
define("DEFAULT_COVER", "");
// 设置欢迎文案
define("WELCOME" , "It's time to duel!");
// 查询来源网页 ourocg.cn(不需要修改)
define("OUROCG","http://www.ourocg.cn/m/card-");
define("SEARCHPAGE","http://www.ourocg.cn/S.aspx?key=");
// ------ BODY ----------
$wechatObj = new wechatCallbackapiTest();
if( isset($_REQUEST['echostr']) )
$wechatObj->valid();
elseif( isset( $_REQUEST['signature'] ) )
{
$wechatObj->responseMsg();
}
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
//valid signature , option
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
public function responseMsg()
{
//get post data, May be due to the different environments
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
//extract post data
if (!empty($postStr)){//发送有效信息
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$time = time();
if(!empty( $keyword )){//开始解析关键词
if( $keyword == 'hi' ){
?>
<xml>
<ToUserName><![CDATA[<?=$fromUsername?>]]></ToUserName>
<FromUserName><![CDATA[<?=$toUsername?>]]></FromUserName>
<CreateTime><?=time()?></CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[<?=WELCOME?>]]></Content>
</xml>
<?php
exit;}
elseif( $keyword == 'roll' ){
?>
<xml>
<ToUserName><![CDATA[<?=$fromUsername?>]]></ToUserName>
<FromUserName><![CDATA[<?=$toUsername?>]]></FromUserName>
<CreateTime><?=time()?></CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[<?=rand(1,6)?>]]></Content>
</xml>
<?php
exit; }
elseif( $keyword == 'coin' ){
?>
<xml>
<ToUserName><![CDATA[<?=$fromUsername?>]]></ToUserName>
<FromUserName><![CDATA[<?=$toUsername?>]]></FromUserName>
<CreateTime><?=time()?></CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[<?=rand(1,2)==1?"正面":"反面";?>]]></Content>
</xml>
<?php
exit; }
elseif( $articles = ws_get_article( $keyword )){
ob_start();
?><xml>
<ToUserName><![CDATA[<?=$fromUsername?>]]></ToUserName>
<FromUserName><![CDATA[<?=$toUsername?>]]></FromUserName>
<CreateTime><?=$time?></CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<Content><![CDATA[搜索结果]]></Content>
<ArticleCount><?=count($articles)?></ArticleCount>
<Articles><?php foreach( $articles as $item ): ?>
<item>
<Title><![CDATA[<?=$item['title']?>]]></Title>
<Description><![CDATA[<?=$item['content']?>]]></Description>
<PicUrl><![CDATA[<?=$item['pic']?>]]></PicUrl>
<Url><![CDATA[<?=$item['url']?>]]></Url>
</item>
<?php endforeach; ?></Articles>
<FuncFlag>0</FuncFlag>
</xml>
<?php
$xml = ob_get_contents();
//file_put_contents('xml.txt', $xml);
header('Content-Type: text/xml');
echo trim($xml);
}
else{//没搜索到结果
?>
<xml>
<ToUserName><![CDATA[<?=$fromUsername?>]]></ToUserName>
<FromUserName><![CDATA[<?=$toUsername?>]]></FromUserName>
<CreateTime><?=time()?></CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[没有搜索到相关卡片,请尝试其他关键词。]]></Content>
</xml>
<?php
}
}else{//发送空关键词
echo "请输入要查询的游戏王卡片关键词。";
}
}else {//未发送有效信息
echo "";
exit;
}
}
private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}
function ws_get_article( $keyword ){
$i = 0;
$sfile = fopen(SEARCHPAGE.$keyword,"r"); //打开查询网页
while(!FEOF($sfile)){ //读出网页源码
$l = trim(fgets($sfile));//读出一行源码
if ( strpos($l,"main-content carddetails")!==false ){ //搜到准确结果
$getCard = get_accurate_result( $keyword );
return $getCard;
exit;
}
elseif ( strpos($l,"info fn-left")!==false ){ //搜到模糊结果,返回前10个
if($i < 10){ //微信限制一次最多返回10篇文章
// 用正则表达式填写进需要的过程,获得各种需要内容
$out = preg_replace('/<div class.*height=120 alt="/','',$l);
$out = preg_replace('/"\/><\/figure>/','',$out);
$result['title'] = "【".$out."】";// 卡片名称
$out = preg_replace('/<div class.*<\/a><\/h1>/','',$l);
$out = preg_replace('/<\/span><\/div>.*<\/figure>/','',$out);
$out = trim(strip_tags($out));// 卡片属性
$result['title'] = $result['title']." - ".$out;
$out = preg_replace('/<div class.*<p class="effect">/','',$l);
$out = preg_replace('/<\/p>.*<\/figure>/','',$out);
$result['content'] = mb_strimwidth($out,0,200,'...','UTF-8');// 卡片效果
$out = preg_replace('/<div class.*h1.*Cards\/View-/','',$l);
$out = preg_replace('/">.*<\/figure>/','',$out);
$result['url'] = OUROCG.$out.".html";// 卡片地址
$out = preg_replace('/<div class.*src="/','',$l);
$out = preg_replace('/" height.*<\/figure>/','',$out);
$result['pic'] = $out;// 卡片图片
$results[] = $result;
$i++;
}
}
}
if( count( $results ) > 0 ) return $results ;
else return false;
}
function get_accurate_result( $keyword ){
//搜到精确结果时,直接呈现结果
$i = 0;
$sfile = fopen(SEARCHPAGE.$keyword,"r"); //打开查询网页
while(!FEOF($sfile)){ //读出网页源码
$l = trim(fgets($sfile));//读出一行源码
if ( strpos($l,"<li>中文名:")!==false ){
$out = preg_replace('/<li>中文名:/','',$l);
$out = preg_replace('/<\/li>/','',$out);
$result['title'] = "【".$out."】";// 卡片名称
}
elseif ( strpos($l,"<li>卡片种类:")!==false ){
$out = preg_replace('/<li>卡片种类:/','',$l);
$out = preg_replace('/<\/li>/','',$out);
$cardCate = $out;// 卡片种类
}
elseif ( strpos($l,"<li>星级:")!==false ){
$out = preg_replace('/<li>星级:/','',$l);
$out = preg_replace('/<\/li>/','',$out);
$cardLevel = " ".$out."星 ";// 卡片星级
}
elseif ( strpos($l,"<li>阶级:")!==false ){
$out = preg_replace('/<li>阶级:/','',$l);
$out = preg_replace('/<\/li>/','',$out);
$cardXyz = " ".$out."阶 ";// 卡片阶级
}
elseif ( strpos($l,"<li>攻击:")!==false ){
$out = preg_replace('/<li>攻击:/','',$l);
$out = preg_replace('/<\/li>/','',$out);
$cardATK = "/ ATK:".$out." ";// ATK
}
elseif ( strpos($l,"<li>防御:")!==false ){
$out = preg_replace('/<li>防御:/','',$l);
$out = preg_replace('/<\/li>/','',$out);
$cardDEF = "/ DEF:".$out." ";// DEF
}
elseif ( strpos($l,"<li>种族:")!==false ){
$out = preg_replace('/<li>种族:/','',$l);
$out = preg_replace('/<\/li>/','',$out);
$cardRace = "/ 种族:".$out." ";// 种族
}
elseif ( strpos($l,"<li>属性:")!==false ){
$out = preg_replace('/<li>属性:/','',$l);
$out = preg_replace('/<\/li>/','',$out);
$cardNature = "/ 属性:".$out;// 属性
}
elseif ( strpos($l,"<li>效果:")!==false ){
$out = preg_replace('/<li>效果:/','',$l);
$out = preg_replace('/<\/li>/','',$out);
$result['content'] = mb_strimwidth($out,0,200,'...','UTF-8');// 卡片效果
}
elseif ( strpos($l,'width="160"')!==false ){
$out = preg_replace('/.*src="/','',$l);
$out = preg_replace('/".*width=.*<\/li>/','',$out);
echo $out;
$result['pic'] = $out;// 卡片图片
}
elseif ( strpos($l,'link rel="canonical')!==false ){
$out = preg_replace('/.*<link rel="canonical" href=".*View-/','',$l);
$out = preg_replace('/">/','',$out);
$result['url'] = OUROCG.$out.".html";// 卡片地址
}
}
$result['title'] = $result['title']." - ".$cardCate.$cardLevel.$cardXyz.$cardATK.$cardDEF.$cardRace.$cardNature;
$results[] = $result;
if( count( $results ) > 0 ) return $results ;
else return false;
}
function thumbnail_url( $html )
{
$reg = '/src="(.+?)"/is';
if(preg_match( $reg , $html , $out ))
{
return $out[1];
}
return false;
}