当前位置: 首页 > news >正文

Linux下PHP5.2安装curl扩展支持https

问题:
线上运行的LNMP服务器,因历史原因安装的curl模块只支持http,不支持https。
类似请求或POST微信接口(小程序),都无法正常使用。

一、解决方法:
编译安装curl,重新编译php,使php的curl模块支持https。

cd /data0/software
1、下载安装curl
wget http://curl.haxx.se/download/curl-7.44.0.tar.gz
tar zxvf curl-7.44.0.tar.gz
cd curl-7.44.0
./configure --prefix=/usr/local/curl --with-gssapi --enable-tls-srp --with-libmetalink
make && make install
2、重新编译php
查找系统之前的php编译参数
/usr/local/webserver/php/bin/php -i | grep configure
./configure --prefix=/usr/local/webserver/php --with-config-file-path=/usr/local/webserver/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr/ --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap

取消原来的--with-curl
替换为:--with-curl=/usr/local/curl
cd /data0/software/php-5.2.17
#进入php安装包目录(注意php版本要和之前一样)
./configure --prefix=/usr/local/webserver/php --with-config-file-path=/usr/local/webserver/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr/ --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl=/usr/local/curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap

/usr/local/webserver/php/sbin/php-fpm stop
#不确定是否要关闭,但还是关闭再编辑吧
make ZEND_EXTRA_LIBS='-liconv'
make install
/usr/local/webserver/php/sbin/php-fpm start

/usr/local/webserver/php/bin/php -i|grep curl
##有libcurl/7.44.0 OpenSSL/1.0.1e 字样了。说明成功了。

二、还有一种方式,不变服务器环境下,使用socket方式来请求或post数据,方法如下:
function socketRequest($url, $data ="", $method ="", $cookieFile = "", $connectTimeout = 1){
$return = '';
$matches = parse_url($url);

    !isset($matches['host'])    && $matches['host']     = '';
    !isset($matches['path'])    && $matches['path']     = '';
    !isset($matches['query'])   && $matches['query']    = '';
    !isset($matches['port'])    && $matches['port']     = '';

    $host       = $matches['host'];
    $path       = $matches['path'] ? $matches['path'].($matches['query'] ? '?'.$matches['query'] : '') : '/';
    $port       = !empty($matches['port']) ? $matches['port'] : 443;

    $conf_arr   = array(
        'limit'     => 0,
        'post'      => $data,
        'cookie'    => $cookieFile,
        'ip'        => '',
        'timeout'   => $connectTimeout,
        'block'     => TRUE,
    );

    foreach ($conf_arr as $k=>$v) ${$k} = $v;
    if($post) {
        if(is_array($post))
        {
            $postBodyString = '';
            foreach ($post as $k => $v) $postBodyString .= "$k=" . urlencode($v) . "&";
            $post = rtrim($postBodyString, '&');
        }
        $out = "POST $path HTTP/1.0\r\n";
        $out .= "Accept: */*\r\n";
        //$out .= "Referer: $boardurl\r\n";
        $out .= "Accept-Language: zh-cn\r\n";
        $out .= "Content-Type: application/x-www-form-urlencoded\r\n";
        $out .= "User-Agent: ".$_SEVER['HTTP_USER_AGENT']."\r\n";
        $out .= "Host: $host\r\n";
        $out .= 'Content-Length: '.strlen($post)."\r\n";
        $out .= "Connection: Close\r\n";
        $out .= "Cache-Control: no-cache\r\n";
        $out .= "Cookie: $cookie\r\n\r\n";
        $out .= $post;
    } else {
        $out = "GET $path HTTP/1.0\r\n";
        $out .= "Accept: */*\r\n";
        //$out .= "Referer: $boardurl\r\n";
        $out .= "Accept-Language: zh-cn\r\n";
        $out .= "User-Agent: ".$_SEVER['HTTP_USER_AGENT']."\r\n";
        $out .= "Host: $host\r\n";
        $out .= "Connection: Close\r\n";
        $out .= "Cookie: $cookie\r\n\r\n";
    }
    $fp = @fsockopen('ssl://'.($ip ? $ip : $host), $port, $errno, $errstr, $timeout);
    if(!$fp) {
        return '';
    } else {
        stream_set_blocking($fp, $block);
        stream_set_timeout($fp, $timeout);
        @fwrite($fp, $out);
        $status = stream_get_meta_data($fp);
        if(!$status['timed_out']) {
            while (!feof($fp)) {
                if(($header = @fgets($fp)) && ($header == "\r\n" ||  $header == "\n")) {
                    break;
                }
            }

            $stop = false;
            while(!feof($fp) && !$stop) {
                $data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit));
                $return .= $data;
                if($limit) {
                    $limit -= strlen($data);
                    $stop = $limit <= 0;
                }
            }
        }
        @fclose($fp);
        return $return;
    }
}

