
最近在一個項目中需要用到php下的curl擴展,但是在實際運行時遭遇到了curl函數(shù)無法執(zhí)行的問題,頁面錯誤為:
Call to undefined function curl_init();
環(huán)境配置:
- windows 10 64bit
- Apache 2.4(win32)
- php 5.6.21 ts
按照常規(guī)步驟檢查
- 用
phpinfo()找到php.ini文件路徑 -
extension_dir是否配置正確 -
extension=php_curl.dll是否打開
結(jié)果一切正常,但是phpinfo()的結(jié)果里沒有curl模塊
然后用搜的,網(wǎng)上大多數(shù)的解決方案是:
- 把
libeay32.dll,ssleay32.dll放進system32里,重啟apache,還有的說要把php5ts.dll也放進去,最接近的是把libssh2.dll放進<Apache dir>/bin里 - 考慮php版本號和
php_curl.dll的版本號是否一致 - 可能要用到
libcurl.dll庫 - 檢查apache的重啟姿勢,不能只用
httpd -k restart,還要從系統(tǒng)服務里重啟
最后朋友給出一種終極方法:把現(xiàn)在的環(huán)境干掉,找個集成環(huán)境重新裝一遍!
這也是個辦法,可是我不想重裝,咋辦?
另外,我還發(fā)現(xiàn)了一個奇怪的現(xiàn)象,我在command line下檢查加載的模塊時,curl是可以加載的,并且可以執(zhí)行。但是用phpinfo()就是顯示沒有加載。
接著就再放狗查這個現(xiàn)象,然而沒有什么有用的信息。
一個偶然的想法,去看了apache的日志,發(fā)現(xiàn)一行:
PHP Warning: PHP Startup: Unable to load dynamic library 'C:\\Server\\php\\ext\\php_curl.dll' - The operating system cannot run %1.\r\n in Unknown on line 0
根據(jù)apache的錯誤日志,大概能看出來,這是操作系統(tǒng)沒有找到對應的動態(tài)鏈接庫。
于是放狗,打開php官方的一篇文檔:PHP: 安裝 - Manual,查找The operating system cannot run %1.\r\n,找到標題是mtudor AT icefusion remove me DOT co uk ?的一篇帖子內(nèi)容,在這篇發(fā)布于7年前的帖子里,他講述了遇到的一個跟這個問題幾乎一致的現(xiàn)象!,并對這個問題給出了詳細的分析、產(chǎn)生的原因和解決方法。
其中在原因一節(jié),他這樣寫道:
CAUSE
-----
This was caused by PHP picking up the WRONG VERSIONS of libeay.dll and ssleay.dll, which were present in multiple locations on my computer.
When any application attempts to use a dll file in windows, the system searches for this file using the following order:
- The directory from which the application loaded.
- The windows\system32 directory.
- The windows\system directory.
- The windows directory.
- The current directory.
- The directories that are listed in the PATH environment variable.
(http://msdn.microsoft.com/en-us/library/ms682586.aspx)
For PHP running under Apache, the application directory is <apache dir>\bin and NOT <php dir>. PHP was finding OUT OF DATE versions of libeay.dll and ssleay.dll in <apache dir>\bin (probably installed when I enabled SSL support in my web server). Because of this, the latest versions in windows\system32 were never reached.
他主要分析了windows系統(tǒng)查找動態(tài)鏈接庫文件的順序,注意到其中的一段話:
**For PHP running under Apache, the application directory is <apache dir>\bin and NOT <php dir>. **
這說明apache可能需要在自己的運行環(huán)境里找到libeay32.dll,ssleay32.dll和php_curl.dll這幾個文件,但我們并沒有把它們從php文件夾下拷到apache/bin下,而是放到了windows/system32中,而并卵。
糾正以后,終于看到了熟悉的結(jié)果。而這也正好解釋了我能在command line下可以執(zhí)行curl_init(),但用phpinfo()函數(shù)看不到的現(xiàn)象。