README.md
86 lines
| 1 | composer/ca-bundle |
| 2 | ================== |
| 3 | |
| 4 | Small utility library that lets you find a path to the system CA bundle, |
| 5 | and includes a fallback to the Mozilla CA bundle. |
| 6 | |
| 7 | Originally written as part of [](https://github.com/composer/composercomposer/composer](https://github.com/composer/composer](https://github.com/composer/composer), |
| 8 | now extracted and made available as a stand-alone library. |
| 9 | |
| 10 | |
| 11 | Installation |
| 12 | ------------ |
| 13 | |
| 14 | Install the latest version with: |
| 15 | |
| 16 | ```bash |
| 17 | $ composer require composer/ca-bundle |
| 18 | ``` |
| 19 | |
| 20 | |
| 21 | Requirements |
| 22 | ------------ |
| 23 | |
| 24 | * PHP 5.3.2 is required but using the latest version of PHP is highly recommended. |
| 25 | |
| 26 | |
| 27 | Basic usage |
| 28 | ----------- |
| 29 | |
| 30 | ### `Composer\CaBundle\CaBundle` |
| 31 | |
| 32 | - `CaBundle::getSystemCaRootBundlePath()`: Returns the system CA bundle path, or a path to the bundled one as fallback |
| 33 | - `CaBundle::getBundledCaBundlePath()`: Returns the path to the bundled CA file |
| 34 | - `CaBundle::validateCaFile($filename)`: Validates a CA file using openssl_x509_parse only if it is safe to use |
| 35 | - `CaBundle::isOpensslParseSafe()`: Test if it is safe to use the PHP function openssl_x509_parse() |
| 36 | - `CaBundle::reset()`: Resets the static caches |
| 37 | |
| 38 | |
| 39 | #### To use with curl |
| 40 | |
| 41 | ```php |
| 42 | $curl = curl_init("https://example.org/"); |
| 43 | |
| 44 | $caPathOrFile = \Composer\CaBundle\CaBundle::getSystemCaRootBundlePath(); |
| 45 | if (is_dir($caPathOrFile)) { |
| 46 | curl_setopt($curl, CURLOPT_CAPATH, $caPathOrFile); |
| 47 | } else { |
| 48 | curl_setopt($curl, CURLOPT_CAINFO, $caPathOrFile); |
| 49 | } |
| 50 | |
| 51 | $result = curl_exec($curl); |
| 52 | ``` |
| 53 | |
| 54 | #### To use with php streams |
| 55 | |
| 56 | ```php |
| 57 | $opts = array( |
| 58 | 'http' => array( |
| 59 | 'method' => "GET" |
| 60 | ) |
| 61 | ); |
| 62 | |
| 63 | $caPathOrFile = \Composer\CaBundle\CaBundle::getSystemCaRootBundlePath(); |
| 64 | if (is_dir($caPathOrFile)) { |
| 65 | $opts['ssl']['capath'] = $caPathOrFile; |
| 66 | } else { |
| 67 | $opts['ssl']['cafile'] = $caPathOrFile; |
| 68 | } |
| 69 | |
| 70 | $context = stream_context_create($opts); |
| 71 | $result = file_get_contents('https://example.com', false, $context); |
| 72 | ``` |
| 73 | |
| 74 | #### To use with Guzzle |
| 75 | |
| 76 | ```php |
| 77 | $client = new \GuzzleHttp\Client([ |
| 78 | \GuzzleHttp\RequestOptions::VERIFY => \Composer\CaBundle\CaBundle::getSystemCaRootBundlePath() |
| 79 | ]); |
| 80 | ``` |
| 81 | |
| 82 | License |
| 83 | ------- |
| 84 | |
| 85 | composer/ca-bundle is licensed under the MIT License, see the LICENSE file for details. |
| 86 |