From 8f859e62757b37d681bbec490af5a4810d9a1ae3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=B9=E8=AF=9A=E8=AF=9A?= Date: Tue, 28 Feb 2017 14:49:25 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3https=E7=AB=99=E7=82=B9?= =?UTF-8?q?=E4=B8=8D=E8=83=BD=E9=9D=99=E6=80=81=E5=8C=96=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E7=9A=84=E9=94=99=E8=AF=AF=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/controllers/information.php | 8 ++--- application/helpers/info_helper.php | 30 ++++++++++++++++++- ...4f500ae61d9e44b931d9f72fb14aaf95517b7e.php | 2 ++ 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/application/controllers/information.php b/application/controllers/information.php index 9aacf4ba..dacbbc81 100644 --- a/application/controllers/information.php +++ b/application/controllers/information.php @@ -503,10 +503,10 @@ class Information extends CI_Controller { } //关闭错误提示,防止file_get_contents请求特殊字符的时候会报错或者404、500,特殊字符需要用urlencode编码第一个/后面的url - error_reporting(NULL); - ini_set('display_errors', 'Off'); + //error_reporting(NULL); + //ini_set('display_errors', 'Off'); - $content = file_get_contents($url); + $content = GET_HTTP($url); if ($content === false) { $data[] = array('name' => 'no', 'value' => sprintf($this->lang->line('update_cache_failed'), $url)); } else { @@ -515,7 +515,7 @@ class Information extends CI_Controller { //CHT网站还需要更新前端服务器缓存 if ($this->config->item('site_code') == 'cht') { $url = 'http://www.chinahighlights.com/api/community/updatecache.asp?gurl=http://www.chinahighlights.com' . $original_url . '&other=1&site=info'; - $content = file_get_contents($url); + $content = GET_HTTP($url); if ($content === false) { $data[] = array('name' => 'no', 'value' => sprintf($this->lang->line('update_vps_failed'), $url)); echo json_encode($data); diff --git a/application/helpers/info_helper.php b/application/helpers/info_helper.php index 0cf3f981..fc53eb95 100644 --- a/application/helpers/info_helper.php +++ b/application/helpers/info_helper.php @@ -287,4 +287,32 @@ function get_content_by_url($url) { return false; } return $response; -} \ No newline at end of file +} + + + + function GET_HTTP($url, $data = '', $method = 'GET') { + $curl = curl_init(); // 启动一个CURL会话 + curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址 + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查 + curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在 + curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器 + curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转 + curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer + if ($method == 'POST' && !empty($data)) { + curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求 + curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包 + } + curl_setopt($curl, CURLOPT_TIMEOUT, 45); // 设置超时限制防止死循环 + curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容 + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回 + $tmpInfo = curl_exec($curl); // 执行操作 + $errno = curl_errno($curl); + if ($errno !== 0) { + return false; + $error_message = $errno . ' ' . curl_error($curl); //记录错误日志 + log_message('error', "train/get_http curl {$error_message}"); + } + curl_close($curl); //关闭CURL会话 + return $tmpInfo; //返回数据 + } \ No newline at end of file diff --git a/please_dont_delete_this_file_4a4f500ae61d9e44b931d9f72fb14aaf95517b7e.php b/please_dont_delete_this_file_4a4f500ae61d9e44b931d9f72fb14aaf95517b7e.php index 5c830f2f..d47f5b2d 100644 --- a/please_dont_delete_this_file_4a4f500ae61d9e44b931d9f72fb14aaf95517b7e.php +++ b/please_dont_delete_this_file_4a4f500ae61d9e44b931d9f72fb14aaf95517b7e.php @@ -1,4 +1,6 @@