zoukankan      html  css  js  c++  java
  • file_get_contents无法请求https连接的解决方法 php开启curl

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

    方法1: 

    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函数封装如下:

    1. function http_request($url,$timeout=30,$header=array()){  
    2.         if (!function_exists('curl_init')) {  
    3.             throw new Exception('server not install curl');  
    4.         }  
    5.         $ch = curl_init();  
    6.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
    7.         curl_setopt($ch, CURLOPT_HEADER, true);  
    8.         curl_setopt($ch, CURLOPT_URL, $url);  
    9.         curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);  
    10.         if (!empty($header)) {  
    11.             curl_setopt($ch, CURLOPT_HTTPHEADER, $header);  
    12.         }  
    13.         $data = curl_exec($ch);  
    14.         list($header, $data) = explode(" ", $data);  
    15.         $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);  
    16.         if ($http_code == 301 || $http_code == 302) {  
    17.             $matches = array();  
    18.             preg_match('/Location:(.*?) /', $header, $matches);  
    19.             $url = trim(array_pop($matches));  
    20.             curl_setopt($ch, CURLOPT_URL, $url);  
    21.             curl_setopt($ch, CURLOPT_HEADER, false);  
    22.             $data = curl_exec($ch);  
    23.         }  
    24.   
    25.         if ($data == false) {  
    26.             curl_close($ch);  
    27.         }  
    28.         @curl_close($ch);  
    29.         return $data;  
    30. }  

    php开启curl  

    开启php curl函数库的步骤

      1).去掉windows/php.ini 文件里;extension=php_curl.dll前面的; /*用 echo phpinfo();查看php.ini的路径*/

      2).把php5/libeay32.dll,ssleay32.dll复制到系统目录windows/下

      3).重启apache

    配置php支持curl

      curl是一个利用URL语法在命令行方式下工作的文件传输工具。它支持很多协议:FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE 以及 LDAP。curl同样支持HTTPS认证,HTTP POST方法, HTTP PUT方法, FTP上传, kerberos认证, HTTP上传, 代理服务器, cookies, 用户名/密码认证, 下载文件断点续传, 上载文件断点续传, http代理服务器管道( proxy tunneling), 甚至它还支持IPv6, socks5代理服务器, 通过http代理服务器上传文件到FTP服务器等等,功能十分强大。Windows操作系统下的网络蚂蚁,网际快车(FlashGet)的功能它都可以做到。准确的说,curl支持文件的上传和下载,所以是一个综合传输工具,但是按照传统,用户习惯称curl为下载工具。

      配置方法:

      1、拷贝PHP目录中的libeay32.dll 和 ssleay32.dll 两个文件到 system32 目录。

      2、修改php.ini:配置好 extension_dir ,去掉 extension = php_curl.dll 前面的分号。

      ---------------------------

      php下扩展php_curl.dll的安装

      ---------------------------

      已经内置有php_curl.dll,在ext目录下,此DLL用于支持SSL和zlib.

      在php.ini中找到有extension=php_curl.dll, 去掉前面的注释.

      设置extension_dir=c:phpext, 刷新PHP页面时报错, 说找不到模块php_curl.dll.

      拷贝php_curl.dll 到windowssystem32,还是同样的错.

      在网上找了一下,需要将:

      libeay32.dll, ssleay32.dll, php5ts.dll, php_curl.dll

      都拷贝到system32目录下,重启IIS即可.

  • 相关阅读:
    学习记录
    cxf client端借口类型找不到问题
    zookeeper dubbo 问题解决录
    unkow jdbc driver : http://maven.apache.org
    推荐eclipse插件Properties Editor
    WPS for Linux,系统缺失字体
    Android 联系人字母排序(仿微信)
    Android之ORMLite实现数据持久化的简单使用
    设计模式之多例模式
    设计模式之单例模式
  • 原文地址:https://www.cnblogs.com/lykbk/p/rwerwe3453453.html
Copyright © 2011-2022 走看看