解决file_get_contents无法请求https连接的方法
解决方案有3:
1.windows下的PHP,只需要到php.ini中把extension=php_openssl.dll前面的;删掉,重启服务就可以了。
2.linux下的PHP,就必须安装openssl模块,安装好了以后就可以访问了。
3.如果服务器你不能修改配置的话,那么就使用curl函数来替代file_get_contents函数,当然不是简单的替换啊。还有相应的参数配置才能正常使用curl函数。
对curl函数封装如下:
复制代码 代码如下:
function http_request($url,$timeout=30,$header=array()){
if (!function_exists('curl_init')) {
throw new Exception('server not install curl');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
if (!emptyempty($header)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
}
$data = curl_exec($ch);
list($header, $data) = explode("\r\n\r\n", $data);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code == 301 || $http_code == 302) {
$matches = array();
preg_match('/Location:(.*?)\n/', $header, $matches);
$url = trim(array_pop($matches));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
$data = curl_exec($ch);
}
if ($data == false) {
curl_close($ch);
}
@curl_close($ch);
return $data;
}
上一篇: 附件名前加网站名
下一篇: Oracle体系结构及备份(五)sga
推荐阅读
-
远程连接局域网内的sql server 无法连接 错误与解决方法
-
无线连接路由器时提示“windows无法连接到选定网络,网络可能不在区域中”的解决方法(
-
serv-u与屏保导致serv-u服务器经常停止无法连接的解决方法
-
WebService出现"因 URL 意外地以 结束,请求格式无法识别"的解决方法
-
AngularJS出现$http异步后台无法获取请求参数问题的解决方法
-
电脑显示系统资源不足无法完成请求的服务(系统资源不足解决方法)
-
thinkphp中连接oracle时封装方法无法用的解决办法
-
小度wifi无法连接网络以及小度wifi创建网络失败的解决方法
-
Win7系统设置家庭组提示此计算机无法连接到家庭组的解决方法图文教程
-
苹果浏览器safari无法建立安全连接的解决方法