2021-02-21 01:34:32 +01:00
# PC/SC for PHP
2021-02-08 21:20:05 +01:00
2021-02-21 01:34:32 +01:00
## About
2021-02-08 21:20:05 +01:00
2021-02-21 01:34:32 +01:00
This is the only extension for using [PC/SC ](http://www.pcscworkgroup.com/ ) based smart cards with [PHP ](http://www.php.net ). It is a wrapper to the wonderful and free project by Ludovic Rousseau, [PCSC-Lite ](https://pcsclite.apdu.fr/ ), which is the middleware to access a smart card using SCard API (PC/SC). Since PCSC-Lite is compatible to the winscard API it should be possible to compile this extension using a Windows or macOS operating system.
2021-02-08 21:20:05 +01:00
2021-02-21 01:34:32 +01:00
Thanks are going to Johann Dantant! He provided a PC/SC extension for PHP since 2005 and I reused some of his code. He allowed me to relicense these parts under the terms of the PHP license so I could integrate PCSC-Lite native into PHP.
2021-02-08 21:20:05 +01:00
2021-02-21 01:34:32 +01:00
## Installation
2021-02-08 21:20:05 +01:00
2021-02-21 01:34:32 +01:00
I recommend to install the PECL extension the "PHP" way:
2021-02-08 21:20:05 +01:00
2021-02-21 01:34:32 +01:00
```
pecl install pcsc-beta
```
2021-02-08 21:20:05 +01:00
2021-02-21 01:34:32 +01:00
You can install the latest code by downloading the sources and compile yourself too.
```
git clone https://github.com/pcsc-for-php/pcsc.git
2021-02-21 00:26:56 +01:00
cd pcsc
2021-02-08 21:20:05 +01:00
phpize
./configure
make
2021-02-21 01:34:32 +01:00
make install
```
2021-02-08 21:20:05 +01:00
2021-02-21 01:34:32 +01:00
After that you have all needed files in ./modules/ .
2021-02-08 21:20:05 +01:00
2021-02-21 01:34:32 +01:00
## API
2021-02-08 21:20:05 +01:00
2021-02-21 01:34:32 +01:00
The extension currently provides the following API:
2021-02-08 21:20:05 +01:00
2021-02-21 01:55:04 +01:00
### scard_establish_context();
2021-02-08 21:20:05 +01:00
2021-02-21 01:34:32 +01:00
Returns the application $context to the PC/SC resource manager.
2021-02-08 21:20:05 +01:00
2021-02-21 01:55:04 +01:00
### scard_is_valid_context($context);
2021-02-08 21:20:05 +01:00
2021-02-21 01:34:32 +01:00
Returns TRUE if $context is valid or FALSE if $context is not valid.
2021-02-08 21:20:05 +01:00
2021-02-21 01:55:04 +01:00
### scard_release_context($context);
2021-02-08 21:20:05 +01:00
2021-02-21 01:34:32 +01:00
Releases the application $context.
2021-02-08 21:20:05 +01:00
2021-02-21 01:55:04 +01:00
### scard_list_readers($context);
2021-02-08 21:20:05 +01:00
2021-02-21 01:34:32 +01:00
Returns an array of available readers or FALSE.
2021-02-08 21:20:05 +01:00
2021-02-21 01:34:32 +01:00
#### Example:
2021-02-08 21:20:05 +01:00
2021-02-21 01:34:32 +01:00
```
array(3) {
[0]=>
2021-02-08 21:20:05 +01:00
string(26) "OMNIKEY CardMan 5x21 00 00"
2021-02-21 01:34:32 +01:00
[1]=>
2021-02-08 21:20:05 +01:00
string(26) "OMNIKEY CardMan 5x21 00 01"
2021-02-21 01:34:32 +01:00
[2]=>
2021-02-08 21:20:05 +01:00
string(76) "SCL01x Contactless Reader [SCL01x Contactless Reader] (21161009200722) 00 00"
}
2021-02-21 01:34:32 +01:00
```
2021-02-08 21:20:05 +01:00
2021-02-21 01:55:04 +01:00
### scard_connect($context, "OMNIKEY CardMan 5x21 00 00");
2021-02-08 21:20:05 +01:00
2021-02-21 01:34:32 +01:00
Connects to a card. Returns the $connection to a reader or FALSE.
2021-02-08 21:20:05 +01:00
2021-02-21 01:55:04 +01:00
### scard_reconnect($connection);
2021-02-08 21:20:05 +01:00
2021-02-21 01:34:32 +01:00
Returns the $connection to a reader or FALSE.
2021-02-08 21:20:05 +01:00
2021-02-21 01:55:04 +01:00
### scard_disconnect($connection);
2021-02-08 21:20:05 +01:00
2021-02-21 01:34:32 +01:00
Disconnects the $connection to a card. Returns the TRUE if disconnecting was succesful or FALSE.
2021-02-08 21:20:05 +01:00
2021-02-21 01:55:04 +01:00
### scard_transmit($connection, $apdu);
2021-02-08 21:20:05 +01:00
2021-02-21 01:34:32 +01:00
Returns the response $apdu as string or FALSE.
2021-02-08 21:20:05 +01:00
2021-02-21 01:55:04 +01:00
### scard_status($connection);
2021-02-08 21:20:05 +01:00
2021-02-21 01:34:32 +01:00
Returns the status or FALSE.
2021-02-08 21:20:05 +01:00
2021-02-21 01:55:04 +01:00
### scard_cancel($context);
2021-02-08 21:20:05 +01:00
2021-02-21 01:34:32 +01:00
## License
2021-02-08 21:20:05 +01:00
2021-02-21 01:34:32 +01:00
This code is licensed under the terms of the PHP License version 3.01. PCSC-Lite is licensed in a way where it is possible to integrate it native in the PHP environment.
2021-02-08 21:20:05 +01:00
2021-02-21 01:34:32 +01:00
## Links
* [PECL Project Page ](http://pecl.php.net/package/pcsc ) - Page for package download at PHP.net
* [PC/SC Worgroup ](http://www.pcscworkgroup.com/ ) - Homepage and definition file downloads
* [PC/SC ](http://en.wikipedia.org/wiki/PC/SC ) - PC/SC at Wikipedia
* [PC/SC-Lite ](https://pcsclite.apdu.fr/ ) - The free and open implementation of the PC/SC SCard API for UNIX
* [PCSC sample in PHP5 ](http://ludovicrousseau.blogspot.de/2015/01/pcsc-sample-in-php5.html ) - Ludovic Rousseau about "PC/SC for PHP"
2021-02-08 21:20:05 +01:00