file_get_contents()获取https出现这个错误Unable to find the wrapper “https”

 更新时间:2016年11月25日 15:30  点击:3109
下面我们来看一篇关于file_get_contents()获取https出现这个错误Unable to find the wrapper “https”问题的解决办法.

file_get_contents()获取https出现这个错误Unable to find the wrapper “https” – did

解决办法一,如果你是用的服务器,可以参考这个办法,修改php配置文件(win主机),来支持https

在php.ini中找到并修改

    extension=php_openssl.dll
    allow_url_include = On

重启服务就可以了,如果你的是linux服务器,linux下的PHP,就必须安装openssl模块,安装好了以后就可以访了。

解决办法二,如果你用的不是服务器,你用的主机,你没法更改php的配置,你可以通过使用curl函数来替代file_get_contents函数,当然你的主机必须支持curl函数。

<?php

function getSslPage($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
echo getSslPage($_GET['url']);
?>

检查主机是否支持php的file_get_contents函数

讲一下代码存储为php文件,在空间运行,即可得出结果。

<?php
echo ‘Curl: ‘, function_exists(‘curl_version’) ? ‘Enabled’ : ‘Disabled’ . ‘<br />
file_get_contents: ‘, file_get_contents(__FILE__) ? ‘Enabled’ : ‘Disabled’;
?>
 

下面我们来看一篇关于file_get_contents url 含有特殊字符获取失败问题处理的问题,希望文章能够帮助到各位朋友.

file_get_contents 在文件名包含特殊符号的时候回遇到处理失败的情况,解决办法是使用urlencode函数处理下文件名。

<?php

$name='包含特殊符的文件名';
$name=urlencode($name);

$url = "http://phpff.com/{$name}.jpg";
$con = file_get_contents($url);
?>

知识扩充

file_get_contents()模拟referer,cookie, 使用proxy等等

参考代码

ini_set('default_socket_timeout',120);
ini_set('user_agent','MSIE 6.0;');
$context=array('http' => array ('header'=> 'Referer: http://www.baidu.com/', ),);
$xcontext = stream_context_create($context);
echo $str=file_get_contents('http://www.111cn.net/',FALSE,$xcontext);

下面是file_get_contents和curl两个函数同样功能的不同写法

file_get_contents函数的使用示例:

< ?php

$file_contents = file_get_contents(‘http://www.zoneself.org/’);

echo $file_contents;

?>
换成curl函数的使用示例:

< ?php

$ch = curl_init();

$timeout = 5;

curl_setopt ($ch, CURLOPT_URL, ‘http://www.ccvita.com’);

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

$file_contents = curl_exec($ch);

curl_close($ch);

echo $file_contents;

?>
利用function_exists函数来判断php是否支持一个函数可以轻松写出下面函数:

< ?php

function vita_get_url_content($url) {

if(function_exists(‘file_get_contents’)) {

$file_contents = file_get_contents($url);

} else {

$ch = curl_init();

$timeout = 5;

curl_setopt ($ch, CURLOPT_URL, $url);

curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

$file_contents = curl_exec($ch);

curl_close($ch);

}

return $file_contents;

}

?>

 
          其实上面的这个函数还有待商榷,如果你的主机服务商把file_get_contents和curl都关闭了,上面的函数就会出现错误。那最后的办法就得换空间或者在支持的空间上写接口,在不支持的空间上进行调用了。

在php中替换字符串我们要用到repalce但如果要在指定行号替换我们可以对策上面的一篇php根据行号替换字符串的例子,具体如下.

php要实现根据行号替换内容,使用str_replace这样的函数是无法实现的,可以先根据换行符把文本分割成数字,然后便利数组实现替换功能。

<?php
$url='z.txt';
$content=file_get_contents($url);
$contentArr=explode("\n",$content);
$ikey=0;
foreach($contentArr as &$val){
    if(strpos($val, '<!–nextpage–>') !== false){
        $val=$ikey.' '.$val;
        $ikey++;
    }
}
   
$content=implode("\n",$contentArr);
?>


补充

1. preg_replace()

$msg = preg_replace("/<style>.+<\/style>/is", "", $msg); -----删除<style></style>和中间的部分
$msg = preg_replace("/<[^>]+>/", "", $msg); -----是删除<>和中间的内容

i (PCRE_CASELESS)
如果设定此修正符,模式中的字符将同时匹配大小写字母。
s (PCRE_DOTALL)
如果设定了此修正符,模式中的圆点元字符(.)匹配所有的字符,包括换行符。没有此设定的话,则不包括换行符。这和 Perl 的 /s 修正符是等效的。排除字符类例如 [^a] 总是匹配换行符的,无论是否设定了此修正符。


2. ereg()与eregi()

注: preg_match() 函数通常是比 ereg() 更快的替代方案

eregi("<body([^>]+)>(.+)</body>",$data,$b)----察看$data中是否有body标签。如果有,把参数赋值$b[0],中间部分赋值$b[1]。

bool ereg ( string pattern, string string [, array regs] )

int eregi ( string pattern, string string, array [regs] )

eregi()和 ereg() 类似,用法也相同。不同之处在于ereg()有区分大小写,eregi()与大小写无关。

下面我们来看一篇关于php并发控制中的独占锁的例子,希望这篇文章能够给各位同学带来帮助让各位更好的了解到php并发控制中的独占锁吧.

1.并发问题

并发大家都知道是什么情况,这里说的是并发多个请求抢占同一个资源,直接上实例吧

请求:index.php?mod=a&action=b&taskid=6

处理:

$key = "a_b::".$uid.'_'.$taskid;
$v = $redis->get($key);
if($v == 1){
    $redis->setex($key,10,1);
    //处理逻辑省略
}
2.分析

逻辑看来还可以,结果发现数据库中写入了两个同样的请求结果,我看了记录的时间戳,天!居然是同一秒.
我用microtime(true) log一下两个请求的时间差居然相差了0.0001s,就是说$redis->setex($key,10,1);还没执行成功 第二个请求已经get到跟第一个请求一样的结果。这不就是传说中的并发抢占资源。这中情况 听过很多,在开发过程中也没刻意去模拟实验过。
3.解决

方案1:第一反应就是要给处理过程加事务(数据库是mysql innoDB),加事务的结果就是 第一个请求成功了 第二个请求会执行到后面捡查发现重了会回滚。其实mysql事务在保证数据一致性上是很ok的,但是通过回滚来保证唯一资源独占代价太大,做过mysql事务测试测同学都知道,事务中的insert是已经插进去了,回滚之后才删掉的。
方案2:还有一个选择就是php中的文件独占锁,那就是说这情况下我要新建 用户数 * 任务数的文件来实现每个请求资源的独占,如果独占资源较少的话可选的解决办法:
/**
     * 加锁
     */
    public function file_lock($filename){
        $fp_key = sha1($filename);
        $this->fps[$fp_key] = fopen($filename, 'w+');
        if($this->fps[$fp_key]){
            return flock($this->fps[$fp_key], LOCK_EX|LOCK_NB);
        }
        return false;
    }
    /**
     * 解锁
     */
    public function file_unlock($filename){
        $fp_key = sha1($filename);
        if($this->fps[$fp_key] ){
            flock($this->fps[$fp_key] , LOCK_UN);
            fclose($this->fps[$fp_key] );
        }
    }
方案3:发现$redis->setnx()可以提供原子操作的状态:相同的key执行setnx之后没过期或者没del,再执行会返回false。这就让两个以上的并发请求得到控制必须成功获取锁才能继续。
/**
     *  加锁
     */
    public function task_lock($taskid){
            $expire = 2;
             $lock_key ='task_get_reward_'.$this->uid.'_'.$taskid;
            $lock = $this->redis->setNX($lock_key , time());//设当前时间
            if($lock){
                $this->redis->expire($lock_key,  $expire); //如果没执行完 2s锁失效
            }
            if(!$lock){//如果获取锁失败 检查时间
                $time = $this->redis->get($lock_key);
                if(time() - $time  >=  $expire){//添加时间戳判断为了避免expire执行失败导致死锁 当然可以用redis自带的事务来保证
                    $this->redis->rm($lock_key);
                }
                $lock =  $this->redis->setNX($lock_key , time());
                if($lock){
                    $this->redis->expire($lock_key,  $expire); //如果没执行完 2s锁失效
                }
            }
            return $lock;
        }
        /**
         *  解锁
         */
        public function task_unlock($taskid){
            $this->set_redis();
            $lock_key = 'task_get_reward_'.$this->uid.'_'.$taskid;
            $this->redis->rm($lock_key);
        }
说明下setNX 和expire 这两个操作其实可以用redis事务来保证一致性

 

[!--infotagslink--]

相关文章

  • php 中file_get_contents超时问题的解决方法

    file_get_contents超时我知道最多的原因就是你机器访问远程机器过慢,导致php脚本超时了,但也有其它很多原因,下面我来总结file_get_contents超时问题的解决方法总结。...2016-11-25
  • php file_get_contents 设置代理抓取页面示例

    file_get_contents函数在php中可以直接打开本地文件也可以直接抓取远程服务器文件,如果简单的采集我们可以使用file_get_contents直接来操作,如果有防采集我们可能需要...2016-11-25
  • MyBatisPlus-QueryWrapper多条件查询及修改方式

    这篇文章主要介绍了MyBatisPlus-QueryWrapper多条件查询及修改方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2022-06-27
  • php报错file_get_contents(): php_network_getaddresses问题

    本文章来为各位介绍一篇关于file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known...错误解决办法。 昨天,服务器的DN...2016-11-25
  • PHP file_get_contents设置超时处理方法

    file_get_contents的超时处理话说,从PHP5开始,file_get_content已经支持context了(手册上写着:5.0.0 Added the context support. ),也就是说,从5.0开始,file_get_contents其实也可以POST数据。今天说的这篇是讲超时的,确实在...2013-10-04
  • file_get_contents()获取https出现这个错误Unable to find the wrapper “https”

    下面我们来看一篇关于file_get_contents()获取https出现这个错误Unable to find the wrapper “https”问题的解决办法. file_get_contents()获取https出现这个错...2016-11-25
  • file_put_contents并发性问题解决方案整理

    在使用file_put_contents时会碰到并发性问题了,对于这个问题我们有多种解决方案了,其实锁是小编比较喜欢的解决办法了,当然也有其它办法,具体如下。 解决 办法一,fil...2016-11-25
  • php提示Warning: file_get_contents(): couldn’t resolve

    在使用file_get_contents函数获取远程文件时提示Warning: file_get_contents(): couldn’t resolve错误了,这个我们可以看出是dns的问题,解决办法也简单。 今天在...2016-11-25
  • file_get_contents不能获取带端口的网址

    本文章来给各位同学介绍file_get_contents不能获取带端口的网址解决办法,有需要了解的同学可参考。 先们来了解file_get_contents() 函数,官方介绍说它是把整个...2016-11-25
  • php中file_get_contents和curl_get_contents介绍

    php中file_get_contents和curl_get_contents介绍 有需要的朋友可参考一下。 分享一个实际在用的函数: file_get_contents() 函数是用于将文件的内容读入到一个字符...2016-11-25
  • centos下file_put_contents()无法写入文件的原因及解决方法

    下面小编就为大家带来一篇centos下file_put_contents()无法写入文件的原因及解决方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2017-04-03
  • php file_put_contents 生成文件

    <html> <head> <title>test</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="text/javascript" src="http://ajax...2016-11-25
  • php curl file_get_contents post方式获取数据

    下面我们在这里来为各位介绍一篇关于php curl file_get_contents post方式获取数据例子,希望文章能够帮助到各位朋友. curl post,file_get_contents post,curl fi...2016-11-25
  • PHP中file_put_contents写入文件的优点

    file_put_contents写入文件在我看到的phper中很少用到了,但小编以前做flash接受数据时就用到了file_put_contents函数了,下面我们来看看file_put_contents写入文件的优...2016-11-25
  • php file_get_contents 函数

    php file_get_contents 函数 file_get_contents ( PHP 4中“ = 4.3.0 , PHP 5中) file_get_contents -读取整个文件转换成字符串 描述 字符串file...2016-11-25
  • php file_get_contents获取百度热词代码

    这是一段很简单的程序利用了php的file_get_contents函数来采集百度的数据,然后通过simplexml_load_String把它数据解析出来,这样数据就保存到了一个数组,我们就可以方便...2016-11-25
  • file file_get_contents HTTP request failed

    /* 我有一个问题,要求从php教程代码的url。我需要调用一个服务,使用从我的php代码的查询字符串。如果我的浏览器中键入一个网址,它工作还算可以,但如果我使用文件获取,内容...2016-11-25
  • php file_put_contents 函数

    php file_put_contents 函数 file_put_contents ( PHP 5中) file_put_contents -写一个字符串到一个文件 描述 国际file_put_contents (字符串$文件名,...2016-11-25
  • file_get_contents实现数据Post数据方法

    file_get_contents() 函数把整个文件读入一个字符串中。 和 file() 一样,不同的是 file_get_contents() 把文件读入一个字符串。 file_get_contents() 函数是用于将文件...2016-11-25
  • php中file_get_contents函数高级用法

    file_get_contents函数我们通常是拿来对文件操作了,下面一起来看看file_get_contents函数的高级使用方法吧. 首先解决file_get_contents的超时问题,在超时返回错误...2016-11-25