curl已加载
最近在一个项目中需要用到php下的curl扩展,但是在实际运行时遭遇到了curl函数无法执行的问题,页面错误为:
call to undefined function curl_init();
环境配置:
windows 10 64bit apache 2.4(win32) php 5.6.21 ts 按照常规步骤检查
用phpinfo()找到php.ini文件路径 extension_dir是否配置正确 extension=php_curl.dll是否打开 结果一切正常,但是phpinfo()的结果里没有curl模块
然后用搜的,网上大多数的解决方案是:
把libeay32.dll,ssleay32.dll放进system32里,重启apache,还有的说要把php5ts.dll也放进去,最接近的是把libssh2.dll放进/bin里 考虑php版本号和php_curl.dll的版本号是否一致 可能要用到libcurl.dll库 检查apache的重启姿势,不能只用httpd -k restart,还要从系统服务里重启 最后朋友给出一种终极方法:把现在的环境干掉,找个集成环境重新装一遍!
这也是个办法,可是我不想重装,咋办?
另外,我还发现了一个奇怪的现象,我在command line下检查加载的模块时,curl是可以加载的,并且可以执行。但是用phpinfo()就是显示没有加载。
接着就再放狗查这个现象,然而没有什么有用的信息。
一个偶然的想法,去看了apache的日志,发现一行:
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
根据apache的错误日志,大概能看出来,这是操作系统没有找到对应的动态链接库。
于是放狗,打开php官方的一篇文档:php: 安装 - manual,查找the operating system cannot run %1.\r\n,找到标题是mtudor at icefusion remove me dot co uk ¶的一篇帖子内容,在这篇发布于7年前的帖子里,他讲述了遇到的一个跟这个问题几乎一致的现象!,并对这个问题给出了详细的分析、产生的原因和解决方法。
其中在原因一节,他这样写道:
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 \bin and not . php was finding out of date versions of libeay.dll and ssleay.dll in \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系统查找动态链接库文件的顺序,注意到其中的一段话:
for php running under apache, the application directory is \bin and not .
这说明apache可能需要在自己的运行环境里找到libeay32.dll,ssleay32.dll和php_curl.dll这几个文件,但我们并没有把它们从php文件夹下拷到apache/bin下,而是放到了windows/system32中,而并卵。
纠正以后,终于看到了熟悉的结果。而这也正好解释了我能在command line下可以执行curl_init(),但用phpinfo()函数看不到的现象。
总结:日志很重要!