|
|
<?php
|
|
|
|
|
|
if (!defined('BASEPATH'))
|
|
|
exit('No direct script access allowed');
|
|
|
|
|
|
class Keyworlds extends CI_Controller {
|
|
|
|
|
|
function __construct() {
|
|
|
parent::__construct();
|
|
|
$this->permission->is_admin(false);
|
|
|
$this->load->model('InfoKeywords_model', 'Keywords_model');
|
|
|
$this->load->model('InfoKeywordsrank_model', 'Keywordsrank_model');
|
|
|
$this->load->model('InfoKeywordsanalytics_model', 'analytics_model');
|
|
|
$this->load->model('InfoStructures_model');
|
|
|
}
|
|
|
|
|
|
public function index($kw_is_id = '') {
|
|
|
$this->load->library('Accesscheck');
|
|
|
$this->accesscheck->check_access();
|
|
|
|
|
|
$this->permission->is_admin();
|
|
|
//查询结构列表信息
|
|
|
$data['keyworldsList'] = $this->Keywords_model->get_structure_list();
|
|
|
if (empty($data['keyworldsList'])) {
|
|
|
$this->add(0, false);
|
|
|
redirect(site_url("Keyworlds/index"));
|
|
|
}
|
|
|
|
|
|
$root_key = false;
|
|
|
$data['keyworldsList'][0]->id == $kw_is_id ? $kw_is_id = '' : null;
|
|
|
|
|
|
if (empty($kw_is_id) || $kw_is_id == 'asc' || $kw_is_id == 'desc') {
|
|
|
$root_key = true;
|
|
|
$data['order'] = empty($kw_is_id) ? 'asc' : $kw_is_id;
|
|
|
$kw_is_id = $data['keyworldsList'][0]->id;
|
|
|
}
|
|
|
|
|
|
$today = date('Y-m-d', time());
|
|
|
$startdate = strtotime("$today -1 month");
|
|
|
$enddate = time();
|
|
|
if ($this->input->post('startdate') != '')
|
|
|
$startdate = strtotime($this->input->post('startdate'));
|
|
|
if ($this->input->post('enddate') != '')
|
|
|
$enddate = strtotime($this->input->post('enddate'));
|
|
|
|
|
|
//查询当前关键词的排名情况
|
|
|
$data['rank_json'] = '';
|
|
|
$data['current_keyworld'] = $this->Keywords_model->get_keyworld_bytreeid($kw_is_id);
|
|
|
|
|
|
//如果是根目录,则展示最新关键词排名变化情况
|
|
|
if ($root_key) {
|
|
|
$data['compare_rank'] = $this->Keywords_model->get_compare_rank($data['order']);
|
|
|
$this->load->view('bootstrap/header', $data);
|
|
|
$this->load->view('keyworlds/compare_rank');
|
|
|
$this->load->view('bootstrap/footer');
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
$data['wordcount'] = 1;
|
|
|
if ($data['current_keyworld']) {
|
|
|
//获取当前节所有直接子节点
|
|
|
$is_id_string = $data['current_keyworld']->kw_id;
|
|
|
$i = 0;
|
|
|
foreach ($data['keyworldsList'] as $kw) {
|
|
|
if ($i > 9)
|
|
|
break;
|
|
|
if ($kw->pId == $kw_is_id) {
|
|
|
$is_id_string.=',' . $kw->kw_id;
|
|
|
$i++;
|
|
|
$data['wordcount'] = 2;
|
|
|
}
|
|
|
}
|
|
|
$data['rank_info'] = $this->Keywordsrank_model->get_rank_list($is_id_string, 30, $startdate, $enddate);
|
|
|
empty($data['rank_info']) && $data['rank_info'] = $this->Keywordsrank_model->get_rank_list($is_id_string, 30);
|
|
|
if (!empty($data['rank_info'])) {
|
|
|
$rank_info = $data['rank_info'];
|
|
|
asort($rank_info);
|
|
|
$data_time = array();
|
|
|
foreach ($rank_info as $key => $r) {
|
|
|
if (!in_array(date('Y-m-d', $r->kwr_datetime), $data_time))
|
|
|
$data_time[] = date('Y-m-d', $r->kwr_datetime);
|
|
|
$rank_json[$r->kwr_kw_id][date('Y-m-d', $r->kwr_datetime)] = $r;
|
|
|
$rank_json2[$r->kwr_kw_id][] = $r;
|
|
|
}
|
|
|
|
|
|
foreach ($data_time as $d) {
|
|
|
foreach ($rank_json as $kw_id => &$value) {
|
|
|
if (!isset($pre[$kw_id]))
|
|
|
$pre[$kw_id] = $rank_json2[$kw_id][0];
|
|
|
if (!isset($value[$d])) {
|
|
|
$value[$d] = $pre[$kw_id];
|
|
|
} else {
|
|
|
$pre[$kw_id] = $value[$d];
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
$data['rank_json'] = '[';
|
|
|
foreach ($rank_json as &$ra) {
|
|
|
ksort($ra);
|
|
|
$data['rank_json'] .='{"name":"' . $ra[$data_time[0]]->kw_keyworlds . '","data":[';
|
|
|
foreach ($ra as $v) {
|
|
|
$kwr_rank = $v->kwr_rank;
|
|
|
if ($v->kwr_rank == '>150')
|
|
|
$kwr_rank = 100;
|
|
|
if (strtolower($this->config->item('site_code')) == 'gm') {
|
|
|
$data['rank_json'] .=-$kwr_rank . ',';
|
|
|
} else {
|
|
|
$data['rank_json'] .=$kwr_rank . ',';
|
|
|
}
|
|
|
}
|
|
|
$data['rank_json'] = substr($data['rank_json'], 0, -1);
|
|
|
$data['rank_json'] .=']},';
|
|
|
}
|
|
|
$data['rank_json'] = substr($data['rank_json'], 0, -1) . ']';
|
|
|
$data['data_time'] = json_encode($data_time);
|
|
|
$data['rank_info'] = '';
|
|
|
isset($rank_json[$data['current_keyworld']->kw_id]) && $data['rank_info'] = $rank_json[$data['current_keyworld']->kw_id];
|
|
|
}
|
|
|
} else {
|
|
|
redirect(site_url("Keyworlds/index"));
|
|
|
}
|
|
|
|
|
|
$data['analytics'] = $this->analytics_model->get_keyword_analytics($data['current_keyworld']->kw_keyworlds, 30);
|
|
|
$data['site_code'] = strtolower($this->config->item('site_code'));
|
|
|
|
|
|
$this->load->view('bootstrap/header', $data);
|
|
|
$this->load->view('keyworlds/index');
|
|
|
$this->load->view('bootstrap/footer');
|
|
|
}
|
|
|
|
|
|
//搜索关键词
|
|
|
public function search_keywords() {
|
|
|
$keyworlds = $this->input->post('keyworlds');
|
|
|
if (empty($keyworlds)) {
|
|
|
echo 0;
|
|
|
return;
|
|
|
}
|
|
|
$data['keyworld'] = $keyworlds;
|
|
|
$data['keyworld_info'] = $this->Keywords_model->get_keyworld_by_word($keyworlds);
|
|
|
$this->load->view('keyworlds/search', $data);
|
|
|
}
|
|
|
|
|
|
//添加关键词
|
|
|
public function add($is_parent_id, $is_ajax = true) {
|
|
|
$insert_id = $this->InfoStructures_model->Add($is_parent_id);
|
|
|
if ($insert_id && $kw_id = $this->Keywords_model->add_keyworlds('New Keywords', $this->config->item('site_code'), time(), $insert_id)) {
|
|
|
$data[] = array('name' => 'ok', 'value' => $this->Keywords_model->get_keyworld($kw_id));
|
|
|
} else {
|
|
|
$data[] = array('name' => 'no', 'value' => $this->lang->line('form_info_error'));
|
|
|
}
|
|
|
if ($is_ajax) {
|
|
|
echo json_encode($data);
|
|
|
return TRUE;
|
|
|
}
|
|
|
return $insert_id;
|
|
|
}
|
|
|
|
|
|
//编辑关键词
|
|
|
public function edit() {
|
|
|
$this->form_validation->set_rules('keyworlds', 'lang:kw_keyworlds', 'required');
|
|
|
$this->form_validation->set_rules('kw_id', 'lang:kw_id', 'required');
|
|
|
if ($this->form_validation->run() == FALSE) {
|
|
|
$data = array();
|
|
|
foreach ($this->form_validation->_error_array as $key => $value) {
|
|
|
$data[] = array('name' => $key, 'value' => $value);
|
|
|
}
|
|
|
echo json_encode($data);
|
|
|
} else {
|
|
|
$isupdate = $this->Keywords_model->edit_keyworlds($this->input->post('keyworlds'), $this->input->post('kw_id'), $this->input->post('status'));
|
|
|
if ($isupdate) {
|
|
|
$data[] = array('name' => 'ok', 'value' => $this->lang->line('form_keyworlds_success'));
|
|
|
echo json_encode($data);
|
|
|
} else {
|
|
|
$data[] = array('name' => 'no', 'value' => $this->lang->line('form_keyworlds_error'));
|
|
|
echo json_encode($data);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//删除某个排名
|
|
|
public function delete_one_rank($kwr_id) {
|
|
|
$this->Keywordsrank_model->delete_one_rank($kwr_id);
|
|
|
header("Location:" . $_SERVER['HTTP_REFERER']);
|
|
|
}
|
|
|
|
|
|
//删除关键词
|
|
|
public function delete_keyworlds() {
|
|
|
$this->form_validation->set_rules('kw_id', 'lang:kw_id', 'required');
|
|
|
$this->form_validation->set_rules('kw_is_id', 'lang:kw_is_id', 'required');
|
|
|
if ($this->form_validation->run() == FALSE) {
|
|
|
$data = array();
|
|
|
foreach ($this->form_validation->_error_array as $key => $value) {
|
|
|
$data[] = array('name' => $key, 'value' => $value);
|
|
|
}
|
|
|
echo json_encode($data);
|
|
|
} else {
|
|
|
$isdelete = $this->Keywords_model->delete($this->input->post('kw_id'));
|
|
|
if ($isdelete) {
|
|
|
$this->Keywordsrank_model->delete($this->input->post('kw_id'));
|
|
|
$this->InfoStructures_model->Delete($this->input->post('kw_is_id'));
|
|
|
$data[] = array('name' => 'ok', 'value' => site_url('keyworlds/index'));
|
|
|
echo json_encode($data);
|
|
|
} else {
|
|
|
$data[] = array('name' => 'no', 'value' => $this->lang->line('form_keyworlds_error'));
|
|
|
echo json_encode($data);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//移动关键词排序顺序
|
|
|
public function move() {
|
|
|
//网站会提交一个同级节点id列表字符串,按照这个去排序
|
|
|
$parent_id = $this->input->post('pid');
|
|
|
$idsStr = $this->input->post('ids');
|
|
|
$idsArray = explode(',', $idsStr);
|
|
|
foreach ($idsArray as $key => $value) {
|
|
|
if ($value) {
|
|
|
//设置排序
|
|
|
$this->InfoStructures_model->set_sort($value, $key);
|
|
|
//设置path
|
|
|
$this->InfoStructures_model->set_path($parent_id, $value);
|
|
|
}
|
|
|
}
|
|
|
$data[] = array('name' => 'ok', 'value' => $this->lang->line('structures_success_move'));
|
|
|
echo json_encode($data);
|
|
|
return TRUE;
|
|
|
}
|
|
|
|
|
|
public function get_google_rank() {
|
|
|
$keyworlds = $this->input->post('keyworlds'); //搜索关键字
|
|
|
$kw_id = $this->input->post('kw_id');
|
|
|
$site_url = $this->config->item('site_url');
|
|
|
|
|
|
//$engines = array('cht'=>'get_search_content_by_aol','gm' =>'get_search_content_by_google','vc' =>'get_search_content_by_google','jp' =>'get_search_content_by_yahoo','vac'=>'get_search_content_by_google','it' =>'get_search_content_by_google','ru' =>'get_search_content_by_google','wt' =>'get_search_content_by_baidu','tbt'=>'get_search_content_by_aol','sht'=>'get_search_content_by_aol','yz' =>'get_search_content_by_aol','gl' =>'get_search_content_by_aol','mbj'=>'get_search_content_by_aol','ct' =>'get_search_content_by_aol','mct'=>'get_search_content_by_aol','dct'=>'get_search_content_by_aol','cits' =>'get_search_content_by_baidu',);
|
|
|
//$engines = array('cht'=>'get_search_content_by_google','gm' =>'get_search_content_by_google','vc' =>'get_search_content_by_google','jp' =>'get_search_content_by_yahoo','vac'=>'get_search_content_by_google','it' =>'get_search_content_by_google','ru' =>'get_search_content_by_google','wt' =>'get_search_content_by_baidu','tbt'=>'get_search_content_by_google','sht'=>'get_search_content_by_google','yz' =>'get_search_content_by_google','gl' =>'get_search_content_by_google','mbj'=>'get_search_content_by_google','ct' =>'get_search_content_by_google','mct'=>'get_search_content_by_google','dct'=>'get_search_content_by_google','cits' =>'get_search_content_by_baidu');
|
|
|
$engines = array('cht' => 'get_search_content_by_google', 'gm' => 'get_search_content_by_google', 'vc' => 'get_search_content_by_google', 'jp' => 'get_search_content_by_google', 'vac' => 'get_search_content_by_google', 'it' => 'get_search_content_by_google', 'ru' => 'get_search_content_by_google', 'wt' => 'get_search_content_by_baidu', 'tbt' => 'get_search_content_by_google', 'sht' => 'get_search_content_by_google', 'yz' => 'get_search_content_by_google', 'gl' => 'get_search_content_by_google', 'mbj' => 'get_search_content_by_google', 'ct' => 'get_search_content_by_google', 'mct' => 'get_search_content_by_google', 'dct' => 'get_search_content_by_google', 'cits' => 'get_search_content_by_baidu', 'ah' => 'get_search_content_by_google');
|
|
|
$site_code = $this->config->item('site_code');
|
|
|
$method = $engines[$site_code];
|
|
|
|
|
|
if ($site_code == 'wt')
|
|
|
$site_url = 'www.iiiyooo.com';
|
|
|
if ($site_code == 'cits')
|
|
|
$site_url = 'www.guilincits.c';
|
|
|
|
|
|
$last_rank = $this->Keywordsrank_model->get_rank_list($kw_id, $top = 1);
|
|
|
if (isset($last_rank[0]->kwr_datetime) and ( time() - $last_rank[0]->kwr_datetime) < 300) {
|
|
|
$data[] = array('name' => 'rank', 'value' => '查询过于频繁,请等待三分钟!');
|
|
|
echo json_encode($data);
|
|
|
return TRUE;
|
|
|
}
|
|
|
|
|
|
//获取排名详细情况,排名超过150的停止搜索
|
|
|
for ($i = 1; $i <= 15; $i++) {
|
|
|
$j = $i;
|
|
|
$rank = $this->$method($keyworlds, $site_url, $j, $site_code);
|
|
|
if ($rank['rank'] != 0) {
|
|
|
if ($rank['rank'] > 150)
|
|
|
$rank['rank'] = '151';
|
|
|
break;
|
|
|
}elseif ($i == 15) {
|
|
|
$rank['rank'] = '151';
|
|
|
break;
|
|
|
} elseif ($rank['rank'] == -2) {
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
//保存排名数据
|
|
|
if ($rank['rank'] == '151') {
|
|
|
$this->Keywords_model->update_time($kw_id);
|
|
|
} elseif (isset($last_rank[0]->kwr_datetime) and $last_rank[0]->kwr_datetime > strtotime(date('Y-m-d'))) {
|
|
|
if ($rank['rank'] != -1 and $rank['rank'] != -2 and $this->Keywordsrank_model->update_rank($last_rank[0]->kwr_id, $rank['rank'], $rank['source'])) {
|
|
|
$this->Keywords_model->update_time($kw_id);
|
|
|
}
|
|
|
} else {
|
|
|
if ($rank['rank'] != -1 and $rank['rank'] != -2 and $this->Keywordsrank_model->add_rank($kw_id, $rank['rank'], $rank['source'], $rank['engines'])) {
|
|
|
$this->Keywords_model->update_time($kw_id);
|
|
|
}
|
|
|
}
|
|
|
$data[] = array('name' => 'rank', 'value' => $rank['rank']);
|
|
|
echo json_encode($data);
|
|
|
return TRUE;
|
|
|
}
|
|
|
|
|
|
//自动爬取排名
|
|
|
public function get_auto_rank($engines = '', $fix_site_code = '') {
|
|
|
$rank = array();
|
|
|
$site_config = $this->config->item('site');
|
|
|
$engines = trim($engines);
|
|
|
//取出需要更新排名的关键词
|
|
|
$worldsList = $this->Keywords_model->get_keyworld_list(1, $engines, $fix_site_code);
|
|
|
//print_r($worldsList);die();
|
|
|
//如果没有需要更新的则返回
|
|
|
if (empty($worldsList)) {
|
|
|
echo 'all done';
|
|
|
return;
|
|
|
}
|
|
|
$last_rank = $this->Keywordsrank_model->get_rank_list($worldsList[0]->kw_id);
|
|
|
|
|
|
//获取排名并保存
|
|
|
foreach ($worldsList as $v) {
|
|
|
$site_config = $site_config[$v->kw_sitecode];
|
|
|
$site_url = $site_config['site_url'];
|
|
|
$site_code = $site_config['site_code'];
|
|
|
|
|
|
for ($i = 1; $i <= 15; $i++) {
|
|
|
if ($engines == '' || $engines == 'en' || $engines == 'eu') {//没有输入搜索引擎的时候根据站点自动分配获取排名的搜索引擎
|
|
|
$rank = $this->get_search_content_by_google($v->kw_keyworlds, $site_url, $i, $site_code);
|
|
|
} else if ($engines == 'yandex') {//俄语站的yangdex
|
|
|
$rank = $this->get_search_content_by_yandex($v->kw_keyworlds, $site_url, $i - 1);
|
|
|
} elseif ($engines == 'yahoo') {
|
|
|
$rank = $this->get_search_content_by_yahoo($v->kw_keyworlds, $site_url, $i, $site_code);
|
|
|
}
|
|
|
|
|
|
if ($rank['rank'] != 0) {
|
|
|
if ($rank['rank'] > 150)
|
|
|
$rank['rank'] = '151';
|
|
|
break;
|
|
|
}elseif ($i == 15) {
|
|
|
$rank['rank'] = '151';
|
|
|
} elseif ($rank['rank'] == -2) {
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
//if ($rank['rank'] == '151') {
|
|
|
if ($rank['rank'] != -1 and $rank['rank'] != -2 and $this->Keywordsrank_model->add_rank($v->kw_id, $rank['rank'], $rank['source'], $rank['engines'])) {
|
|
|
$this->load->model('InfoSMS_model');
|
|
|
$last_rank = isset($last_rank[0]) ? $last_rank[0]->kwr_rank : $rank['rank'];
|
|
|
$this->InfoSMS_model->readed_for_info($v->kw_id, 'keywords_rank');
|
|
|
$this->InfoSMS_model->add('keywords_rank', $v->kw_id, $rank['rank'], $last_rank, (string) ($rank['rank'] - $last_rank));
|
|
|
if ($engines != 'yandex')
|
|
|
$this->Keywords_model->update_time($v->kw_id);
|
|
|
}
|
|
|
}
|
|
|
var_dump($worldsList[0]->kw_keyworlds, $rank);
|
|
|
}
|
|
|
|
|
|
//计算关键词在google搜索引擎的排名
|
|
|
public function get_search_content_by_google($keyworlds, $site_url, $page, $site_code) {
|
|
|
$rank['rank'] = 0;
|
|
|
$rank['source'] = $site_url;
|
|
|
$rank['engines'] = 'google';
|
|
|
if ($page > 3) {
|
|
|
$rank['rank'] = 151;
|
|
|
return $rank;
|
|
|
}
|
|
|
|
|
|
//拼接关键词
|
|
|
/* $key_array = explode(' ',$keyworlds);
|
|
|
$search_worlds='';
|
|
|
foreach ($key_array as $v) {
|
|
|
$search_worlds.='+'.$v;
|
|
|
}
|
|
|
$search_worlds = substr($search_worlds,1); */
|
|
|
$search_worlds = urlencode($keyworlds);
|
|
|
|
|
|
//拼接搜索url链接
|
|
|
$start = ($page - 1) * 50;
|
|
|
$google_url = 'google.com';
|
|
|
$google_domain = array(
|
|
|
'gm' => 'google.de',
|
|
|
'vc' => 'google.fr',
|
|
|
'vac' => 'google.es',
|
|
|
'ru' => 'google.ru',
|
|
|
'it' => 'google.it',
|
|
|
'jp' => 'google.co.jp'
|
|
|
);
|
|
|
if (!empty($google_domain[$site_code]))
|
|
|
$google_url = $google_domain[$site_code];
|
|
|
//搜索使用的语种,默认是英语
|
|
|
$language = 'en';
|
|
|
$language_array = array('gm' => 'de', 'vc' => 'fr', 'vac' => 'es', 'ru' => 'ru', 'it' => 'it', 'jp' => 'jp');
|
|
|
if (!empty($language_array[$site_code]))
|
|
|
$language = $language_array[$site_code];
|
|
|
|
|
|
//抓取google搜索结果
|
|
|
$this->load->library('Snoopy');
|
|
|
$snoopy = $this->snoopy;
|
|
|
$snoopy->maxredirs = 0;
|
|
|
$snoopy->agent = '(compatible; MSIE 11; MSN 8; AOL 4.0; Windows 98)'; //模拟浏览器
|
|
|
if ($snoopy->fetch("http://www.$google_url/search?q=$search_worlds&start=$start&num=50&hl=$language")) {
|
|
|
$response = $snoopy->results;
|
|
|
//分析google搜索结果,计算排名
|
|
|
if (false !== stripos($response, '<h1>302 Moved</h1>')) {
|
|
|
$rank['rank'] = -2;
|
|
|
} elseif (false !== stripos($response, $site_url)) {
|
|
|
preg_match_all('/<h3 class="r">(.*)<\/h3>/smUi', $response, $store);
|
|
|
foreach ($store[1] as $k => $v) {
|
|
|
$ismatch = stripos($v, $site_url);
|
|
|
if (false !== $ismatch) {
|
|
|
//计算排名
|
|
|
$rank['rank'] = $start + $k + 1;
|
|
|
//抓取关键词来源
|
|
|
$end = stripos($v, '&', $ismatch);
|
|
|
$source = substr($v, $ismatch, $end);
|
|
|
$source = explode('&', $source);
|
|
|
$rank['source'] = $source[0];
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
$rank['rank'] = -1;
|
|
|
}
|
|
|
|
|
|
return $rank;
|
|
|
}
|
|
|
|
|
|
public function test() {
|
|
|
$ranks = $this->get_search_content_by_yandex('Путеводитель по городу гуйлинь', 'www.chinahighlights.ru');
|
|
|
var_dump($ranks);
|
|
|
}
|
|
|
|
|
|
//计算关键词在yandex搜索引擎的排名
|
|
|
public function get_search_content_by_yandex($keyworlds, $site_url, $page = 0, $sitecode = false) {
|
|
|
$rank['rank'] = 0;
|
|
|
$rank['source'] = $site_url;
|
|
|
$rank['engines'] = 'yandex';
|
|
|
$startrank = $page * 10;
|
|
|
$search_worlds = urlencode($keyworlds);
|
|
|
|
|
|
//抓取搜索结果数据
|
|
|
$this->load->library('Snoopy');
|
|
|
$snoopy = $this->snoopy;
|
|
|
$snoopy->host = 'yandex.ru';
|
|
|
$snoopy->referer = "http://www.yandex.ru/";
|
|
|
//$snoopy->maxredirs=0;
|
|
|
$snoopy->agent = 'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:20.0) Gecko/20100101 Firefox/20.0';
|
|
|
//$snoopy->agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36';//模拟浏览器
|
|
|
if ($snoopy->fetch("http://www.yandex.ru/yandsearch?p=$page&text=$search_worlds&lr=21436")) {
|
|
|
$response = $snoopy->results;
|
|
|
//分析YANDEX搜索结果,计算排名
|
|
|
if (false !== stripos($response, '<h1>302 Found</h1>')) {
|
|
|
$rank['rank'] = -2;
|
|
|
} elseif (false !== stripos($response, $site_url)) {
|
|
|
preg_match_all("/<b class=\"b-serp-item__number\">(.*)<\/a>/smUi", $response, $store);
|
|
|
foreach ($store[1] as $k => $v) {
|
|
|
$start_s = stripos($v, $site_url);
|
|
|
if (false !== $start_s) {
|
|
|
$rank['rank'] = $startrank + $k + 1;
|
|
|
$end_s = stripos($v, '" ', $start_s);
|
|
|
$source = substr($v, $start_s, $end_s);
|
|
|
$sources = explode('" ', $source);
|
|
|
$rank['source'] = $sources[0];
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
$rank['rank'] = -1;
|
|
|
}
|
|
|
return $rank;
|
|
|
}
|
|
|
|
|
|
//计算关键词在google搜索引擎的排名
|
|
|
public function get_search_content_by_yahoo($keyworlds, $site_url, $page = 1, $sitecode = false) {
|
|
|
$rank['rank'] = 0;
|
|
|
$rank['source'] = $site_url;
|
|
|
$site_url_array = explode('//', $site_url);
|
|
|
$site_url = $site_url_array[1];
|
|
|
$rank['engines'] = 'yahoo';
|
|
|
if ($page > 3) {
|
|
|
$rank['rank'] = 151;
|
|
|
return $rank;
|
|
|
}
|
|
|
|
|
|
//拼接关键词
|
|
|
/* $key_array = explode(' ',$keyworlds);
|
|
|
$search_worlds='';
|
|
|
foreach ($key_array as $v) {
|
|
|
$search_worlds.='+'.$v;
|
|
|
}
|
|
|
$search_worlds = substr($search_worlds,1); */
|
|
|
$search_worlds = urlencode($keyworlds);
|
|
|
$yahoourl = 'http://search.yahoo.com';
|
|
|
if ($sitecode == 'jp')
|
|
|
$yahoourl = 'http://search.yahoo.co.jp';
|
|
|
|
|
|
$start = ($page - 1) * 50 + 1;
|
|
|
$pstart = $page + 4;
|
|
|
|
|
|
//抓取yahoo搜索结果
|
|
|
$this->load->library('Snoopy');
|
|
|
$snoopy = $this->snoopy;
|
|
|
$snoopy->maxredirs = 0;
|
|
|
$snoopy->agent = '(compatible; MSIE 9; MSN 8; AOL 4.0; Windows 98)'; //模拟浏览器
|
|
|
if ($snoopy->fetch("$yahoourl/search;_ylt=A0SO8yDYHxBT6xQAWAFXNyoA?p=$search_worlds&n=100&ei=UTF-8&va_vt=any&vo_vt=any&ve_vt=any&vp_vt=any&vst=0&vf=all&vm=i&fl=0&fr=sfp&b=$start&pstart=$pstart")) {
|
|
|
$response = $snoopy->results;
|
|
|
//分析yahoo搜索结果,计算排名
|
|
|
//出现被屏蔽情况
|
|
|
if (false !== stripos($response, 'document has moved')) {
|
|
|
$rank['rank'] = -2;
|
|
|
} else {
|
|
|
preg_match_all('/<div id=\"web\">(.*)<\/ol>/smUi', $response, $search_c);
|
|
|
if (false !== stripos($search_c[0][0], $site_url)) {
|
|
|
preg_match_all('/<li><a(.*)<\/a>/smUi', $search_c[0][0], $store);
|
|
|
foreach ($store[1] as $k => $v) {
|
|
|
$ismatch = stripos($v, $site_url);
|
|
|
if (false !== $ismatch) {
|
|
|
//计算排名
|
|
|
$rank['rank'] = $start + $k + 1;
|
|
|
//抓取关键词来源
|
|
|
$end = stripos($v, '"><b>', $ismatch);
|
|
|
$source = substr($v, $ismatch, $end);
|
|
|
$source = explode('"><b>', $source);
|
|
|
$rank['source'] = 'http://' . $source[0];
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
$rank['rank'] = -1;
|
|
|
}
|
|
|
return($rank);
|
|
|
}
|
|
|
|
|
|
//返回指定页面路径pagepath在指定时间内的成效信息
|
|
|
public function get_analytics_by_date($loadtotal = 0) {
|
|
|
$data = array();
|
|
|
//获取请求的日期范围以及请求的页面路径
|
|
|
$startdate = $this->input->post('startdate');
|
|
|
$enddate = $this->input->post('enddate');
|
|
|
$pagepath = $this->input->post('pagepath');
|
|
|
//如果开始时间为空则把开始时间设为往前七天
|
|
|
if ($startdate == '') {
|
|
|
$startdate = time() - 7 * 24 * 60 * 60;
|
|
|
} else {
|
|
|
$startdate = strtotime($startdate);
|
|
|
}
|
|
|
//如果结束时间为空,则把结束时间设为当天
|
|
|
if ($enddate == '') {
|
|
|
$enddate = time();
|
|
|
} else {
|
|
|
$enddate = strtotime($enddate);
|
|
|
}
|
|
|
if ($startdate > $enddate) {
|
|
|
echo "请选择正确的日期";
|
|
|
die();
|
|
|
}
|
|
|
//查取google分析数据
|
|
|
$data['analytics_list'] = $this->analytics_model->get_analytics_list($pagepath, 50, $startdate, $enddate);
|
|
|
$total_visit['pv'] = $total_visit['uv'] = $total_visit['agv_time'] = $total_visit['entrances'] = 0;
|
|
|
$analytics_count = 0;
|
|
|
//访问量汇总
|
|
|
foreach ($data['analytics_list'] as $v) {
|
|
|
$total_visit['pv']+=$v->kwa_pageviews;
|
|
|
$total_visit['uv']+=$v->kwa_uniquepageviews;
|
|
|
$total_visit['agv_time']+=$v->kwa_avgtimeonpage;
|
|
|
$total_visit['entrances']+=$v->kwa_entrances;
|
|
|
$analytics_count++;
|
|
|
}
|
|
|
$analytics_count == 0 ? $analytics_count = 1 : null;
|
|
|
$total_visit['agv_time'] = ceil($total_visit['agv_time'] / $analytics_count);
|
|
|
$data['total_visit'] = $total_visit;
|
|
|
//如果loadtotal==1,则只返回google分析的汇总数据;如果loadtotal==0,则返回明细数据表
|
|
|
$data['loadtotal'] = $loadtotal;
|
|
|
|
|
|
$this->load->view('keyworlds/analytics', $data);
|
|
|
}
|
|
|
|
|
|
//从google分析接口获取数据并入库,$start_index:从接口提取数据时的起始条数
|
|
|
public function get_google_analytics_data($sitecode = 'cht', $start_index = 1) {
|
|
|
session_start();
|
|
|
//判断该站点是否已经更新.
|
|
|
$update_count = $this->analytics_model->get_update_count($sitecode);
|
|
|
$update_count;
|
|
|
$flag = false;
|
|
|
switch ($sitecode) {
|
|
|
case 'cht':
|
|
|
case 'jp':
|
|
|
case 'ct':
|
|
|
//对于以上三个站点如果数据库中存在的数量大于当前起始数,则表示该站点已经更新
|
|
|
if ($update_count > $start_index)
|
|
|
$flag = true;
|
|
|
break;
|
|
|
default:
|
|
|
//对于其他站点如果数据库中存在的数量大于0,则表示该站点已经更新
|
|
|
if ($update_count > 0) {
|
|
|
$flag = true;
|
|
|
}
|
|
|
}
|
|
|
//如果站点已经更新,则放弃从google分析抓取数据
|
|
|
if ($flag) {
|
|
|
echo 'finished';
|
|
|
exit();
|
|
|
}
|
|
|
//初始化Google_Client
|
|
|
require_once APPPATH . 'libraries/Google_Client/Google_Client.php';
|
|
|
require_once APPPATH . 'libraries/Google_Client/contrib/Google_AnalyticsService.php';
|
|
|
$baseurl = site_url("Keyworlds/get_google_analytics_data/");
|
|
|
$revokeUrl = $baseurl . '?action=revoke';
|
|
|
|
|
|
//google分析接口过滤条件
|
|
|
$site_config = array('cht' => 5120, 'gm' => 6028186, 'vc' => 68571571, 'vac' => 68484932, 'ru' => 68577400, 'it' => 68564185, 'jp' => 574459, 'tbt' => 715050, 'sht' => 4705635, 'yz' => 68484504, 'gl' => 68460695, 'mbj' => 68479244, 'ct' => 8567691);
|
|
|
$profileId = $site_config[$sitecode]; //网站标志ID
|
|
|
$analytics_start_time = date('Y-m-d', time() - 7 * 24 * 60 * 60); //获取数据的开始时间
|
|
|
$analytics_end_time = date('Y-m-d', time()); //获取数据的结束时间
|
|
|
//授权设置
|
|
|
$client = new Google_Client();
|
|
|
$client->setClientId('50804699713-l5sfpffqpo4kbrbhk9tf53049labr214.apps.googleusercontent.com');
|
|
|
$client->setClientSecret('_IpQ8Qi1QFlogmxiMhIRHgEa');
|
|
|
$client->setRedirectUri('http://localhost:9999/info.php/Keyworlds/get_google_analytics_data');
|
|
|
$client->setApplicationName('Google Analytics Application');
|
|
|
$client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));
|
|
|
$client->setUseObjects(true);
|
|
|
$client->setAccessType('offline');
|
|
|
|
|
|
//解除授权
|
|
|
if (isset($_GET['action']) && $_GET['action'] == 'revoke') {
|
|
|
$accessToken = $_SESSION['access_token'];
|
|
|
if ($accessToken) {
|
|
|
$tokenObj = json_decode($accessToken);
|
|
|
$client->revokeToken($tokenObj->refresh_token);
|
|
|
unset($_SESSION['access_token']);
|
|
|
}
|
|
|
header("Location: $baseurl");
|
|
|
}
|
|
|
//获取授权
|
|
|
if (isset($_SESSION['access_token'])) {
|
|
|
$accessToken = $_SESSION['access_token'];
|
|
|
$client->setAccessToken($accessToken);
|
|
|
} else {
|
|
|
//授权过期重新生成access_token
|
|
|
$client->refreshToken('1/ZX7Qo-m_rxIL1_LqE8vM2sYKATkAJxm0Fc5g7BRXeo0');
|
|
|
$_SESSION['access_token'] = $client->getAccessToken();
|
|
|
//$accessToken = $client->authenticate();
|
|
|
//$_SESSION['access_token'] = $accessToken;
|
|
|
}
|
|
|
|
|
|
//获取数据
|
|
|
if ($client->getAccessToken()) {
|
|
|
$analytics = new Google_AnalyticsService($client);
|
|
|
//获取详细信息
|
|
|
$results = $analytics->data_ga->get(
|
|
|
'ga:' . $profileId, $analytics_start_time, $analytics_end_time, 'ga:visits', array(
|
|
|
'dimensions' => 'ga:pagePath,ga:source,ga:keyword',
|
|
|
'metrics' => 'ga:pageviews,ga:uniquePageviews,ga:avgTimeOnPage,ga:entrances,ga:exitRate,ga:pageValue',
|
|
|
'sort' => '-ga:pageviews',
|
|
|
'max-results' => '10000',
|
|
|
'start-index' => $start_index
|
|
|
)
|
|
|
);
|
|
|
}
|
|
|
$_SESSION['access_token'] = $client->getAccessToken();
|
|
|
|
|
|
$columnheader = array('pagepath', 'source', 'keyword', 'pv', 'uv', 'avgtimeonpage', 'entrances', 'exitrate', 'pageValue');
|
|
|
$output = array();
|
|
|
$txt = '';
|
|
|
foreach ($results->getRows() as $row) {
|
|
|
foreach ($row as $i => $cell) {
|
|
|
$table[$columnheader[$i]] = htmlspecialchars($cell, ENT_NOQUOTES);
|
|
|
}
|
|
|
$txt.=$table['source'] . '|@|' . $table['pv'] . '|@|' . $table['uv'] . '|@|' . $table['avgtimeonpage'] . '|@|' . $table['entrances'] . '|@|' . $table['exitrate'] . '|@|' . $table['pageValue'] . '|@|' . time() . '|@|' . $sitecode . '|@|' . $table['pagepath'] . '|@|' . substr($table['keyword'], 0, 255) . "\r\n";
|
|
|
$output[] = $table;
|
|
|
unset($table);
|
|
|
}
|
|
|
$fp = fopen(APPPATH . 'helpers/analytics_data.txt', 'wb');
|
|
|
fwrite($fp, $txt);
|
|
|
fclose($fp);
|
|
|
$this->analytics_model->add_analytics_by_txt();
|
|
|
echo $sitecode;
|
|
|
}
|
|
|
|
|
|
//从cvs文件导入关键词
|
|
|
public function hand_add($parentid, $bookname) {
|
|
|
$row = 1; //第一行开始
|
|
|
$filename = APPPATH . $bookname . '.csv';
|
|
|
if (($handle = fopen($filename, "r")) !== false) {
|
|
|
while (($dataSrc = fgetcsv($handle)) !== false) {
|
|
|
$num = count($dataSrc);
|
|
|
for ($c = 0; $c < $num; $c++) {//列 column
|
|
|
if ($row === 1) {//第一行作为字段
|
|
|
$dataName[] = $dataSrc[$c]; //字段名称
|
|
|
} else {
|
|
|
foreach ($dataName as $k => $v) {
|
|
|
if ($k == $c) {//对应的字段
|
|
|
$dataRtn[$v] = @iconv('GBK', 'UTF-8', $dataSrc[$c]);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
if (!empty($dataRtn)) {
|
|
|
$insert_id = $this->InfoStructures_model->Add($parentid);
|
|
|
if ($insert_id && $kw_id = $this->Keywords_model->add_keyworlds($dataRtn['keyword'], $dataRtn['site'], time() - 24 * 60 * 60, $insert_id, 1)) {
|
|
|
$datas[] = array('name' => 'ok', 'value' => $this->Keywords_model->get_keyworld($kw_id));
|
|
|
} else {
|
|
|
$datas[] = array('name' => 'no', 'value' => $this->lang->line('form_info_error'));
|
|
|
}
|
|
|
echo $insert_id;
|
|
|
$data[] = $dataRtn;
|
|
|
}
|
|
|
|
|
|
$row++;
|
|
|
}
|
|
|
fclose($handle);
|
|
|
var_dump($data);
|
|
|
echo 'ok';
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//收录查询结果
|
|
|
public function check_embody() {
|
|
|
$this->load->model('InfoMetas_model');
|
|
|
$this->load->model('InfoSMS_model');
|
|
|
//获取未收录的数据
|
|
|
$today_date = date('Y-m-d', time() - 3 * 24 * 60 * 60);
|
|
|
$embody = $this->InfoMetas_model->get_unembody_content($today_date);
|
|
|
if (empty($embody)) {
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
//页面url
|
|
|
$embody = $embody[0];
|
|
|
if ($embody->ic_url == '')
|
|
|
return false;
|
|
|
$site = $this->config->item('site');
|
|
|
$site_url = $site[$embody->ic_sitecode]['site_url'];
|
|
|
$site_url = $site_url . $embody->ic_url;
|
|
|
//收录查询搜索url
|
|
|
$search_worlds = 'site:' . $site_url;
|
|
|
$search_worlds;
|
|
|
$google_url = 'google.com';
|
|
|
$language = 'en';
|
|
|
$language_array = array('gm' => 'de', 'vc' => 'fr', 'vac' => 'es', 'ru' => 'ru', 'it' => 'it', 'jp' => 'jp');
|
|
|
if (!empty($language_array[$embody->ic_sitecode]))
|
|
|
$language = $language_array[$embody->ic_sitecode];
|
|
|
|
|
|
|
|
|
//抓取google搜索结果
|
|
|
$this->load->library('Snoopy');
|
|
|
$snoopy = $this->snoopy;
|
|
|
$snoopy->maxredirs = 0;
|
|
|
$snoopy->agent = '(compatible; MSIE 9; MSN 8; AOL 4.0; Windows 98)'; //模拟浏览器
|
|
|
if ($snoopy->fetch("http://www.$google_url/search?q=$search_worlds&start=0&num=10&hl=$language")) {
|
|
|
$response = $snoopy->results;
|
|
|
if (false !== stripos($response, '<h1>302 Moved</h1>')) {
|
|
|
echo 302;
|
|
|
return false;
|
|
|
}
|
|
|
preg_match_all('/<h3 class="r">(.*)<\/h3>/smUi', $response, $store);
|
|
|
if (isset($store[1][0]) && false !== stripos($store[1][0], $site_url)) {
|
|
|
$ismatch = stripos($store[1][0], $site_url);
|
|
|
$end = stripos($store[1][0], '&', $ismatch);
|
|
|
$source = substr($store[1][0], $ismatch, $end);
|
|
|
$source = explode('&', $source);
|
|
|
$source = $source[0];
|
|
|
if ($source == $site_url) {
|
|
|
$this->InfoMetas_model->update($embody->im_ic_id, 'meta_embody', '1');
|
|
|
$this->InfoSMS_model->add('info', $embody->im_ic_id, $embody->ic_author, 'lyy', '已收录');
|
|
|
echo 'ok';
|
|
|
} else {
|
|
|
$this->InfoMetas_model->update($embody->im_ic_id, 'meta_embody', date('Y-m-d H:i:s'));
|
|
|
$this->InfoSMS_model->add('info', $embody->im_ic_id, $embody->ic_author, 'lyy', '未被收录');
|
|
|
echo 'no';
|
|
|
}
|
|
|
} else {
|
|
|
$this->InfoMetas_model->update($embody->im_ic_id, 'meta_embody', date('Y-m-d H:i:s'));
|
|
|
$this->InfoSMS_model->add('info', $embody->im_ic_id, $embody->ic_author, 'lyy', '未被收录');
|
|
|
echo 'no';
|
|
|
}
|
|
|
}
|
|
|
echo '<br>end';
|
|
|
}
|
|
|
|
|
|
//定时批量更新CDN
|
|
|
public function update_cdn() {
|
|
|
$this->load->model('Logs_model');
|
|
|
$last_update = $this->Logs_model->get_last_cdn_update_info();
|
|
|
if (empty($last_update)) {
|
|
|
$last_log = $this->Logs_model->get_last_log();
|
|
|
$this->Logs_model->write('update_cdn', $last_log->log_id);
|
|
|
$last_update->log_res_id = $last_log->log_id;
|
|
|
}
|
|
|
$last_update_id = $last_update->log_res_id;
|
|
|
$list = $this->Logs_model->get_uncdn_list($last_update_id);
|
|
|
if (empty($list)) {
|
|
|
echo 'updated';
|
|
|
die();
|
|
|
}
|
|
|
//设置需要更新的url路径
|
|
|
$site_path = array();
|
|
|
$path_array = array();
|
|
|
$site_array = array('vac', 'ru', 'it'); //需要更新的站点
|
|
|
foreach ($list as $v) {
|
|
|
if (in_array($v->is_sitecode, $site_array)) {
|
|
|
if (!isset($site_path[$v->is_sitecode])) {
|
|
|
$site_path[$v->is_sitecode] = '';
|
|
|
$path_array[$v->is_sitecode] = array();
|
|
|
}
|
|
|
if (!in_array($v->ic_url, $path_array[$v->is_sitecode])) {
|
|
|
$site_path[$v->is_sitecode].="&path=" . $v->ic_url;
|
|
|
$path_array[$v->is_sitecode][] = $v->ic_url;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
//已经给是最新的了
|
|
|
if (empty($site_path)) {
|
|
|
echo 'updated.';
|
|
|
die();
|
|
|
}
|
|
|
|
|
|
//更新CDN缓存
|
|
|
$notice = array();
|
|
|
$flag = false;
|
|
|
$siteconfig = $this->config->item('site');
|
|
|
foreach ($site_path as $k => $cdn) {
|
|
|
$pad = str_replace('http://', '', $siteconfig[$k]['site_url']);
|
|
|
$result = file_get_contents('https://openapi.us.cdnetworks.com/purge/rest/doPurge?user=ycc@chinahighlights.com&pass=cXi2UbsTrw9Urv&pad=' . $pad . '&type=item' . $cdn . '&output=json');
|
|
|
$result = json_decode($result, true);
|
|
|
if ($result['resultCode'] != 200) {
|
|
|
$notice[] = $k . ':' . $result['notice'] . $result['details'];
|
|
|
} else {
|
|
|
$notice[] = $k . ':' . $result['resultCode'] . '<br>' . $cdn;
|
|
|
$flag = TRUE;
|
|
|
}
|
|
|
}
|
|
|
//修改最近更新的信息的log_id,方便下次更新读取数据
|
|
|
if ($flag) {
|
|
|
$lastinfo = end($list);
|
|
|
$this->Logs_model->update('update_cdn', $lastinfo->log_id);
|
|
|
}
|
|
|
|
|
|
var_dump($notice);
|
|
|
}
|
|
|
|
|
|
public function mobile_friendly_test() {
|
|
|
set_time_limit(60);
|
|
|
$url = $this->input->post('pageurl');
|
|
|
$googleapis = 'https://www.googleapis.com/pagespeedonline/v3beta1/mobileReady?url=' . $url;
|
|
|
|
|
|
$ch = curl_init();
|
|
|
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
|
|
|
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22');
|
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
|
|
curl_setopt($ch, CURLOPT_URL, $googleapis);
|
|
|
$response2 = curl_exec($ch);
|
|
|
$response = json_decode($response2);
|
|
|
|
|
|
$data['pageurl'] = $url;
|
|
|
$data['score'] = 60;
|
|
|
$data['pass'] = false;
|
|
|
isset($response->ruleGroups->USABILITY->score) && $data['score'] = $response->ruleGroups->USABILITY->score;
|
|
|
isset($response->ruleGroups->USABILITY->pass) && $data['pass'] = $response->ruleGroups->USABILITY->pass;
|
|
|
if (empty($response2)) {
|
|
|
$pass_text = '请求超时';
|
|
|
$title = '请求超时,请稍后再试!';
|
|
|
$link_color = 'text-error';
|
|
|
$data['score'] = '-';
|
|
|
} elseif ($data['pass']) {
|
|
|
$pass_text = 'OK';
|
|
|
$link_color = 'text-info';
|
|
|
$title = 'Awesome! This page is mobile-friendly (友好度' . $data['score'] . ', 总分100分)';
|
|
|
} else {
|
|
|
$pass_text = 'No';
|
|
|
$title = 'Not mobile-friendly (友好度' . $data['score'] . ', 总分100分)';
|
|
|
$link_color = 'text-error';
|
|
|
}
|
|
|
$html = '<a title="' . $title . '" class="' . $link_color . '" target="__blank" href="https://developers.google.com/speed/pagespeed/insights/?utm_source=analytics&tab=mobile&url=' . $data['pageurl'] . '"><span>' . $pass_text . ',友好度:' . $data['score'] . '</span></a>';
|
|
|
echo $html;
|
|
|
}
|
|
|
|
|
|
}
|