CMIContent Marketing InstituteContent directory

Technology · Software · General

Stripe PHP bindings [

Software Technology

TL;DR: Stripe PHP bindings provide a convenient library to integrate Stripe payment processing into PHP applications, installable via Composer with support for PHP 5.6 and later.

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:

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:

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.

Key takeaways

Frequently asked questions

What is the minimum PHP version required?

PHP 5.6.0 or later is required for current versions. Older versions of the library support PHP 5.2, 5.3, 5.4, and 5.5, available from the GitHub releases page.

Do I need Composer to use Stripe PHP bindings?

No, Composer is recommended but optional. You can manually download the library from GitHub releases and include init.php directly in your code.

What PHP extensions are required?

Three extensions are required: curl (for HTTP requests), json (for data encoding), and mbstring (for multibyte strings). Composer handles these automatically if available.

How do I use test vs. live API keys?

Test keys start with sk_test_ and live keys with sk_live_. Always use test keys during development. Store keys in environment variables, never in version control.

Will the library work if Stripe's API changes?

Yes. The library uses dynamic class initialization that adapts to different Stripe API versions, ensuring compatibility without requiring updates for every API change.

More in General · More in Software · More in Technology