解決了一個(gè) curl 庫(kù)導(dǎo)致的 https 訪問(wèn)錯(cuò)誤

來(lái)源:http://i.am.simonkuang.com/post/resolved-a-https-handshake-problem-against-curl/

使用 EasyWeChat 庫(kù)調(diào)用微信服務(wù)的時(shí)候,在 laravel tinker 里面調(diào)試,發(fā)現(xiàn)每次進(jìn)入 tinker 之后,第一次調(diào)用接口沒(méi)有問(wèn)題,第二次之后,就會(huì)報(bào)一個(gè)很詭異的錯(cuò)誤。

$app = app('wechat');
// EasyWeChat\Foundation\Application {#628}

$users = $app->user->lists();
// EasyWeChat\Support\Collection {#756}

$user = $app->user->get('o7qrUviv1tkcDFMJ5wdXrpng9NNQ');
// GuzzleHttp\Exception\ConnectException with message 'cURL error 35: A PKCS #11 module returned CKR_DEVICE_ERROR, indicating that a problem has occurred with the token or slot. (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)'

PKCS #11 返回的錯(cuò)誤,好高端啊。

中間的曲折就不說(shuō)了。答案在一篇歪果仁的博客上找到,在 github 的評(píng)論上得到印證及解決問(wèn)題的思路。

很明顯的,兩篇在遇到跟我相同問(wèn)題的同時(shí),都提到了一個(gè)關(guān)鍵詞 NSS,因此,我又看了一下我自己的 curl 庫(kù)信息。

curl -V
// curl 7.48.0 (x86_64-pc-linux-gnu) libcurl/7.29.0 NSS/3.19.1 Basic ECC zlib/1.2.7 libidn/1.28 libssh2/1.4.3
// Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp scp sftp smtp smtps telnet tftp
// Features: AsynchDNS IDN IPv6 Largefile NTLM NTLM_WB SSL libz

果然我本地的 curl 是帶 NSS 的。

于是,用一個(gè)不帶 NSS 的 curl 庫(kù)來(lái)重新編譯 php 吧。

mkdir -p /data/soft

# 編譯并安裝不帶 nss 的 curl
cd /data/soft
wget "https://curl.haxx.se/download/curl-7.48.0.tar.bz2"
tar zxf curl-7.48.0.tar.bz2
cd curl-7.48.0
./configure --prefix=/usr --without-nss
make && make install

# 重新編譯 php
cd /data/soft
wget -O "php-5.6.19.tar.bz2" "http://cn2.php.net/get/php-5.6.19.tar.bz2/from/this/mirror"
tar jxf php-5.6.19.tar.bz2
cd php-5.6.19
'./configure' \
  '--prefix=/usr/local/php-5.6.19' \
  '--with-libdir=lib64' \
  '--with-config-file-path=/usr/local/php-5.6.19/lib' \
  '--with-fpm-user=nobody' \
  '--with-fpm-group=nobody' \
  '--with-libxml-dir=/usr' \
  '--with-openssl=/usr' \
  '--with-openssl-dir=/usr' \
  '--with-zlib' \
  '--enable-bcmath' \
  '--with-bz2=/usr' \
  '--enable-calendar' \
  '--with-curl=/usr' \
  '--enable-dba' \
  '--with-gdbm=/usr' \
  '--enable-exif' \
  '--enable-ftp' \
  '--with-gd' \
  '--with-vpx-dir=/usr' \
  '--with-jpeg-dir=/usr' \
  '--with-png-dir=/usr' \
  '--with-xpm-dir=/usr' \
  '--with-zlib-dir=/usr' \
  '--with-freetype-dir=/usr' \
  '--with-gettext=/usr' \
  '--with-gmp=/usr' \
  '--with-mhash=/usr' \
  '--enable-intl' \
  '--enable-mbstring' \
  '--with-mcrypt=/usr' \
  '--with-mysql=/usr/local/mysql' \
  '--with-mysql-sock=/tmp/mysql.sock' \
  '--with-mysqli=/usr/local/mysql/bin/mysql_config' \
  '--enable-embedded-mysqli' \
  '--enable-pcntl' \
  '--enable-opcache' \
  '--with-pdo-mysql' \
  '--with-libedit=/usr' \
  '--with-readline=/usr' \
  '--enable-soap' \
  '--enable-mysqlnd' \
  '--enable-sockets' \
  '--enable-sysvmsg' \
  '--enable-sysvsem' \
  '--enable-sysvshm' \
  '--with-tidy=/usr' \
  '--enable-wddx' \
  '--with-xsl=/usr' \
  '--enable-fpm'
make && make install

# 重新啟動(dòng)由 supervisor 管理的 php-fpm(非必須)
supervisorctl restart php-fpm

檢查 curl 的信息。
NSS 不見(jiàn)了!

PS:一些心得

  • 原來(lái) CURL 之前一直有這個(gè)問(wèn)題,見(jiàn)這則 stackoverflow 上面的答復(fù);
  • 一個(gè)基礎(chǔ)庫(kù)的小問(wèn)題,會(huì)因?yàn)橐蕾嚇?shù)的關(guān)系,被無(wú)限放大;
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容