PHP Composer Guide

Welcome! This article will assist you to understand better about getting to know Composer!

In this article, we’ll cover what Composer is, what Composer does, how to use a composer, when to use Composer and how Composer is used in Xendit PHP SDK, and how you can easily integrate Xendit with your PHP application via composer.

What is a composer? What does a composer do?

If you’ve been coding in PHP for some time, you'll be aware of how PHP libraries can help save work and make code reusable. In the past, it was harder to add libraries to PHP, which led to a lot of reinventing the wheel for common features. For example, when you want to integrate with a new library, you have to download the library and call include native PHP function and you’re able to execute your desired function in that library.

In a nutshell, we need a tool which can be used to install libraries and manage application dependencies. Composer does a great job of this. It is an application-level package manager for PHP that has gained immense popularity and become the standard for managing dependencies in PHP applications.

INFO

"Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you" — Composer Introduction

It’s important to note that Composer allows you to install the necessary libraries on a per-project basis. It allows you to use different versions of the same library across different PHP projects. Of course, there’s an option to install a library globally, but it’s not recommended. If you’ve heard of npm for Node.js, or Bundler for Ruby, that's what Composer is for PHP.

To install and use libraries that are managed by Composer, you just need to declare them in your project in a standard format, and Composer will manage the rest. For example, if you want to install the phpmailer library by using Composer, you just need to run the following command in the root of your project.

$composer require phpmailer/phpmailer 

When to use Composer

Now you should have understood the composer from the previous section. And you might be wondering when is the right time to use a composer. If your PHP project uses multiple external libraries and various packages as helpers within your project, composer is the right tool for you to consider and use.

Composer allows you to very easily install a multitude of libraries for your project without dealing with the details. It allows the authors to use another library themselves without you having to deal with all the details.

For example, if you would want to manually install a library that requires to install two additional libraries in the correct version, with these libraries requiring one additional library each, and additionally you'd have to initialize the autoloading of all five libraries, this might be some task to tackle.

With Composer, you only require one library, and after that everything is done for you.

Additionally, it makes updating way easier for you. If your library has a bug that got fixed in a newer version, you simply update and see if your application still runs. You would spend most of the time checking everything still runs - and barely any time updating.

How to use Composer

There are two ways when it comes to installing libraries with Composer. Let’s quickly go through it to understand how it works.

The install Command

To use install, you need to create a composer.json file in your project first. In the composer.json file, you just need to declare your project dependencies, as shown in the following snippet.

{
    "require": {
        "phpmailer/phpmailer": "~6.1"
    }
}

Next, when you run the composer install command from that folder,

$composer install 

Composer installs the phpmailer package and its dependencies in the vendor directory. More importantly, it also creates the composer.lock file, which maintains a list of all of the packages and the exact versions of them that are installed.

How composer is used in Xendit PHP SDK

Installation

Install the Xendit PHP library by adding the following to your composer.json file

{
  "repositories": [
    {
      "type": "vcs",
      "url": "https://github.com/xendit/xendit-php.git"
    }
  ],
  "require": {
    "xendit/xendit-php": "4.0.0" // Refer to GitHub for our latest version
  }
}

Then install the library by running

composer install

You've successfully installed Xendit's PHP library with Composer. Then import the library:

<?php
require_once(__DIR__ . '/vendor/autoload.php');

Check out our Python source on Github for the complete installation guide and to verify the latest version.

Authorization

The SDK needs to be instantiated using your secret API key obtained from the Xendit Dashboard. You can sign up for a free Dashboard account here.

<?php
require_once(__DIR__ . '/vendor/autoload.php');

use Xendit\Configuration;

Configuration::setXenditKey('XENDIT_API_KEY');

Sample Usage

You can use the APIs below to interface with Xendit's BalanceApi. To start using the API, you need to configure the secret key and initiate the client instance.

<?php
require_once(__DIR__ . '/vendor/autoload.php');

use Xendit\Configuration;
use Xendit\BalanceAndTransaction\BalanceApi;

Configuration::setXenditKey("YOUR_API_KEY_HERE");

$apiInstance = new BalanceApi();
$account_type = "CASH"; // string | The selected balance type
$currency = "IDR"; // string | Currency for filter for customers with multi currency accounts
$for_user_id = "5dbf20d7c8eb0c0896f811b6"; // string | The sub-account user-id that you want to make this transaction for. This header is only used if you have access to xenPlatform. See xenPlatform for more information

try {
    $result = $apiInstance->getBalance($account_type, $currency, $for_user_id);
    print_r($result);
} catch (\Xendit\XenditSdkException $e) {
    echo 'Exception when calling BalanceApi->getBalance: ', $e->getMessage(), PHP_EOL;
    echo 'Full Error: ', json_encode($e->getFullError()), PHP_EOL;
}

For sample of other available APIs, please refer to our PHP SDK Github Documentation.

Last Updated on 2024-01-09