問題:
線上啟動並執行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);