Problem:
On-line lamp server, the default Yum installed Curl module only supports HTTP and does not support HTTPS.
Workaround:
Compile and install curl, recompile PHP, and enable PHP's Curl module to support HTTPS.
Specific steps:
1, Download Curl
CD/USR/LOCAL/SRC #进入安装包存放目录
wget http://curl.haxx.se/download/curl-7.44.0.tar.gz #下载
2. Installation Curl
Cd/usr/local/src
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 #安装
3, recompile PHP
Find the PHP compilation parameters before the system
/usr/local/php/bin/php-i | grep Configure #查看php编译参数
As follows:
'./configure '--prefix=/usr/local/php '--with-config-file-path=/usr/local/php/etc '--with-apxs2=/usr/local/ Apache/bin/apxs '--with-mysql=/usr/local/mysql ' '--with-mysqli=/usr/local/mysql/bin/mysql_config '-- With-mysql-sock=/tmp/mysql.sock '--with-pdo-mysql=/usr/local/mysql '--with-gd '--with-png-dir=/usr/local/ Libpng '--with-jpeg-dir=/usr/local/jpeg '--with-freetype-dir=/usr/local/freetype '--with-xpm-dir=/usr/'-- With-zlib-dir=/usr/local/zlib '--with-t1lib=/usr/local/t1lib '--with-iconv '--enable-libxml '--enable-xml ' Enable-bcmath '--enable-shmop '--enable-sysvsem '--enable-inline-optimization '--enable-mbregex ' Enable-mbstring '--enable-ftp '--enable-gd-native-ttf '--with-openssl '--enable-pcntl '--enable-sockets ' With-xmlrpc '--enable-zip ', '--enable-soap ', '--without-pear '--with-gettext '--enable-session ', '--with-mcrypt ' With-curl '--enable-ctype '
To modify a parameter:
As follows
./configure--prefix=/usr/local/php--with-config-file-path=/usr/local/php/etc--with-apxs2=/usr/local/apache/bin /apxs--with-mysql=/usr/local/mysql--with-mysqli=/usr/local/mysql/bin/mysql_config--with-mysql-sock=/tmp/ Mysql.sock--with-pdo-mysql=/usr/local/mysql--with-gd--with-png-dir=/usr/local/libpng--with-jpeg-dir=/usr/local /jpeg--with-freetype-dir=/usr/local/freetype--with-xpm-dir=/usr/--with-zlib-dir=/usr/local/zlib--with-t1lib=/ Usr/local/t1lib--with-iconv--enable-libxml--enable-xml--enable-bcmath--enable-shmop-- Enable-inline-optimization--enable-mbregex--enable-mbstring--enable-ftp--enable-gd-native-ttf--with-openssl-- Enable-pcntl--enable-sockets--with-xmlrpc--enable-zip--enable-soap--without-pear--with-gettext--enable-session --with-mcrypt--with-curl=/usr/local/curl--enable-ctype
Remarks: Modify part
Cancel the original--with-curl
To be replaced by:--with-curl=/usr/local/curl
Remove single quotes on both sides of a parameter
Other unchanged
cd/usr/local/src/php #进入php安装包目录 (Note that PHP version should be the same as before)
./configure--prefix=/usr/local/php--with-config-file-path=/usr/local/php/etc--with-apxs2=/usr/local/apache/bin /apxs--with-mysql=/usr/local/mysql--with-mysqli=/usr/local/mysql/bin/mysql_config--with-mysql-sock=/tmp/ Mysql.sock--with-pdo-mysql=/usr/local/mysql--with-gd--with-png-dir=/usr/local/libpng--with-jpeg-dir=/usr/local /jpeg--with-freetype-dir=/usr/local/freetype--with-xpm-dir=/usr/--with-zlib-dir=/usr/local/zlib--with-t1lib=/ Usr/local/t1lib--with-iconv--enable-libxml--enable-xml--enable-bcmath--enable-shmop-- Enable-inline-optimization--enable-mbregex--enable-mbstring--enable-ftp--enable-gd-native-ttf--with-openssl-- Enable-pcntl--enable-sockets--with-xmlrpc--enable-zip--enable-soap--without-pear--with-gettext--enable-session --with-mcrypt--with-curl=/usr/local/curl--enable-ctype #配置
Make #编译
Make install #安装
4, restart Apache to make the settings effective
Service httpd Restart #重启
Fault Resolution!
5, testing
The following code, save as Phpinfo.php
<?php
Phpinfo ();
?>
Upload to the Site directory, find Curl, as shown in the following figure, the installation is successful!
At this point, Linux under the PHP installation Curl extension support HTTPS tutorial complete!
Extended reading: viewing software compilation parameters
View Nginx Compilation parameters:/usr/local/nginx/sbin/nginx-v
View Apache compilation parameters: Cat/usr/local/apache/build/config.nice
View MySQL compilation parameters: Cat/usr/local/mysql/bin/mysqlbug | grep configure_line
View PHP Compilation parameters:/usr/local/php/bin/php-i | grep Configure
Example
The code is as follows: curl_setopt ($ch, Curlopt_ssl_verifypeer, false); Skipping certificate checking
curl_setopt ($ch, Curlopt_ssl_verifyhost, true); Check to see if the SSL encryption algorithm exists from the certificate
Curl HTTPS Request Code
The code is as follows: <?php
/** Curl Get HTTPS request
* @param String $url the requested URL
* @param Array $data to send??
* The header to send when @param Array $header request
* @param int $timeout timeout time, default 30s
*/
function Curl_https ($url, $data =array (), $header =array (), $timeout =30) {
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_ssl_verifypeer, false); Skipping certificate checking
curl_setopt ($ch, Curlopt_ssl_verifyhost, true); Check to see if the SSL encryption algorithm exists from the certificate
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, Curlopt_httpheader, $header);
curl_setopt ($ch, Curlopt_post, true);
curl_setopt ($ch, Curlopt_postfields, Http_build_query ($data));
curl_setopt ($ch, Curlopt_returntransfer, true);
curl_setopt ($ch, Curlopt_timeout, $timeout);
$response = curl_exec ($ch);
if ($error =curl_error ($ch)) {
Die ($error);
}
Curl_close ($ch);
return $response;
}
Call
$url = ' https://www.example.com/api/message.php ';
$data = Array (' name ' => ' Fdipzone ');
$header = Array ();
$response = Curl_https ($url, $data, $header, 5);
Echo $response;
?>
More advanced and Practical code
function Curlpost ($url, $data, $timeout = 30)
{
$ssl = substr ($url, 0, 8) = = "https://"? True:false;
$ch = Curl_init ();
$opt = Array (
Curlopt_url => $url,
Curlopt_post => 1,
Curlopt_header => 0,
Curlopt_postfields => (array) $data,
Curlopt_returntransfer => 1,
Curlopt_timeout => $timeout,
);
if ($SSL)
{
$opt [Curlopt_ssl_verifyhost] = 1;
$opt [Curlopt_ssl_verifypeer] = FALSE;
}
Curl_setopt_array ($ch, $opt);
$data = curl_exec ($ch);
Curl_close ($ch);
return $data;
}
$data = Curlpost (' https://www.111cn.net ', Array (' P ' => ' Hello '));
Echo ($data);