欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

PHP安装gpg扩展

程序员文章站 2022-05-22 09:57:28
...
我看了下,基本上原生php没有安装gpg扩展过。所以我们自己为php安装gpg扩展:

1.首先我们去下载gpg的源码包:

http://pecl.php.net/package/gnupg

我下载的是:gnupg-1.3.6.tgz

解压之后,cd到目录中phpize,然后./configure。

2.

andy@AndyMacBookPro:~/Downloads/gnupg-1.3.6$ phpize
Configuring for:
PHP Api Version:         20100412
Zend Module Api No:      20100525
Zend Extension Api No:   220100525
andy@AndyMacBookPro:~/Downloads/gnupg-1.3.6$ ./configure
checking for grep that handles long lines and-e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for a sed that does not truncate output... /usr/bin/sed
checking for cc... cc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether cc accepts -g... yes
checking for cc option to accept ISO C89...none needed
checking how to run the C preprocessor... cc -E
checking for icc... no
checking for suncc... no
checking whether cc understands -cand-o together... yes
checking for system library directory... lib
checking if compiler supports -R... no
checking if compiler supports -Wl,-rpath,... yes
checking build system type... i386-apple-darwin13.4.0
checking host system type... i386-apple-darwin13.4.0
checking target system type... i386-apple-darwin13.4.0
checking for PHP prefix... /usr
checking for PHP includes...-I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib
checking for PHP extension directory... /usr/lib/php/extensions/no-debug-non-zts-20100525
checking for PHP installed headers prefix... /usr/include/php
checking if debug is enabled... no
checking if zts is enabled... no
checking for re2c... no
configure: WARNING: You will need re2c 0.13.4or later if you want to regenerate PHP parsers.
checking for gawk... no
checking for nawk... no
checking for awk... awk
checking if awk is broken... no
checking for gnupg support... yes, shared
checking for gnupg files in default path...not found
configure: error: Please reinstall the gpgme distribution

可以看出来提醒我们安装gpgme。ok,那我们下面安装gpgme。

  1. 我们安装gpgme
andy@AndyMacBookPro:~/Downloads/gnupg-1.3.6$ brew install gpgme
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/gpgme-1.4.3########################################################################100.0%
==> Pouring gpgme-1.4.3.mavericks.bottle.tar.gz
??  /usr/local/Cellar/gpgme/1.4.3: 17 files, 1.2M
andy@AndyMacBookPro:~/Downloads/gnupg-1.3.6$ ./configure
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for a sed that does not truncate output... /usr/bin/sed
checking for cc... cc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether cc accepts -g... yes
checking for cc option to accept ISO C89... none needed
checking how to run the C preprocessor... cc -E
checking for icc... no
checking for suncc... no
checking whether cc understands -c and -o together... yes
checking for system library directory... lib
checking if compiler supports -R... no
checking if compiler supports -Wl,-rpath,... yes
checking build system type... i386-apple-darwin13.4.0
checking host system type... i386-apple-darwin13.4.0
checking target system type... i386-apple-darwin13.4.0
checking for PHP prefix... /usr
checking for PHP includes... -I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib
checking for PHP extension directory... /usr/lib/php/extensions/no-debug-non-zts-20100525
checking for PHP installed headers prefix... /usr/include/php
checking if debug is enabled... no
checking if zts is enabled... no
checking for re2c... noconfigure: WARNING: You will need re2c 0.13.4or later if you want to regenerate PHP parsers.
checking for gawk... no
checking for nawk... no
checking for awk... awk
checking if awk is broken... no
checking for gnupg support... yes, shared
checking for gnupg files indefault path... found in /usr/local/include
checking for gpgme_check_version in -lgpgme... yes
checking for gpg... /usr/local/bin/gpg
checking for ld used by cc... /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld
checking if the linker (/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld) is GNU ld... no
checking for /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld option to reload object files... -r
checking for BSD-compatible nm... /usr/bin/nm
checking whether ln -s works... yes
checking how to recognize dependent libraries... pass_all
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking dlfcn.h usability... yes
checking dlfcn.h presence... yes
checking for dlfcn.h... yes
checking the maximum length of command line arguments... 196608
checking command to parse /usr/bin/nm output from cc object... ok
checking for objdir... .libs
checking for ar... ar
checking for ranlib... ranlib
checking for strip... strip
checking for dsymutil... dsymutil
checking for nmedit... nmedit
checking for -single_module linker flag... yes
checking for -exported_symbols_list linker flag... yes
checking if cc supports -fno-rtti -fno-exceptions... yes
checking for cc option to produce PIC... -fno-common
checking if cc PIC flag -fno-common works... yes
checking if cc static flag -static works... no
checking if cc supports -c -o file.o... yes
checking whether the cc linker (/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld) supports shared libraries... yes
checking dynamic linker characteristics... darwin13.4.0 dyld
checking how to hardcode library paths into programs... immediate
checking whether stripping libraries is possible... yes
checking if libtool supports shared libraries... yes
checking whether to build shared libraries... yes
checking whether to build static libraries... nocreating libtool
appending configuration tag "CXX" to libtool
configure: creating ./config.status
config.status: creating config.h
andy@AndyMacBookPro:~/Downloads/gnupg-1.3.6$

make:

