I Build libSQL Client for PHP

I Build libSQL Client for PHP

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.

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:

{
“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)
LibSQL Client PHP
LibSQL PHP Extension

Repository


darkterminal
/
libsql-client-php

Turso + PHP SDK for Native LibSQL

Turso + PHP

SQLite for Production. Powered by libSQL.

Turso ·
Quickstart ·
Examples ·
Docs ·
Discord ·
Blog & Tutorials

Documentation

Turso Quickstart — Learn how create and connect your first database.

SDK Quickstart — Learn how to install and execute queries using the libSQL client.

SDK Reference — Dive deeper with the libSQL SDK reference and examples.

What is Turso?

Turso is a SQLite-compatible database built on libSQL, the Open Contribution fork of SQLite. It enables scaling to hundreds of thousands of databases per organization and supports replication to any location, including your own servers, for microsecond-latency access.

Learn more about what you can do with Turso:

Embedded Replicas
Platform API
Data Edge
Branching
Point-in-Time Recovery
Scale to Zero

Want to Sponsoring me at GitHub?

💖 Yes I want

Leave a Reply

Your email address will not be published. Required fields are marked *