Merge branch 'keyword'

hotfix/远程访问多媒体中心
lyt 8 years ago
commit f546c63983

@ -70,6 +70,7 @@ class Keyworlds extends CI_Controller {
$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'])) {
@ -94,10 +95,15 @@ class Keyworlds extends CI_Controller {
}
}
}
// print_r($data['wordcount']);
// print_r($rank_json);
// 拼接highcharts数据格式
$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')
@ -110,8 +116,25 @@ class Keyworlds extends CI_Controller {
}
$data['rank_json'] = substr($data['rank_json'], 0, -1);
$data['rank_json'] .=']},';
// 叶子节点 显示mobile记录
if ($data['wordcount'] == 1) {
$data['rank_json'] .='{"name":"' . $ra[$data_time[0]]->kw_keyworlds . '(mobile)","data":[';
foreach ($ra as $v) {
$kwr_mobile_rank = $v->kwr_mobile_rank ? $v->kwr_mobile_rank : 'null' ;
if ($v->kwr_mobile_rank == '>150')
$kwr_mobile_rank = 100;
if (strtolower($this->config->item('site_code')) == 'gm') {
$data['rank_json'] .=-$kwr_mobile_rank . ',';
} else {
$data['rank_json'] .=$kwr_mobile_rank . ',';
}
}
$data['rank_json'] = substr($data['rank_json'], 0, -1);
$data['rank_json'] .=']},';
}
}
$data['rank_json'] = substr($data['rank_json'], 0, -1) . ']';
// print_r($data['rank_json']);
$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];
@ -120,9 +143,10 @@ class Keyworlds extends CI_Controller {
redirect(site_url("Keyworlds/index"));
}
$data['analytics'] = $this->analytics_model->get_keyword_analytics($data['current_keyworld']->kw_keyworlds, 30);
// $data['analytics'] = $this->analytics_model->get_keyword_analytics($data['current_keyworld']->kw_keyworlds, 30);
$data['site_code'] = strtolower($this->config->item('site_code'));
// print_r($data['rank_info']);
// print_r($data["rank_json"]);
$this->load->view('bootstrap/header', $data);
$this->load->view('keyworlds/index');
$this->load->view('bootstrap/footer');
@ -231,69 +255,105 @@ class Keyworlds extends CI_Controller {
$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');
$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',
'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',
'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';
$today = strtotime(date('Y-m-d'));
$client_flag = 'PC';
$rank_flag = 'rank';
$source_flag = 'source';
$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) {
if (isset($last_rank[0]->kwr_datetime) and ( time() - $last_rank[0]->kwr_datetime) < 180) {
$data[] = array('name' => 'rank', 'value' => '查询过于频繁,请等待三分钟!');
echo json_encode($data);
return TRUE;
} else if (empty($last_rank[0]->kwr_mobile_rank) &&
intval($last_rank[0]->kwr_datetime) > intval($today)) {
$client_flag = 'm';
$rank_flag = 'rank_m';
$source_flag = 'source_m';
}
//获取排名详细情况,排名超过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';
$rank = $this->$method($client_flag, $keyworlds, $site_url, $j, $site_code);
// 仅更新一个字段
if (intval($rank['rank']) == 0) {
$rank['rank'] = $last_rank[0]->kwr_rank;
$rank['source'] = $last_rank[0]->kwr_source;
} else {
$rank['rank_m'] = null;
$rank['source_m'] = null;
}
if ($rank[$rank_flag] != 0) {
if ($rank[$rank_flag] > 150)
$rank[$rank_flag] = '151';
break;
}elseif ($i == 15) {
$rank['rank'] = '151';
$rank[$rank_flag] = '151';
break;
} elseif ($rank['rank'] == -2) {
} elseif ($rank[$rank_flag] == -2) {
break;
}
}
//保存排名数据
if ($rank['rank'] == '151') { //超过151的排名则不记录
if ($rank[$rank_flag] == '151') { //超过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'])) {
if ($rank[$rank_flag] != -1 and $rank[$rank_flag] != -2 and $this->Keywordsrank_model->update_rank($last_rank[0]->kwr_id, $rank['rank'], $rank['rank_m'], $rank['source'], $rank['source_m'])) {
$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'])) {
if ($rank[$rank_flag] != -1 and $rank[$rank_flag] != -2 and $this->Keywordsrank_model->add_rank($kw_id, $rank['rank'], $rank['rank_m'], $rank['source'], $rank['source_m'], $rank['engines'])) {
$this->Keywords_model->update_time($kw_id);
}
}
$data[] = array('name' => 'rank', 'value' => $rank['rank']);
$data[] = array('name' => 'rank', 'value' => ($rank['rank'] . " -- " . $rank["rank_m"]));
echo json_encode($data);
return TRUE;
}
//自动爬取排名
public function get_auto_rank($engines = '', $fix_site_code = '') {
set_time_limit(60);
$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;
}
$today = strtotime(date('Y-m-d'));
$client_flag = "PC";
$rank_flag = 'rank';
$source_flag = 'source';
if (intval($worldsList[0]->kwr_datetime) >= intval($today) ) {
$client_flag = "m";
$rank_flag = 'rank_m';
$source_flag = 'source_m';
}
$last_rank = $this->Keywordsrank_model->get_rank_list($worldsList[0]->kw_id);
//获取排名并保存
@ -304,26 +364,30 @@ class Keyworlds extends CI_Controller {
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);
$rank = $this->get_search_content_by_google($client_flag,$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);
$rank = $this->get_search_content_by_yandex($client_flag,$v->kw_keyworlds, $site_url, $i - 1);
}
if ($rank['rank'] != 0) {
if ($rank['rank'] > 150)
$rank['rank'] = '151';
if (intval($rank['rank']) == 0) {
$rank['rank'] = $last_rank[0]->kwr_rank;
$rank['source'] = $last_rank[0]->kwr_source;
} else {
$rank['rank_m'] = null;
$rank['source_m'] = null;
}
if ($rank[$rank_flag] != 0) {
if ($rank[$rank_flag] > 150)
$rank[$rank_flag] = '151';
break;
}elseif ($i == 15) {
$rank['rank'] = '151';
} elseif ($rank['rank'] == -2) {
$rank[$rank_flag] = '151';
} elseif ($rank[$rank_flag] == -2) {
break;
}
}
if ($rank['rank'] == '151') { //超过151的排名则不记录
if ($rank[$rank_flag] == '151') { //超过151的排名则不记录
$this->Keywords_model->update_time($v->kw_id);
}else if ($rank['rank'] != -1 and $rank['rank'] != -2 and $this->Keywordsrank_model->add_rank($v->kw_id, $rank['rank'], $rank['source'], $rank['engines'])) {
}else if ($rank[$rank_flag] != -1 and $rank[$rank_flag] != -2 and $this->Keywordsrank_model->add_rank($v->kw_id, $rank['rank'], $rank['rank_m'], $rank['source'], $rank['source_m'], $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');
@ -336,22 +400,19 @@ class Keyworlds extends CI_Controller {
}
//计算关键词在google搜索引擎的排名
public function get_search_content_by_google($keyworlds, $site_url, $page, $site_code) {
public function get_search_content_by_google($client_flag,$keyworlds, $site_url, $page, $site_code) {
$site_url .= "/";
$rank['rank'] = 0;
$rank['source'] = $site_url;
$rank['rank_m'] = 0;
$rank['source_m'] = $site_url;
$rank['engines'] = 'google';
if ($page > 3) {
$rank['rank'] = 151;
$rank['rank_m'] = 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链接
@ -374,292 +435,103 @@ class Keyworlds extends CI_Controller {
$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;
$url = "https://www.$google_url/search?q=$search_worlds&start=$start&num=50&hl=$language";
$match_fun = '/<h3 class="r">(.*)<\/h3>/smUi';
$rank_flag = 'rank';
$source_flag = 'source';
if ($client_flag == 'm') {
$match_fun = '/<a class="_Olt _bCp"(.*)<div/smUi';
$rank_flag = 'rank_m';
$source_flag = 'source_m';
}
$response = $this->curl($url,$client_flag);
if ($response) {
//分析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);
preg_match_all($match_fun, $response, $store);
foreach ($store[1] as $k => $v) {
$ismatch = stripos($v, $site_url);
if (false !== $ismatch) {
//计算排名
$rank['rank'] = $start + $k + 1;
$rank[$rank_flag] = $start + $k + 1;
//抓取关键词来源
$end = stripos($v, '&', $ismatch);
$end = strpos($v, '"', $ismatch);
$source = substr($v, $ismatch, $end);
$source = explode('&', $source);
$rank['source'] = $source[0];
$source = explode('"', $source);
$rank[$source_flag] = $source[0];
break;
}
}
} else {
// log_message('error','google search response not found CH');
}
} else {
$rank['rank'] = -1;
$rank[$rank_flag] = -1;
}
return $rank;
}
public function test() {
$ranks = $this->get_search_content_by_yandex('Путеводитель по городу гуйлинь', 'www.chinahighlights.ru');
// $ranks = $this->get_search_content_by_yandex('Путеводитель по городу гуйлинь', 'www.chinahighlights.ru');
// $ranks = $this->get_search_content_by_google('PC','travel china', 'https://www.chinahighlights.com',1,"en");
var_dump($ranks);
}
//计算关键词在yandex搜索引擎的排名
public function get_search_content_by_yandex($keyworlds, $site_url, $page = 0, $sitecode = false) {
public function get_search_content_by_yandex($client_flag,$keyworlds, $site_url, $page = 0, $sitecode = false) {
$site_url .= "/";
$rank['rank'] = 0;
$rank['rank_m'] = 0;
$rank['source'] = $site_url;
$rank['source_m'] = $site_url;
$rank['engines'] = 'yandex';
if ($page > 3) {
$rank['rank'] = 151;
$rank['rank_m'] = 151;
return $rank;
}
$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;
$url = "https://www.yandex.ru/yandsearch?p=$page&text=$search_worlds&lr=21436";
$refer = "https://www.yandex.ru/";
$match_fun = '/<a class=\"link link_theme_normal organic__url link_cropped_no i-bem\"(.*)class=/smUi';
$rank_flag = 'rank';
$source_flag = 'source';
if ($client_flag == 'm') {
$match_fun = '/<a class="link link_theme_normal organic__url i-bem"(.*)class=/smUi';
$rank_flag = 'rank_m';
$source_flag = 'source_m';
}
$response = $this->curl($url,$client_flag,$refer);
if ($response) {
// 分析YANDEX搜索结果计算排名
if (false !== stripos($response, '<h1>302 Found</h1>')) {
if (false !== stripos($response, '<h1>302 Found</h1>') ||
false !== stripos($response, 'капчей') || false !== stripos($response, 'captcha')) {
$rank['rank'] = -2;
} elseif (false !== stripos($response, $site_url)) {
preg_match_all("/<b class=\"b-serp-item__number\">(.*)<\/a>/smUi", $response, $store);
preg_match_all($match_fun, $response, $store);
foreach ($store[1] as $k => $v) {
$start_s = stripos($v, $site_url);
if (false !== $start_s) {
$rank['rank'] = $startrank + $k + 1;
$rank[$rank_flag] = $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];
$rank[$source_flag] = $sources[0];
break;
}
}
}
}
} else {
$rank['rank'] = -1;
}
return($rank);
// log_message('error','yandex not found CH');
}
//返回指定页面路径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();
$rank[$rank_flag] = -1;
}
//查取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;
return $rank;
}
//从cvs文件导入关键词
@ -699,70 +571,6 @@ class Keyworlds extends CI_Controller {
}
}
//收录查询结果
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');
@ -824,44 +632,43 @@ class Keyworlds extends CI_Controller {
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;
protected function curl($url, $useragent = 'PC', $referer = false) {
if ($useragent == 'PC') {
$useragent = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22";
} else {
$useragent = "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Mobile Safari/537.36";
}
$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_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_FAILONERROR, false);
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分)';
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
// curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5_HOSTNAME);
// curl_setopt($ch, CURLOPT_PROXY, "202.103.68.28:11111"); // 本地代理
if ($referer) {
curl_setopt($ch, CURLOPT_REFERER, $referer);
}
$headers = array('content-type: application/x-www-form-urlencoded;charset=UTF-8');
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$reponse = curl_exec($ch);
if (curl_errno($ch)) {
log_message('error', "curl error code: ".curl_error($ch).";");
} else {
$pass_text = 'No';
$title = 'Not mobile-friendly (友好度' . $data['score'] . ', 总分100分)';
$link_color = 'text-error';
$httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (200 !== $httpStatusCode) {
log_message('error', "curl Request html Status Code: ".$httpStatusCode.";");
}
}
$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;
curl_close($ch);
return $reponse;
}
}

