I Build libSQL Client for PHP
Tags: database, php, turso, sdk
Published: 2024-05-08 18:46:56

Hello Punk! Yes, I am again...

> **LibSQL** is SQLite for modern applications. But, they doesn't have PHP SDK. That's why, I build one!

---

Here is, how I start build the SDK for Turso/libSQL.

{% embed https://dev.to/darkterminal/turso-php-the-libsql-client-for-php-1amh %}

## The LibSQL Client PHP

This library SDK is build entirely using PHP that wrap main functionality of LibSQL and try to keep match with official SDK did.

At first, I made this SDK just for fun, because at that time my fingers needed to dance as a routine activity with the background music that I hated the most.

But then this became Turso/LibSQL's first medal in PHP. I found out about this after the CEO of Turso told me that if I could make a PHP LibSQL Client that could read the LibSQL driver natively (not using the HTTP SDK) it would be very profitable:

- Developers can create **local database connections** using LibSQL (not SQLite)
- Developers can create **in-memory database connections** using LibSQL (not SQLite)
- Developers can create **remote database connections** using LibSQL (not SQLite)
- Developers can create **remote replica / remote embedded connections** using LibSQL (not SQLite)

When I can get it to interact natively with the LibSQL driver...

## How?

- Then how can I talk directly to the LibSQL driver from PHP?
- Because LibSQL is built with Rust and I can't speak Rust! Damn it! (At first)

## Learning Rust Damn it!

Yes, I finally learned to speak Rust. My first assignment was "**Learning Rust while creating a native driver for PHP**" because this has long been an interesting goal for me when I can create a native PHP driver myself!

In short, I interacted with the LibSQL binary using Rust FFI and `cbindgen` and generated a binary and C `header` that I could read using PHP FFI.

> Yes, I made a middleware/translator/whatever to be able to communicate PHP with the LibSQL binary.

And yeah, that works! If you if you look at `composer.json` file at `libsql-client-php`, they will look like this:

```json
{
    "name": "darkterminal/libsql-client-php",
    "description": "PHP client API for libSQL",
    "license": "MIT",
    "autoload": {
        "psr-4": {
            "Darkterminal\\LibSQL\\": "src/"
        },
        "files": [
            "src/Utils/helpers.php",
            "src/Utils/constants.php"
        ]
    },
    "authors": [
        {
            "name": "Imam Ali Mustofa",
            "email": "darkterminal@duck.com"
        }
    ],
    "scripts": {
        "post-install-cmd": ["./vendor/bin/build"],
        "post-update-cmd": ["./vendor/bin/build"]
    },
    "require": {
        "guzzlehttp/guzzle": "^7.0",
        "darkterminal/libsql-php-ext": "^1.0"
    },
    "funding": [
        {
            "type": "github",
            "url": "https://github.com/sponsors/darkterminal"
        },
        {
            "type": "other",
            "url": "https://paypal.me/lazarusalhambra"
        }
    ]
}
```
Because this library only encapsulates the main functionality as offered by **Turso/LibSQL**, I separated it from its core dependencies. Yes! the `darkterminal/libsql-php-ext` is the driver that will be downloaded by `libsql-client-php` and will be compiled directly during the installation process.

That's why, to be able to use this library developers need to meet the following requirements:
- Linux or Darwin OS
- C/C++ Compiler
- jq
- Rust Installed
- PHP Installed
- FFI Extension is Enabled (_Why? I read the C header definition from wrapper_)

The driver will compile on-demand from you device.

## Read The Docs

I also provide a documentation how to use it and also the quickstart guide is available at **Turso Official** site.

- [Turso Quickstart (PHP)](https://docs.turso.tech/sdk/php/quickstart)
- [LibSQL Client PHP](https://github.com/darkterminal/libsql-client-php/tree/main/docs)
- [LibSQL PHP Extension](https://github.com/darkterminal/libsql-php-ext/tree/main/docs)

## Repository

{% embed https://github.com/darkterminal/libsql-client-php %}

## Want to Sponsoring me at GitHub?


![Want to Sponsoring me at GitHub](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2dtppp5m83lt2affq2b3.png)

{% cta https://github.com/sponsors/darkterminal %} 💖 Yes I want {% endcta %}