天空小小岛技术网站

标题: file_get_contents(): Unable to find the wrapper "https"解决方案 [打印本页]

作者: 一粒米    时间: 2013-12-9 21:36
标题: file_get_contents(): Unable to find the wrapper "https"解决方案
问题表现
file_get_contents 读取一个正常的 https 的url时,返回 false

打开报错
错误提示
  1. PHP Warning:  file_get_contents(https://graph.qq.com/): failed to open stream: No such file or directory in Command line code on line 1
复制代码


原因
php 所在的环境没有打开 openssl 的支持

解决方法
一、在 windows 下
只要打开相应的 dll 即可,就是找到 php.ini

  1. ;extension=php_openssl.dll
复制代码

前的分号去掉,重启 web 服务。
二、在 linux 下
重新编译 php,加入
  1. '--with-openssl'
复制代码
参数
或者单独编译 openssl,然后在 php.ini 里添加
  1. extension = openssl.so
复制代码
加载该模块

========
如果没有条件修改环境,并且服务器支持 curl,可以使用以下函数替换 file_get_contents
  1. function _file_get_contents($url) {

  2.         if (!function_exists('curl_init')) {  
  3.                 return false;
  4.         }

  5.         $timeout = 10;
  6.         $ch = curl_init();
  7.         curl_setopt($ch, CURLOPT_URL, $url);
  8.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  9.         curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  10.         $contents = curl_exec($ch);
  11.         curl_close($ch);

  12.         return $contents;
  13. }
复制代码





欢迎光临 天空小小岛技术网站 (http://tkxxd.net/) Powered by Discuz! X3.1