andy@AndyMacBookPro:~/Downloads/gnupg-1.3.6$ make
/bin/sh /Users/andy/Downloads/gnupg-1.3.6/libtool --mode=compile cc  -I.-I/Users/andy/Downloads/gnupg-1.3.6-DPHP_ATOM_INC-I/Users/andy/Downloads/gnupg-1.3.6/include -I/Users/andy/Downloads/gnupg-1.3.6/main -I/Users/andy/Downloads/gnupg-1.3.6-I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -I/usr/local/include  -DHAVE_CONFIG_H-g-O2-c /Users/andy/Downloads/gnupg-1.3.6/gnupg.c -o gnupg.lo
mkdir .libs
 cc -I.-I/Users/andy/Downloads/gnupg-1.3.6-DPHP_ATOM_INC-I/Users/andy/Downloads/gnupg-1.3.6/include -I/Users/andy/Downloads/gnupg-1.3.6/main -I/Users/andy/Downloads/gnupg-1.3.6-I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -I/usr/local/include -DHAVE_CONFIG_H-g-O2-c /Users/andy/Downloads/gnupg-1.3.6/gnupg.c  -fno-common-DPIC-o.libs/gnupg.o
/bin/sh /Users/andy/Downloads/gnupg-1.3.6/libtool --mode=compile cc  -I.-I/Users/andy/Downloads/gnupg-1.3.6-DPHP_ATOM_INC-I/Users/andy/Downloads/gnupg-1.3.6/include -I/Users/andy/Downloads/gnupg-1.3.6/main -I/Users/andy/Downloads/gnupg-1.3.6-I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -I/usr/local/include  -DHAVE_CONFIG_H-g-O2-c /Users/andy/Downloads/gnupg-1.3.6/gnupg_keylistiterator.c -o gnupg_keylistiterator.lo
 cc -I.-I/Users/andy/Downloads/gnupg-1.3.6-DPHP_ATOM_INC-I/Users/andy/Downloads/gnupg-1.3.6/include -I/Users/andy/Downloads/gnupg-1.3.6/main -I/Users/andy/Downloads/gnupg-1.3.6-I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -I/usr/local/include -DHAVE_CONFIG_H-g-O2-c /Users/andy/Downloads/gnupg-1.3.6/gnupg_keylistiterator.c  -fno-common-DPIC-o.libs/gnupg_keylistiterator.o
/bin/sh /Users/andy/Downloads/gnupg-1.3.6/libtool --mode=link cc -DPHP_ATOM_INC-I/Users/andy/Downloads/gnupg-1.3.6/include -I/Users/andy/Downloads/gnupg-1.3.6/main -I/Users/andy/Downloads/gnupg-1.3.6-I/usr/include/php -I/usr/include/php/main -I/usr/include/php/TSRM -I/usr/include/php/Zend -I/usr/include/php/ext -I/usr/include/php/ext/date/lib -I/usr/local/include  -DHAVE_CONFIG_H-g-O2-o gnupg.la -export-dynamic-avoid-version-prefer-pic-module-rpath /Users/andy/Downloads/gnupg-1.3.6/modules  gnupg.lo gnupg_keylistiterator.lo -Wl,-rpath,/usr/local/include/lib -L/usr/local/include/lib -lgpgme
cc ${wl}-flat_namespace ${wl}-undefined ${wl}suppress -o.libs/gnupg.so -bundle.libs/gnupg.o .libs/gnupg_keylistiterator.o  -L/usr/local/include/lib -lgpgme-Wl,-rpath-Wl,/usr/local/include/lib
ld: warning: directory not found for option '-L/usr/local/include/lib'
dsymutil .libs/gnupg.so || :
creating gnupg.la
(cd .libs && rm -f gnupg.la && ln -s../gnupg.la gnupg.la)
/bin/sh /Users/andy/Downloads/gnupg-1.3.6/libtool --mode=install cp ./gnupg.la /Users/andy/Downloads/gnupg-1.3.6/modules
cp ./.libs/gnupg.so /Users/andy/Downloads/gnupg-1.3.6/modules/gnupg.so
cp ./.libs/gnupg.lai /Users/andy/Downloads/gnupg-1.3.6/modules/gnupg.la
----------------------------------------------------------------------
Libraries have been installed in:
   /Users/andy/Downloads/gnupg-1.3.6/modules

If you ever happen to want tolink against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the `DYLD_LIBRARY_PATH' environment variable
     during execution

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------

Build complete.
Don't forget to run 'make test'.andy@AndyMacBookPro:~/Downloads/gnupg-1.3.6$

4.我们可以看到so文件已经生成,我们把so文件移动到php的扩展目录中:

andy@AndyMacBookPro:/usr/lib/php/extensions/no-debug-non-zts-20100525$ sudo cp /Users/andy/Downloads/gnupg-1.3.6/modules/gnupg.so ./

之后我们到php.ini中添加此扩展:

extension = gnupg.so

之后我们就可以在php中使用gunpg扩展了:

$url = 'http://192.168.2.85/2015/09/20/加密文件.csv';
$data = file_get_contents($url);
$data = mb_convert_encoding($data, 'UTF-8', 'GBK');

$encrypted_text = $data;
$res = gnupg_init();
gnupg_adddecryptkey($res,"ABCFA237F71C57CA2042439F93704A63BCC8A2F","12345678");
$plain = gnupg_decrypt($res,$encrypted_text);
var_dump($plain);exit;

非常感谢这篇文章:

http://devzone.zend.com/1278/using-gnupg-with-php/

以上就介绍了PHP安装gpg扩展,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。