預設情況下,libcurl不支援https, 如果使用https連結,就會出現" Protocol https not supported or disabled in libcurl" 的錯誤提示。查看curl是否支援https可以使用命令:
curl -V。
curl有兩種方式使用https :
1. 設定為不驗證認證和HOST
code = curl_easy_setopt(handle, CURLOPT_SSL_VERIFYPEER, 0L);
2. 設定一個SSL判別認證
http://curl.haxx.se/ca/cacert.pem
基於這兩種方法都不知道怎麼去使用,所以只好用最笨的方法,重裝curl:
1.下載curl-7.14.0.tar.gz壓縮包,網上很多
2.解壓:# tar -zxvf curl-7.14.0.tar.gz
# cd curl-7.14.0
# ./configure 這裡注意的是最後會顯示一段資訊提示是否支援https:
curl version: 7.14.0 Host setup: x86_64-unknown-linux-gnu Install prefix: /usr/local Compiler: gcc SSL support: no (--with-ssl / --with-gnutls) zlib support: enabled krb4 support: no (--with-krb4*) GSSAPI support: no (--with-gssapi) SPNEGO support: no (--with-spnego) c-ares support: no (--enable-ares) ipv6 support: enabled IDN support: enabled Build libcurl: Shared=yes, Static=yes Built-in manual: enabled Verbose errors: enabled (--disable-verbose) SSPI support: no (--enable-sspi) ca cert path: no
這裡的SSL support提示的是不支援的,因為https協議是加密安全的基於http的協議,需要使用openssl的靜態庫,所以需要支援https就必須下載openssl,這裡不做贅述,方法可見:http://www.linuxidc.com/Linux/2011-01/31229.htm
# vi /etc/ld.so.conf,在這裡面將openssl產生的庫檔案所在目錄加入,使用命令ldconfig重新整理緩衝。
# ./configure --prefix=/usr/local/curl --with-ssl=/usr/local/ssl ,注意最後一段資訊
curl version: 7.14.0 Host setup: x86_64-unknown-linux-gnu Install prefix: /usr/local Compiler: gcc SSL support: enabled (OpenSSL) zlib support: enabled krb4 support: no (--with-krb4*) GSSAPI support: no (--with-gssapi) SPNEGO support: no (--with-spnego) c-ares support: no (--enable-ares) ipv6 support: enabled IDN support: enabled Build libcurl: Shared=yes, Static=yes Built-in manual: enabled Verbose errors: enabled (--disable-verbose) SSPI support: no (--enable-sspi) ca cert path: /usr/local/share/curl/curl-ca-bundle.crt
提示支援openssl已經支援了,然後再make, make install即可。
# curl -V
curl 7.14.0 (x86_64-unknown-linux-gnu) libcurl/7.14.0 OpenSSL/1.0.1e zlib/1.2.3 libidn/1.18Protocols: ftp gopher telnet dict ldap http file https ftps Features: IDN IPv6 Largefile NTLM SSL libz
提示已經支援https了。