@ -67,10 +67,8 @@ class infoKeywords_model extends CI_Model
public function get_keyworld_list($top=1,$engines='',$site_code=false)
{
$today = strtotime(date('Y-m-d'));
$mapsql="";
$map=" AND kw_datetime<'$today'";
$map = " ";
if ($engines=='yandex') {
$mapsql="LEFT JOIN infoKeywordsrank ON kwr_kw_id=kw_id and kwr_datetime<'$today'";
$map=" AND kw_sitecode='ru' ";
}elseif ($engines=='baidu') {
$map.=" AND kw_sitecode='wt' ";
@ -88,11 +86,17 @@ class infoKeywords_model extends CI_Model
kw_is_id,
kw_sitecode,
kw_datetime,
kw_status
FROM infoKeywords $mapsql
kw_status,
kwr_datetime,
kwr_rank,
kwr_mobile_rank,
kwr_id
FROM infoKeywords INNER JOIN infoKeywordsrank ON kwr_kw_id=kw_id
AND kwr_id = ( SELECT MAX(kwr_id) FROM infoKeywordsrank WHERE kwr_kw_id=kw_id )
AND (kwr_datetime<'$today'
OR kwr_mobile_rank IS NULL)
WHERE kw_status=1 $map
ORDER BY newid()";
$query = $this->HT->query($sql);
//print_r($this->HT->queries);
$result = $query->result();

@ -9,27 +9,31 @@ class infoKeywordsrank_model extends CI_Model
}
//添加关键词排名
public function add_rank($kwr_kw_id,$kwr_rank,$kwr_source,$kwr_engines)
public function add_rank($kwr_kw_id,$kwr_rank,$kwr_mobile_rank,$kwr_source,$kwr_mobile_source,$kwr_engines)
{
$sql = "INSERT INTO infoKeywordsrank (
kwr_kw_id,
kwr_rank,
kwr_mobile_rank,
kwr_source,
kwr_mobile_source,
kwr_datetime,
kwr_engines
) VALUES (?,?,?,?,?)";
$query = $this->HT->query($sql, array($kwr_kw_id,$kwr_rank,$kwr_source,time(),$kwr_engines));
) VALUES (?,?,?,?,?,?,?)";
$query = $this->HT->query($sql, array($kwr_kw_id,$kwr_rank,$kwr_mobile_rank,$kwr_source,$kwr_mobile_source,time(),$kwr_engines));
return $this->HT->last_id('infoKeywordsrank');
}
//更新关键词排名
public function update_rank($kwr_id,$rank,$source){
public function update_rank($kwr_id,$rank,$mobile_rank,$source,$mobile_source){
$set_sql = "UPDATE infoKeywordsrank
SET kwr_datetime = ?,
kwr_rank=?,
kwr_source=?
kwr_mobile_rank=?,
kwr_source=?,
kwr_mobile_source=?
WHERE kwr_id = ?";
$result=$this->HT->query($set_sql, array(time(),$rank,$source,$kwr_id));
$result=$this->HT->query($set_sql, array(time(),$rank,$mobile_rank,$source,$mobile_source,$kwr_id));
return $result;
}
@ -46,7 +50,9 @@ class infoKeywordsrank_model extends CI_Model
$sql = "SELECT $topmap kwr_id,
kwr_kw_id,
kwr_rank,
kwr_mobile_rank,
kwr_source,
kwr_mobile_source,
kwr_engines,
kwr_datetime,
kw_keyworlds

@ -48,53 +48,28 @@
<tr>
<th>#</th>
<th>时间</th>
<th>来源</th>
<th>排名</th>
<th>来源(PC/Mobile)</th>
<th>排名(PC/Mobile)</th>
</tr>
</thead>
<tbody>
<?php foreach ($rank_info as $k=>$rank) { ?>
<?php $ki=0; foreach ($rank_info as $k=>$rank) { ?>
<tr>
<td><?php echo $k+1; ?></td>
<td><?php echo ++$ki; ?></td>
<td><?php echo date('Y-m-d H:i:s',$rank->kwr_datetime); ?></td>
<td><a target="_blank" href="<?php echo $rank->kwr_source; ?>"><?php echo $rank->kwr_source; ?></a></td>
<td><?php echo $rank->kwr_rank; ?></td>
<td>
<a target="_blank" href="<?php echo $rank->kwr_source; ?>"><?php echo $rank->kwr_source; ?></a>
<br>
<a target="_blank" href="<?php echo $rank->kwr_mobile_source; ?>"><?php echo $rank->kwr_mobile_source; ?></a>
</td>
<td><?php echo $rank->kwr_rank; ?>&nbsp;--&nbsp;
<?php $kwr_mobile_rank = $rank->kwr_mobile_rank ? $rank->kwr_mobile_rank : '未抓取'; echo $kwr_mobile_rank; ?></td>
</tr>
<?php if($k+1==7) break; ?>
<?php } ?>
</tbody>
</table><br><br>
<?php } ?>
<?php if (!empty($analytics)) { ?>
<legend>访问量明细</legend>
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>时间</th>
<th>路径</th>
<th>PV</th>
<th>UV</th>
<th>平均停留时间</th>
<th>跳出率</th>
</tr>
</thead>
<tbody>
<?php foreach ($analytics as $key_a=>$a) { ?>
<tr>
<td><?php echo $key_a+1; ?></td>
<td><?php echo date('Y-m-d',$a->kwa_datetime-7*24*60*60); ?></td>
<td><?php echo $a->kwa_pagepath; ?></td>
<td><?php echo $a->kwa_pageviews; ?></td>
<td><?php echo $a->kwa_uniquepageviews; ?></td>
<td><?php echo number_format($a->kwa_avgtimeonpage,1); ?></td>
<td><?php echo number_format($a->kwa_exitrate,1); ?>%</td>
</tr>
<?php } ?>
</tbody>
</table>
<?php } ?>
</div>
</div>
</div>
@ -353,7 +328,7 @@
categories: <?php echo $data_time; ?>,
labels: {rotation: -45,align: 'right'}
},
yAxis: {title: {text: '关键词排名'}},
yAxis: {title: {text: '关键词排名'},reversed:true,allowDecimals:false},
tooltip: {
enabled: true,
formatter: function() {

Loading…
Cancel
Save