手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

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

[日期:2018-09-03] 来源:Linux社区  作者:ning235 [字体: ]

问题:
线上运行的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;
    }
}

Linux公社的RSS地址:https://www.linuxidc.com/rssFeed.aspx

本文永久更新链接地址https://www.linuxidc.com/Linux/2018-09/153852.htm

linux
相关资讯       安装curl扩展 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款