Overview
The Stripe PHP library provides a clean, object-oriented interface to the Stripe payment API. It allows PHP developers to integrate payment processing, subscription management, and other Stripe features directly into their applications without manually constructing HTTP requests.
Installation
Using Composer (Recommended)
Composer is the standard dependency manager for PHP. To install the Stripe bindings, run:
composer require stripe/stripe-php
Then include the autoloader in your application:
require_once('vendor/autoload.php');
Manual Installation
If you prefer not to use Composer, download the latest release from the GitHub releases page. Extract the archive and include the init.php file:
require_once('/path/to/stripe-php/init.php');
System Requirements
The Stripe PHP library requires PHP 5.6.0 or later. Three PHP extensions must be available:
- curl: For making HTTP requests to Stripe's servers (you can substitute a custom HTTP client if needed)
- json: For encoding and decoding JSON data
- mbstring: For handling multibyte character strings
When using Composer, these dependencies are typically handled automatically. For manual installations, verify these extensions are enabled in your PHP configuration.
Getting Started
Once installed, set your API key and begin making requests. Here's a simple example that creates a charge:
\Stripe\Stripe::setApiKey('sk_test_BQokikJOvBiI2HlWgH4olfQ2');
$charge = \Stripe\Charge::create(['amount' => 2000, 'currency' => 'usd', 'source' => 'tok_189fqt2eZvKYlo2CTGBeg6Uq']);
echo $charge;
The library uses dynamic class initialization, meaning resource objects automatically adapt to your Stripe API version. This ensures compatibility across multiple API versions without requiring library updates for every Stripe change.
Legacy PHP Versions
If your application runs on older PHP versions, specific library versions are maintained:
- PHP 5.4 and 5.5: Use v6.21.1 (available as zip or tar.gz)
- PHP 5.3: Use v5.9.2 (available as zip or tar.gz)
- PHP 5.2: Use v1.18.0 (available as zip or tar.gz)
These legacy versions continue to work with current Stripe API features for standard use cases. For PHP 5.2, include the library differently:
require_once("/path/to/stripe-php/lib/Stripe.php");
Stripe::setApiKey('d8e8fca2dc0f896fd7cb4cb0031ba249');
$charge = Stripe_Charge::create(array('source' => 'tok_XXXXXXXX', 'amount' => 2000, 'currency' => 'usd'));
Configuration and Best Practices
Always use test API keys during development. Stripe provides separate test and live keys: test keys begin with sk_test_ and live keys with sk_live_. Never commit live keys to version control.
For production applications, store your API key in environment variables or a secure configuration file outside your repository. The library supports custom request timeouts, though Stripe recommends against reducing timeouts for non-read operations like charge creation, since local timeouts do not cancel requests on Stripe's servers.
Documentation and Support
Complete API documentation is available in the official Stripe PHP API reference. The library's source code and issue tracker are hosted on GitHub.