转载于:https://blog.51cto.com/ning235/2114434

相关文章:

  • 分布式架构总汇【转】
  • MFS分布式文件系统部署
  • 阿里云Elasticsearch公测发布
  • Django中Model-Form验证
  • win10 常用设置 桌面出来计算机图标,固定桌面摆好的图标设置方法,电脑设备ID方法...
  • D的去世给我的震撼
  • SAP云平台运行环境Cloud Foundry和Neo的区别
  • CAP理论的例子讲解
  • 欧拉函数 + 线性求法
  • 编写高质量JavaScript代码之并发
  • Python成长之路【第三篇】函数
  • Callable和Future用法示例
  • 谁说我们IT不重要???
  • linux-NAT连接外网
  • DataWorks支持PyODPS类型任务
  • [译] React v16.8: 含有Hooks的版本
  • 【跃迁之路】【641天】程序员高效学习方法论探索系列(实验阶段398-2018.11.14)...
  • Angular 2 DI - IoC DI - 1
  • create-react-app做的留言板
  • CSS实用技巧
  • DOM的那些事
  • ES6--对象的扩展
  • IP路由与转发
  • JavaScript设计模式之工厂模式
  • Promise初体验
  • Spring思维导图,让Spring不再难懂(mvc篇)
  • swift基础之_对象 实例方法 对象方法。
  • 阿里云前端周刊 - 第 26 期
  • 盘点那些不知名却常用的 Git 操作
  • 少走弯路,给Java 1~5 年程序员的建议
  • 通过几道题目学习二叉搜索树
  • 责任链模式的两种实现
  • 最近的计划
  • ## 临床数据 两两比较 加显著性boxplot加显著性
  • #{}和${}的区别是什么 -- java面试
  • #1014 : Trie树
  • #ubuntu# #git# repository git config --global --add safe.directory
  • (1/2)敏捷实践指南 Agile Practice Guide ([美] Project Management institute 著)
  • (第一天)包装对象、作用域、创建对象
  • (二)windows配置JDK环境
  • (分布式缓存)Redis持久化
  • (附源码)ssm经济信息门户网站 毕业设计 141634
  • (紀錄)[ASP.NET MVC][jQuery]-2 純手工打造屬於自己的 jQuery GridView (含完整程式碼下載)...
  • (转)一些感悟
  • (轉貼) 蒼井そら挑戰筋肉擂台 (Misc)
  • .net 4.0 A potentially dangerous Request.Form value was detected from the client 的解决方案
  • .NET Framework .NET Core与 .NET 的区别
  • .NET/ASP.NETMVC 深入剖析 Model元数据、HtmlHelper、自定义模板、模板的装饰者模式(二)...
  • /usr/local/nginx/logs/nginx.pid failed (2: No such file or directory)
  • ?.的用法
  • @Conditional注解详解
  • @GlobalLock注解作用与原理解析
  • [C++] cout、wcout无法正常输出中文字符问题的深入调查(1):各种编译器测试
  • [C++]拼图游戏
  • [Codeforces1137D]Cooperative Game