问题: 线上运行的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.0rn"; $out .= "Accept: */*rn"; //$out .= "Referer: $boardurlrn"; $out .= "Accept-Language: zh-cnrn"; $out .= "Content-Type: application/x-www-form-urlencodedrn"; $out .= "User-Agent: ".$_SEVER['HTTP_USER_AGENT']."rn"; $out .= "Host: $hostrn"; $out .= 'Content-Length: '.strlen($post)."rn"; $out .= "Connection: Closern"; $out .= "Cache-Control: no-cachern"; $out .= "Cookie: $cookiernrn"; $out .= $post; } else { $out = "GET $path HTTP/1.0rn"; $out .= "Accept: */*rn"; //$out .= "Referer: $boardurlrn"; $out .= "Accept-Language: zh-cnrn"; $out .= "User-Agent: ".$_SEVER['HTTP_USER_AGENT']."rn"; $out .= "Host: $hostrn"; $out .= "Connection: Closern"; $out .= "Cookie: $cookiernrn"; } $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 == "rn" || $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; } }