阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

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

102次阅读
没有评论

共计 4491 个字符,预计需要花费 12 分钟才能阅读完成。

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

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计4491字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中