一聚教程网:一个值得你收藏的教程网站

Linux下PHP安装curl扩展支持https例子

时间:2016-09-30 00:00:00 编辑:简简单单 来源:转载

问题:
 
线上运行的lamp服务器,默认yum安装的curl模块只支持http,不支持https
 
解决方法:
 
编译安装curl,重新编译php,使php的curl模块支持https。
 
具体步骤:
 
1、下载curl
 
cd /usr/local/src  #进入安装包存放目录
 
wget http://curl.haxx.se/download/curl-7.44.0.tar.gz  #下载
 
2、安装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、重新编译php
 
查找系统之前的php编译参数
 
/usr/local/php/bin/php -i | grep configure    #查看php编译参数
 
如下:
 
'./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'
 
对参数进行修改:
 
如下
 
./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=/usr/local/curl --enable-ctype
 
备注:修改部分
 
 
取消原来的--with-curl
 
替换为:--with-curl=/usr/local/curl
 
取消参数两边的单引号
 
其它不变
 
cd /usr/local/src/php #进入php安装包目录(注意php版本要和之前一样)
 
./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=/usr/local/curl --enable-ctype   #配置
 
make #编译
 
make install #安装
 
4、重启apache使设置生效
 
service httpd restart #重启
 
故障解决!
 
5、测试
 
以下代码,保存为phpinfo.php
 
<?php
 
phpinfo();
 
?>
 
上传到网站目录,查找curl,如下图所示,说明安装成功!
 


至此,Linux下PHP安装curl扩展支持https教程完成!
 
扩展阅读:查看软件编译参数
 
查看nginx编译参数:/usr/local/nginx/sbin/nginx -V
 
查看apache编译参数:cat /usr/local/apache/build/config.nice
 
查看mysql编译参数:cat /usr/local/mysql/bin/mysqlbug | grep CONFIGURE_LINE
 
查看php编译参数:/usr/local/php/bin/php -i | grep configure

例子

代码如下: curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true); // 从证书中检查SSL加密算法是否存在
 

curl https请求代码

代码如下: <?php
/** curl 获取 https 请求
* @param String $url 请求的url
* @param Array $data 要?送的??
* @param Array $header 请求时发送的header
* @param int $timeout 超时时间,默认30s
*/
function curl_https($url, $data=array(), $header=array(), $timeout=30){
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true); // 从证书中检查SSL加密算法是否存在
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;

}

// 调用
$url = 'https://www.example.com/api/message.php';
$data = array('name'=>'fdipzone');
$header = array();

$response = curl_https($url, $data, $header, 5);

echo $response;
?>
 更高级实用的代码

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);
 

文章评论

热门栏目