`
tw5566
  • 浏览: 448768 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

file_get_contents无法请求https连接的解决方法

    博客分类:
  • php
阅读更多

PHP.ini默认配置下,用file_get_contents读取https的链接,就会如下错误:

Warning: fopen() [function.fopen]: Unable to find the wrapper "https" - did you forget to enable it when you configured PHP?

解决方案有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 (!empty($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;
}

 

 

分享到:
评论

相关推荐

    解决file_get_contents无法请求https连接的方法

    PHP.ini默认配置下,用file_get_contents读取https的链接,就会报如下错误,本文给出解决方法

    PHP使用file_get_contents发送http请求功能简单示例

    主要介绍了PHP使用file_get_contents发送http请求功能,结合实例形式分析了file_get_contents结合stream_context_create实现的发送post请求数据相关原理与操作技巧,需要的朋友可以参考下

    php中file_get_contents与curl性能比较分析

    本文实例讲述了php中file_get_contents与curl性能比较分析。...1.fopen /file_get_contents 每次请求都会重新做DNS查询,并不对 DNS信息进行缓存。但是CURL会自动对DNS信息进行缓存。对同一域名下的网页或

    PHP中使用file_get_contents抓取网页中文乱码问题解决方法

    本文实例讲述了PHP中使用file_get_contents抓取网页中文乱码问题解决方法。分享给大家供大家参考。具体方法如下: file_get_contents函数本来就是一个非常优秀的php自带本地与远程文件操作函数,它可以让我们不花吹挥...

    php下载文件file_get_contents php input

    php下载文件file_get_contents php input php input是获得raw原始数据流,可以访问请求的原始数据的只读流 所以比$_POST更低层,能作的事情也更多

    PHP curl 或 file_get_contents 获取需要授权页面的方法

    今天因工作需要,需要用 curl / file_get_contents 获取需要授权(Authorization)的页面内容,解决后写了这篇文章分享给大家。 PHP curl 扩展,能够在服务器端发起POST/GET请求,访问页面,并能获取页面的返回数据。 ...

    php中file_get_content 和curl以及fopen 效率分析

    1. fopen /file_get_contents 每次请求都会重新做DNS查询,并不对DNS信息进行缓存。但是CURL会自动对DNS信息进行缓存。对同一域名下的网页或者图片的请求只需要一次DNS查询。这大大减少了DNS查询的次数。所以CURL的...

    file-get-contents.js:Node.js版本的file_get_contents PHP函数

    const fileGetContents = require ( 'file-get-contents' ) ; // A File request try { let data = await fileGetContents ( '/tmp/foo/bar' ) ; console . log ( data ) ; } catch ( err ) { console . log ( '...

    解析file_get_contents模仿浏览器头(user_agent)获取数据

    什么是user agentUser Agent中文名为...这很有可能是服务器上做了设置,根据 User_agent判断是否为正常的浏览器请求,因为默认PHP的file_get_contents函数是不发送ua的。如果要采集这样的网站,我们就必须要让PHP模拟

    php file_get_contents抓取Gzip网页乱码的三种解决方法

    把抓取到的内容转下编码即可($content=iconv(“GBK”, “UTF-8//IGNORE”, $...复制代码 代码如下:请求头信息原始头信息Accept text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8Accept-Encoding gz

    动动客短信HTTP接口

    $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_...

    curl 不支持https解决方法

    curl 不支持https 请求 windows系统file_get_contents返回false远程phpstudy

    PHP请求远程地址设置超时时间的解决方法

    php请求远程地址设置超时时间,主要讲解file_get_contents、fopen、curl这三个简单常用函数设置超时时间的方法,一般情况下建议使用curl,性能最好,效率也最高。 1、file_get_contents 请求超时设置 $timeout = ...

    php发送get、post请求的6种方法简明总结

    方法1: 用file_get_contents 以get方式获取内容: <?php $url='https://www.jb51.net/'; $html = file_get_contents($url); echo $html; ?> 方法2: 用fopen打开url, 以get方式获取内容: <?php $...

    PHP抓取HTTPS内容和错误处理的方法

    因为所有的Hacker News API都是通过加密的HTTPS协议访问的,跟普通的HTTP协议不同,当使用PHP里的函数 file_get_contents() 来获取API里提供的数据时,出现错误 使用的代码是这样的: <?php $data = ...

    微信小程序获取用户openId的实现方法

    微信小程序获取用户openId的实现方法 前端: wx.login({ success: function (res) { res.code }) 获取到code后,传到后台, 然后请求微信接口 ...

    php发送http请求的常用方法分析

    1. file_get_contents();详情见:https://www.jb51.net/article/41833.htm 2. curl发送请求。 3. fsocket发送。 下面说使用curl发送。 首先环境需要配置好curl组件。 在windows中让php支持curl比较简单: 在...

    PHP stream_context_create()函数的使用示例

    ,file_get_contents()等过程的超时设置、代理服务器、请求方式、头信息设置的特殊过程。 比如说,上篇php教程中gd库实现下载网页所有图片中,第10行: 利用了stream_context_create()设置代理服务器: 复制...

Global site tag (gtag.js) - Google Analytics