关键词 改curl;HTTPS;+移动端;单个关键词页面图标增加移动端排名;图标Y轴升序;删除Google分析;删除Yahoo搜索;

hotfix/远程访问多媒体中心
lyt 8 years ago
parent 7ce925b753
commit 0cf51d7cf0

@ -16,7 +16,7 @@ class Keyworlds extends CI_Controller {
public function index($kw_is_id = '') { public function index($kw_is_id = '') {
$this->load->library('Accesscheck'); $this->load->library('Accesscheck');
$this->accesscheck->check_access(); $this->accesscheck->check_access(); // test--
$this->permission->is_admin(); $this->permission->is_admin();
//查询结构列表信息 //查询结构列表信息
@ -70,6 +70,7 @@ class Keyworlds extends CI_Controller {
$data['wordcount'] = 2; $data['wordcount'] = 2;
} }
} }
$data['rank_info'] = $this->Keywordsrank_model->get_rank_list($is_id_string, 30, $startdate, $enddate); $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); empty($data['rank_info']) && $data['rank_info'] = $this->Keywordsrank_model->get_rank_list($is_id_string, 30);
if (!empty($data['rank_info'])) { 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'] = '['; $data['rank_json'] = '[';
foreach ($rank_json as &$ra) { foreach ($rank_json as &$ra) {
ksort($ra); ksort($ra);
// 每条线
$data['rank_json'] .='{"name":"' . $ra[$data_time[0]]->kw_keyworlds . '","data":['; $data['rank_json'] .='{"name":"' . $ra[$data_time[0]]->kw_keyworlds . '","data":[';
// 每个点
foreach ($ra as $v) { foreach ($ra as $v) {
$kwr_rank = $v->kwr_rank; $kwr_rank = $v->kwr_rank;
if ($v->kwr_rank == '>150') 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'] = substr($data['rank_json'], 0, -1);
$data['rank_json'] .=']},'; $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) . ']'; $data['rank_json'] = substr($data['rank_json'], 0, -1) . ']';
// print_r($data['rank_json']);
$data['data_time'] = json_encode($data_time); $data['data_time'] = json_encode($data_time);
$data['rank_info'] = ''; $data['rank_info'] = '';
isset($rank_json[$data['current_keyworld']->kw_id]) && $data['rank_info'] = $rank_json[$data['current_keyworld']->kw_id]; 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")); 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')); $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('bootstrap/header', $data);
$this->load->view('keyworlds/index'); $this->load->view('keyworlds/index');
$this->load->view('bootstrap/footer'); $this->load->view('bootstrap/footer');
@ -228,12 +252,29 @@ class Keyworlds extends CI_Controller {
public function get_google_rank() { public function get_google_rank() {
$keyworlds = $this->input->post('keyworlds'); //搜索关键字 $keyworlds = $this->input->post('keyworlds'); //搜索关键字
$kw_id = $this->input->post('kw_id'); $kw_id = $this->input->post('kw_id');
$site_url = $this->config->item('site_url'); $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(
//$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'); 'cht' => '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', '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'); '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'); $site_code = $this->config->item('site_code');
$method = $engines[$site_code]; $method = $engines[$site_code];
@ -268,15 +309,15 @@ class Keyworlds extends CI_Controller {
if ($rank['rank'] == '151') { //超过151的排名则不记录 if ($rank['rank'] == '151') { //超过151的排名则不记录
$this->Keywords_model->update_time($kw_id); $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'))) { } 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'] != -1 and $rank['rank'] != -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); $this->Keywords_model->update_time($kw_id);
} }
} else { } 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'] != -1 and $rank['rank'] != -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); $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); echo json_encode($data);
return TRUE; return TRUE;
} }
@ -307,8 +348,6 @@ class Keyworlds extends CI_Controller {
$rank = $this->get_search_content_by_google($v->kw_keyworlds, $site_url, $i, $site_code); $rank = $this->get_search_content_by_google($v->kw_keyworlds, $site_url, $i, $site_code);
} else if ($engines == 'yandex') {//俄语站的yangdex } else if ($engines == 'yandex') {//俄语站的yangdex
$rank = $this->get_search_content_by_yandex($v->kw_keyworlds, $site_url, $i - 1); $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'] != 0) {
@ -323,7 +362,7 @@ class Keyworlds extends CI_Controller {
} }
if ($rank['rank'] == '151') { //超过151的排名则不记录 if ($rank['rank'] == '151') { //超过151的排名则不记录
$this->Keywords_model->update_time($v->kw_id); $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'] != -1 and $rank['rank'] != -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'); $this->load->model('InfoSMS_model');
$last_rank = isset($last_rank[0]) ? $last_rank[0]->kwr_rank : $rank['rank']; $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->readed_for_info($v->kw_id, 'keywords_rank');
@ -339,31 +378,27 @@ class Keyworlds extends CI_Controller {
public function get_search_content_by_google($keyworlds, $site_url, $page, $site_code) { public function get_search_content_by_google($keyworlds, $site_url, $page, $site_code) {
$rank['rank'] = 0; $rank['rank'] = 0;
$rank['source'] = $site_url; $rank['source'] = $site_url;
$rank['rank_m'] = 0;
$rank['source_m'] = $site_url;
$rank['engines'] = 'google'; $rank['engines'] = 'google';
if ($page > 3) { if ($page > 3) {
$rank['rank'] = 151; $rank['rank'] = 151;
$rank['rank_m'] = 151;
return $rank; 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); $search_worlds = urlencode($keyworlds);
//拼接搜索url链接 //拼接搜索url链接
$start = ($page - 1) * 50; $start = ($page - 1) * 50;
$google_url = 'google.com'; $google_url = 'google.com';
$google_domain = array( $google_domain = array(
'gm' => 'google.de', 'gm' => 'google.de',
'vc' => 'google.fr', 'vc' => 'google.fr',
'vac' => 'google.es', 'vac' => 'google.es',
'ru' => 'google.ru', 'ru' => 'google.ru',
'it' => 'google.it', 'it' => 'google.it',
'jp' => 'google.co.jp' 'jp' => 'google.co.jp'
); );
if (!empty($google_domain[$site_code])) if (!empty($google_domain[$site_code]))
$google_url = $google_domain[$site_code]; $google_url = $google_domain[$site_code];
@ -374,12 +409,9 @@ class Keyworlds extends CI_Controller {
$language = $language_array[$site_code]; $language = $language_array[$site_code];
//抓取google搜索结果 //抓取google搜索结果
$this->load->library('Snoopy'); $url = "https://www.$google_url/search?q=$search_worlds&start=$start&num=50&hl=$language";
$snoopy = $this->snoopy; $response = $this->curl($url);
$snoopy->maxredirs = 0; if ($response) {
$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搜索结果计算排名 //分析google搜索结果计算排名
if (false !== stripos($response, '<h1>302 Moved</h1>')) { if (false !== stripos($response, '<h1>302 Moved</h1>')) {
$rank['rank'] = -2; $rank['rank'] = -2;
@ -391,17 +423,43 @@ class Keyworlds extends CI_Controller {
//计算排名 //计算排名
$rank['rank'] = $start + $k + 1; $rank['rank'] = $start + $k + 1;
//抓取关键词来源 //抓取关键词来源
$end = stripos($v, '&', $ismatch); $end = strpos($v, '"', $ismatch);
$source = substr($v, $ismatch, $end); $source = substr($v, $ismatch, $end);
$source = explode('&', $source); $source = explode('"', $source);
$rank['source'] = $source[0]; $rank['source'] = $source[0];
break; break;
} }
} }
} else {
// log_message('error','google search response not found CH');
} }
} else { } else {
$rank['rank'] = -1; $rank['rank'] = -1;
} }
// 移动端
$response_m = $this->curl($url,"m");
if ($response_m) {
if (false !== stripos($response_m, '<h1>302 Moved</h1>')) {
$rank['rank_m'] = -2;
} elseif (false !== stripos($response_m, $site_url)) {
preg_match_all('/<a class="_Olt _bCp"(.*)<div/smUi', $response_m, $store_m);
foreach ($store_m[1] as $km => $vm) {
$ismatch_m = stripos($vm, $site_url);
if (false !== $ismatch_m) {
//计算排名
$rank['rank_m'] = $start + $km + 1;
//抓取关键词来源
$end_m = strpos($vm, '"', $ismatch_m);
$source_m = substr($vm, $ismatch_m, $end_m);
$source_m = explode('"', $source_m);
$rank['source_m'] = $source_m[0];
break;
}
}
}
} else {
$rank['rank_m'] = -1;
} // ---- 移动端 end ----
return $rank; return $rank;
} }
@ -414,26 +472,30 @@ class Keyworlds extends CI_Controller {
//计算关键词在yandex搜索引擎的排名 //计算关键词在yandex搜索引擎的排名
public function get_search_content_by_yandex($keyworlds, $site_url, $page = 0, $sitecode = false) { public function get_search_content_by_yandex($keyworlds, $site_url, $page = 0, $sitecode = false) {
$rank['rank'] = 0; $rank['rank'] = 0;
$rank['rank_m'] = 0;
$rank['source'] = $site_url; $rank['source'] = $site_url;
$rank['source_m'] = $site_url;
$rank['engines'] = 'yandex'; $rank['engines'] = 'yandex';
if ($page > 3) {
$rank['rank'] = 151;
$rank['rank_m'] = 151;
return $rank;
}
$startrank = $page * 10; $startrank = $page * 10;
$search_worlds = urlencode($keyworlds); $search_worlds = urlencode($keyworlds);
//抓取搜索结果数据 //抓取搜索结果数据
$this->load->library('Snoopy'); $url = "https://www.yandex.ru/yandsearch?p=$page&text=$search_worlds&lr=21436";
$snoopy = $this->snoopy; $refer = "https://www.yandex.ru/";
$snoopy->host = 'yandex.ru'; $response = $this->curl($url,'PC',$refer);
$snoopy->referer = "http://www.yandex.ru/"; if ($response) {
//$snoopy->maxredirs=0; // log_message('error','yandex 1');
$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搜索结果计算排名 //分析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; $rank['rank'] = -2;
} elseif (false !== stripos($response, $site_url)) { } elseif (false !== stripos($response, $site_url)) {
preg_match_all("/<b class=\"b-serp-item__number\">(.*)<\/a>/smUi", $response, $store); preg_match_all("/<a class=\"link link_theme_normal organic__url link_cropped_no i-bem\"(.*)class=/smUi", $response, $store);
foreach ($store[1] as $k => $v) { foreach ($store[1] as $k => $v) {
$start_s = stripos($v, $site_url); $start_s = stripos($v, $site_url);
if (false !== $start_s) { if (false !== $start_s) {
@ -449,217 +511,33 @@ class Keyworlds extends CI_Controller {
} else { } else {
$rank['rank'] = -1; $rank['rank'] = -1;
} }
return $rank; // 移动端
} $response_m = $this->curl($url,"m",$refer);
if ($response_m) {
//计算关键词在google搜索引擎的排名 if (false !== stripos($response_m, '<h1>302 Found</h1>') ||
public function get_search_content_by_yahoo($keyworlds, $site_url, $page = 1, $sitecode = false) { false !== stripos($response, 'капчей') || false !== stripos($response, 'captcha')) {
$rank['rank'] = 0; $rank['rank_m'] = -2;
$rank['source'] = $site_url; } elseif (false !== stripos($response_m, $site_url)) {
$site_url_array = explode('//', $site_url); preg_match_all('/<a class="link link_theme_normal organic__url i-bem"(.*)class=/smUi', $response_m, $store_m);
$site_url = $site_url_array[1]; foreach ($store_m[1] as $km => $vm) {
$rank['engines'] = 'yahoo'; $ismatch_m = stripos($vm, $site_url);
if ($page > 3) { if (false !== $ismatch_m) {
$rank['rank'] = 151; //计算排名
return $rank; $rank['rank_m'] = $startrank + $km + 1;
} //抓取关键词来源
$end_m = strpos($vm, '"', $ismatch_m);
//拼接关键词 $source_m = substr($vm, $ismatch_m, $end_m);
/* $key_array = explode(' ',$keyworlds); $source_m = explode('"', $source_m);
$search_worlds=''; $rank['source_m'] = $source_m[0];
foreach ($key_array as $v) { break;
$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 { } else {
$rank['rank'] = -1; $rank['rank_m'] = -1;
} } // ---- 移动端 end ----
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:从接口提取数据时的起始条数 return $rank;
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文件导入关键词 //从cvs文件导入关键词
@ -669,8 +547,8 @@ class Keyworlds extends CI_Controller {
if (($handle = fopen($filename, "r")) !== false) { if (($handle = fopen($filename, "r")) !== false) {
while (($dataSrc = fgetcsv($handle)) !== false) { while (($dataSrc = fgetcsv($handle)) !== false) {
$num = count($dataSrc); $num = count($dataSrc);
for ($c = 0; $c < $num; $c++) {//列 column for ($c = 0; $c < $num; $c++) {//列 column
if ($row === 1) {//第一行作为字段 if ($row === 1) {//第一行作为字段
$dataName[] = $dataSrc[$c]; //字段名称 $dataName[] = $dataSrc[$c]; //字段名称
} else { } else {
foreach ($dataName as $k => $v) { foreach ($dataName as $k => $v) {
@ -864,4 +742,45 @@ class Keyworlds extends CI_Controller {
echo $html; echo $html;
} }
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_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_SSLVERSION, 1); // 2,3
// curl_setopt($ch, CURLOPT_SSLVERSION, 'CURL_SSLVERSION_SSLv3');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
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', "search engines curl error code: ".curl_error($ch).";");
} else {
$httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (200 !== $httpStatusCode) {
log_message('error', "search engines Request html Status Code: ".$httpStatusCode.";");
}
}
curl_close($ch);
return $reponse;
}
} }

@ -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 ( $sql = "INSERT INTO infoKeywordsrank (
kwr_kw_id, kwr_kw_id,
kwr_rank, kwr_rank,
kwr_mobile_rank,
kwr_source, kwr_source,
kwr_mobile_source,
kwr_datetime, kwr_datetime,
kwr_engines kwr_engines
) VALUES (?,?,?,?,?)"; ) VALUES (?,?,?,?,?,?,?)";
$query = $this->HT->query($sql, array($kwr_kw_id,$kwr_rank,$kwr_source,time(),$kwr_engines)); $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'); 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_sql = "UPDATE infoKeywordsrank
SET kwr_datetime = ?, SET kwr_datetime = ?,
kwr_rank=?, kwr_rank=?,
kwr_source=? kwr_mobile_rank=?,
kwr_source=?,
kwr_mobile_source=?
WHERE kwr_id = ?"; 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; return $result;
} }
@ -43,10 +47,12 @@ class infoKeywordsrank_model extends CI_Model
}else{ }else{
$topmap= "TOP $top"; $topmap= "TOP $top";
} }
$sql = "SELECT $topmap kwr_id, $sql = "SELECT $topmap kwr_id,
kwr_kw_id, kwr_kw_id,
kwr_rank, kwr_rank,
kwr_mobile_rank,
kwr_source, kwr_source,
kwr_mobile_source,
kwr_engines, kwr_engines,
kwr_datetime, kwr_datetime,
kw_keyworlds kw_keyworlds
@ -71,4 +77,4 @@ class infoKeywordsrank_model extends CI_Model
$query = $this->HT->query($sql, array($kwr_id)); $query = $this->HT->query($sql, array($kwr_id));
return $query; return $query;
} }
} }

@ -31,7 +31,7 @@
<?php echo isset($rank_change_html)?$rank_change_html:''; ?> <?php echo isset($rank_change_html)?$rank_change_html:''; ?>
</div> </div>
<div id="rankinfo"> <div id="rankinfo">
<form class="span3" style="position: absolute;top: 165px;z-index: 100;right: 50px;" method="post" action="<?php echo site_url("Keyworlds/index/$current_keyworld->kw_is_id"); ?>"> <form class="span3" style="position: absolute;top: 165px;z-index: 100;right: 50px;" method="post" action="<?php echo site_url("Keyworlds/index/$current_keyworld->kw_is_id"); ?>">
<div class="input-append"> <div class="input-append">
<button class="btn pull-right" type="submit">查询</button> <button class="btn pull-right" type="submit">查询</button>
@ -48,53 +48,28 @@
<tr> <tr>
<th>#</th> <th>#</th>
<th>时间</th> <th>时间</th>
<th>来源</th> <th>来源(PC/Mobile)</th>
<th>排名</th> <th>排名(PC/Mobile)</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<?php foreach ($rank_info as $k=>$rank) { ?> <?php $ki=0; foreach ($rank_info as $k=>$rank) { ?>
<tr> <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><?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>
<td><?php echo $rank->kwr_rank; ?></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> </tr>
<?php if($k+1==7) break; ?> <?php if($k+1==7) break; ?>
<?php } ?> <?php } ?>
</tbody> </tbody>
</table><br><br> </table><br><br>
<?php } ?> <?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> </div>
</div> </div>
@ -252,12 +227,12 @@
} }
//选择节点 //选择节点
var node = zTree.getNodeByParam("id",<?php echo $current_keyworld->kw_is_id; ?>); var node = zTree.getNodeByParam("id",<?php echo $current_keyworld->kw_is_id; ?>);
zTree.selectNode(node); zTree.selectNode(node);
$("#startdate,#enddate").datepicker({ $("#startdate,#enddate").datepicker({
showButtonPanel: true, showButtonPanel: true,
dateFormat:'yy-mm-dd' dateFormat:'yy-mm-dd'
}); });
}); });
//删除结构和信息 //删除结构和信息
@ -353,7 +328,7 @@
categories: <?php echo $data_time; ?>, categories: <?php echo $data_time; ?>,
labels: {rotation: -45,align: 'right'} labels: {rotation: -45,align: 'right'}
}, },
yAxis: {title: {text: '关键词排名'}}, yAxis: {title: {text: '关键词排名'},reversed:true,allowDecimals:false},
tooltip: { tooltip: {
enabled: true, enabled: true,
formatter: function() { formatter: function() {
@ -389,4 +364,4 @@
<?php } ?> <?php } ?>
}); });
</script> </script>

Loading…
Cancel
Save