load->library('simple_html_dom_lib'); } public function index() { $this->load->view('welcome'); } public function optimize() { $htmlsource = $this->input->post('htmlsource'); $websitehost = $this->input->post('websitehost'); $template_name = $this->input->post('template_name'); $create_amp = $this->input->post('create_amp'); $debug = $this->input->post('debug'); if (empty($htmlsource) || empty($websitehost)) { $this->output->set_status_header(500); echo 'error:htmlsource or websitehost is empty!'; log_message('error', "htmlsource or websitehost is empty! " . $websitehost); return false; } //域名后面不能有/ if (substr($websitehost, -1, 1) == '/') { $websitehost = substr($websitehost, 0, -1); } $html_object = str_get_html($htmlsource); if (!empty($html_object)) { //提取和下载所有CSS样式,包括链接文件和页面样式 $link_css_array = array(); $css_content = ''; foreach ($html_object->find('link') as $link_css) { if ($link_css->rel == 'stylesheet' && !empty($link_css->href)) { $link_css_array[] = $link_css->href; $link_css->outertext = ''; //删除链接 } } //print_r($link_css_array); foreach ($link_css_array as $item) { $get_http_temp = GET_HTTP($this->format_url($item, $websitehost)); if ($get_http_temp == false) { $this->output->set_status_header(404); echo 'CSS文件下载错误'; log_message('error', "optimize CSS文件下载错误! " . $item); return FALSE; } $css_content .= $get_http_temp; } foreach ($html_object->find('style') as $style_css) { if ($style_css->type == "text/css") { $css_content .= $style_css->innertext; } } // echo $css_content; // echo $html_object;die(); // //提取和下载所有JS脚本,包括链接文件和页面脚本 $link_js_array = array(); $js_inline_content = ''; $js_jquery_content = ''; foreach ($html_object->find('script') as $link_script) { if (!empty($link_script->src)) { if (empty($link_script->defer) && empty($link_script->async)) { //没有defer标记的才处理,否则让js延后加载 $link_js_array[] = $link_script->src; $link_script->outertext = ''; //删除链接,移动到页底 } } else { //网页内的js不需要提取 //$js_content.= $link_script->innertext;//js的内容 // $js_content.= $link_script;//js的内容,包含'; $js_content = $js_scr_content . $js_jquery_content; //延迟加载js,需要把返回的js代码保存到一个文件中,然后替换占位符,以便加载js文件 $lastload_js .= ''; $lastload_js .= $js_inline_content; $html_object = str_replace('', $lastload_js . '', $html_object); } //只返回AMP代码 if (!empty($create_amp)) { $amp = $this->create_amp($html_object, $template_name, $websitehost,$debug); echo json_encode(array('result' => 'ok', 'data' => array('amp' => $amp))); return; } echo json_encode(array('result' => 'ok', 'data' => array('html_object' => $html_object, 'js_content' => $js_content))); } //格式化url,保证请求的URL有域名,//更换为对应的域名路径 function format_url($url, $host = '') { if (substr($url, 0, 8) == 'https://' || substr($url, 0, 7) == 'http://') { $url = str_replace('http://', 'https://', $url);//http要改为https return urldecode($url); } if (substr($url, 0, 2) == '//') { //https或http return urldecode('https://'.substr($url, 2, strlen($url)-2)); //只替换开头的//,部分url不规范,有//在url中间 } return urldecode($host . $url); } //生成AMP版本 用移动端版本生成 /* 1.加载站点AMP模板,模板只有导航、头部、底部 2.删除全部js引用和内链代码 3.删除全部css引用,内联css放入