keywords.php
8.7 KB
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
<?php
define( 'SITE_URL', $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'] );
define( 'DOING_AJAX', true );
define( 'WP_ADMIN', true );
/** Load WordPress Bootstrap */
require_once( dirname( dirname( __FILE__ ) ) . '/wp-load.php' );
/** Allow for cross-domain requests (from the frontend). */
send_origin_headers();
/** Load WordPress Administration APIs */
require_once( ABSPATH . 'wp-admin/includes/admin.php' );
/** Load Ajax Handlers for WordPress Core */
require_once( ABSPATH . 'wp-admin/includes/ajax-actions.php' );
@header( 'Content-Type: text/html; charset=' . get_option( 'blog_charset' ) );
@header( 'X-Robots-Tag: noindex' );
send_nosniff_header();
nocache_headers();
do_action( 'admin_init' );
if ( !is_user_logged_in() ){
echo json_encode(array('errorcode'=>2,'msg'=>'登录超时!请重新登录!'));exit;
}
if(!isset($_POST['keywords'])||!$_POST['keywords']){
echo json_encode(array('errorcode'=>2,'msg'=>'请输入关键字!'));exit;
}
$tempArr=$_POST['keywords'];
$tagNameArr = array_filter($tempArr);//去空数组
foreach ($tagNameArr as $k => $v) {
$tagNameArr[$k] = trim($v);//去除两端空字符
}
$tagNameArr = array_unique($tagNameArr);//去除重复值
if ($tagNameArr && count($tagNameArr) > 0) {
$taxonomy = !empty($_POST['taxonomy']) ? $_POST['taxonomy'] : 'post_tag';
global $wpdb;
$level = 0;
$return_arr=array();
// //判断并且创建wp_term_flag表 关键词是否爬取标记
// //判断并且创建wp_term_news表 关键词爬取信息
//
//
// $wpdb->query("CREATE TABLE IF NOT EXISTS `wp_term_flag`(`id` int(11) NOT NULL AUTO_INCREMENT,`term` varchar(512) DEFAULT NULL COMMENT '关键词',`term_id` int(11) DEFAULT NULL COMMENT '关键词表id',`tflag` tinyint(1) DEFAULT '1' COMMENT '爬取标记:1未爬取2已爬取',`intime` datetime DEFAULT NULL COMMENT '添加时间',PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC");
// $wpdb->query("CREATE TABLE IF NOT EXISTS `wp_term_news` (`id` int(11) NOT NULL AUTO_INCREMENT,`term_id` int(11) DEFAULT NULL COMMENT '关键词id',`title` varchar(512) DEFAULT NULL COMMENT '标题',`vurl` varchar(512) DEFAULT NULL COMMENT '视频连接',`content` longtext COMMENT '新闻内容',`sorts` tinyint(1) DEFAULT '1' COMMENT '分类:1新闻2视频',`adtime` datetime DEFAULT NULL COMMENT '添加时间',PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8 CHECKSUM=1 DELAY_KEY_WRITE=1 ROW_FORMAT=DYNAMIC");
$term_id_arr=array();
$level=0;
$description='';
$parent='';
foreach($tagNameArr as $vo){
$tag = array();
$slug=sanitize_title(stripslashes(wp_unslash($vo)));//去除特殊字符 大写并且转小写 别名
//根据别名查询
$existing_term = $wpdb->get_row( $wpdb->prepare( "SELECT name,term_id FROM $wpdb->terms WHERE slug = %s", $slug), ARRAY_A );
if(!$existing_term){
$name=wp_unslash($vo);
$term_res = $wpdb->insert( $wpdb->terms, compact( 'name', 'slug' ));//添加关键词表
if(!$term_res){
echo json_encode(array('errorcode'=>2,'msg'=>'关键词添加失败,请稍后重试!'));exit;
}
$term_id= (int) $wpdb->insert_id;
$term_taxonomy_res= $wpdb->insert( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent') + array( 'count' => 0 ) );//添加关键词分类数据
if(!$term_taxonomy_res){
echo json_encode(array('errorcode'=>2,'msg'=>'关键词分类添加失败,请稍后重试!'));exit;
}
$term_taxonomy_id = (int) $wpdb->insert_id;
$tag['term_id']=$term_id;
$tag['term_taxonomy_id']=$term_taxonomy_id;
$tag['count']=$level;
do_action("create_term", $term_id, $term_taxonomy_id, $taxonomy);
do_action("create_$taxonomy", $term_id, $term_taxonomy_id);
$term_id = apply_filters('term_id_filter', $term_id, $term_taxonomy_id);
clean_term_cache($term_id, $taxonomy);
do_action("created_term", $term_id, $term_taxonomy_id, $taxonomy);
do_action("created_$taxonomy", $term_id, $term_taxonomy_id);
}else{
$term_id=$existing_term['term_id'];
$term_taxonomy_res = $wpdb->get_row( $wpdb->prepare( "SELECT term_taxonomy_id,count FROM $wpdb->term_taxonomy WHERE term_id = %d", $term_id), ARRAY_A );
if(!$term_taxonomy_res){
$term_taxonomy_res= $wpdb->insert( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent') + array( 'count' => 0 ) );//添加关键词分类数据
if(!$term_taxonomy_res){
echo json_encode(array('errorcode'=>2,'msg'=>'关键词分类添加失败,请稍后重试!'));exit;
}
$term_taxonomy_id = (int) $wpdb->insert_id;
}else{
$term_taxonomy_id=$term_taxonomy_res['term_taxonomy_id'];
}
$tag['term_id']=$term_id;
$tag['term_taxonomy_id']=$term_taxonomy_id;
$tag['count']=$term_taxonomy_res['count']?$term_taxonomy_res['count']:0;
}
$tbnm = 'wp_term_flag';
$is_tab= $wpdb->query('SHOW TABLES LIKE "' . $tbnm . '"');
if($is_tab){
$term_flag=$wpdb->get_var( $wpdb->prepare( "SELECT tfalg FROM wp_term_flag WHERE term_id = %d", $tag['term_id'] ) );
if(!$term_flag){
$tm=date('Y-m-d H:i:s');
$term_res = $wpdb->query($wpdb->prepare("INSERT INTO wp_term_flag (term,term_id,intime) VALUES (%s,%d,%s)",$vo,$tag['term_id'],$tm));
$term_id_arr[$tag['term_id']]=$vo;
}
}
$return_arr[]='<tr id="tag-"'.$tag['term_id'].' class="alternate"><th scope="row" class="check-column"><label class="screen-reader-text" for="cb-select-"'.$tag['term_id'].'>'.$vo.'</label><div class="cata"'.$tag['term_id'].'><input type="checkbox" name="delete_tags[]" value='.$tag['term_id'].' id="cb-select-"'.$tag['term_id'].'></div></th><td class="name column-name"><strong><a class="row-title" style="margin:0 20px 0 0" href="'.SITE_URL.'/'.$slug.'/'.'" target="_bank" title="查看分类页面">'.$vo.'</a></strong> <a class="row-title" href="'.SITE_URL.'/wp-admin/edit-tags.php?action=edit&taxonomy=post_tag&tag_ID='.$tag['term_id'].'&post_type=post">编辑</a><br><div class="row-actions"><span class="edit"><div style="float:left;" class="cata_"'.$tag['term_id'].'><a href="'.SITE_URL.'/wp-admin/edit-tags.php?action=edit&taxonomy=post_tag&tag_ID='.$tag['term_id'].'&post_type=post">编辑</a><span class="view"></span></div></span></div><div class="hidden" id="inline_"'.$tag['term_id'].'><div class="name">'.$vo.'</div><div class="slug">'.$slug.'</div><div class="parent">0</div></div></td><td class="description column-description"></td><td class="slug column-slug">'.$slug.'</td><td class="posts column-posts"><a href="'.SITE_URL.'/wp-admin/edit.php?tag='.$slug.'">'.$tag['count'].'</a></td></tr>';
}
echo json_encode(array('errorcode'=>1,'return_data'=>$return_arr));exit;
} else {
echo json_encode(array('errorcode'=>2,'msg'=>'请输入关键字!'));exit;
}
function redis_keywords($data_news){//将关键词传送到redis-list
$posturl='http://45.136.130.176:10001/keywords-redis.php';
$keywords_url=$_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/wp-admin/insert-keywords.php';//回调地址
$video_url=$_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/wp-admin/insert-video.php';//回调地址
$redis_arr=array();
foreach ($data_news as $k=>$v){
if($v){
$api_key=sha1(md5('dst').$k);
$redis_arr[$k]['googlenews']=$k.'&&'.$v.'&&'.$keywords_url.'&&'.$api_key;
$redis_arr[$k]['youtobe']=$k.'&&'.$v.'&&'.$video_url.'&&'.$api_key;
}
}
if($redis_arr){
// try{
//初使化init方法
$ch = curl_init();
//指定URL
curl_setopt($ch, CURLOPT_URL, $posturl);
//设定请求后返回结果
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//声明使用POST方式来进行发送
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不验证证书下同
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
//发送数据
curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($redis_arr,true));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type:application/json',
'charset=utf-8'
));
//忽略header头信息
curl_setopt($ch, CURLOPT_HEADER, 0);
//设置超时时间
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
//发送请求
curl_exec($ch);
curl_close($ch);
}
}