前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux下PHP5.2安装curl扩展支持https

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

作者头像
星哥玩云
发布2022-07-19 15:15:22
1.6K0
发布2022-07-19 15:15:22
举报
文章被收录于专栏:开源部署

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
命令行工具
腾讯云命令行工具 TCCLI 是管理腾讯云资源的统一工具。使用腾讯云命令行工具,您可以快速调用腾讯云 API 来管理您的腾讯云资源。此外,您还可以基于腾讯云的命令行工具来做自动化和脚本处理,以更多样的方式进行组合和重用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档