PHPackages                             php-io-extensions/gpio - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. php-io-extensions/gpio

ActivePhp-ext[Utility &amp; Helpers](/categories/utility)

php-io-extensions/gpio
======================

PHP-Controllable GPIO Extension

v0.1.1(4w ago)08MITCPHP &gt;=8.3CI passing

Since Apr 17Pushed 4w agoCompare

[ Source](https://github.com/php-io-extensions/gpio)[ Packagist](https://packagist.org/packages/php-io-extensions/gpio)[ RSS](/packages/php-io-extensions-gpio/feed)WikiDiscussions main Synced 1w ago

READMEChangelogDependenciesVersions (3)Used By (0)

GPIO
====

[](#gpio)

[![PHP](https://camo.githubusercontent.com/91e2ff786d2fba1edf015025006e0156a071320b3662eaf2c50f39d4bb4b2369/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344382e332d626c7565)](https://www.php.net)[![Zephir](https://camo.githubusercontent.com/5add3cf7b546a9aa97c8f3d9f5caeda8c9fdf1bd0f90151d5b3e8e25e591ed83/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7a65706869722d302e31392532422d6f72616e6765)](https://docs.zephir-lang.com/latest/en/installation)[![Platform](https://camo.githubusercontent.com/df6c8370b77128ba402b779fe0b9b6c86d11e8de91179c057e3c8e7a66c6ae68/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f706c6174666f726d2d6c696e75782532302d6c6967687467726579)](https://www.kernel.org)

PHP-controllable Linux GPIO extension built with Zephir.

The GPIO extension is an extension for PHP running on supported Linux single-board computers. It allows PHP to use GPIO character devices for sensors, interrupts, LEDs, relays, fans, and similar hardware.

Requirements
------------

[](#requirements)

- PHP 8.3+ with development headers (`php-dev` / `php-devel`)
- [FD extension](https://github.com/DeptOfScrapyardRobotics/FD) — provides raw integer file descriptors via `Fd\FD::open()`
- C build toolchain for PHP extensions (`gcc`, `make`, `phpize`, `autoconf`)
- Supported Linux SBC platforms:
    - Raspberry Pi boards running Debian Trixie
    - NVIDIA Jetson Orin family boards running JetPack 6
- Linux kernel with GPIO character device support (`/dev/gpiochipN`)
- GPIO v2 userspace API — kernel 5.10+

Installation
------------

[](#installation)

### Automatic Installation

[](#automatic-installation)

[PHP PIE](https://github.com/php/pie) is the official PHP extension installer. It builds and enables the extension from the generated C sources in `ext/`.

```
pie install php-io-extensions/gpio
```

Automatic installation requires PIE and the C toolchain for building PHP extensions. Zephir is not required for automatic installation.

Install the [FD extension](https://github.com/DeptOfScrapyardRobotics/FD) first — GPIO requires raw integer file descriptors that PHP's stream layer cannot provide.

### Manual Installation

[](#manual-installation)

Manual installation builds the extension from the Zephir source and requires [Zephir](https://docs.zephir-lang.com/latest/en/installation) 0.19+.

Install Zephir if you haven't already:

```
composer global require phalcon/zephir
```

Use the installer for your target platform:

```
git clone https://github.com/DeptOfScrapyardRobotics/GPIO
cd GPIO

# Raspberry Pi / Debian Trixie
bash install-debian-trixie.sh

# NVIDIA Jetson Orin / JetPack 6
bash install-jetpack6.sh
```

The installer handles the full workflow: clean → build → copy `.so` → write `30-gpio.ini` into detected `conf.d` directories → verify `php -m` → reload php-fpm if present.

To use a specific Zephir binary:

```
ZEPHIR_BIN=/path/to/zephir bash install-debian-trixie.sh
```

API
---

[](#api)

All methods are static. File descriptors are plain integers. Use the [FD extension](https://github.com/DeptOfScrapyardRobotics/FD) to open and close them — PHP's stream-based `fopen` does not produce the raw integer FDs the kernel ioctls require.

```
use Fd\FD;

$fd = FD::open('/dev/gpiochip0', 2); // O_RDWR = 2
// ... use $fd with GPIOChip methods ...
FD::close($fd);
```

### `Gpio\GPIOChip`

[](#gpiogpiochip)

Operations on a GPIO chip file descriptor (e.g. an open `/dev/gpiochip0`).

---

#### `getLine(int $fd, int $line, int $flags, int $event_buffer_size = 0, string $consumer = "php-ext-ioctl"): int`

[](#getlineint-fd-int-line-int-flags-int-event_buffer_size--0-string-consumer--php-ext-ioctl-int)

Requests a GPIO line and returns a new **line file descriptor**, or `-1` on failure.

ParameterDescription`$fd`Chip file descriptor`$line`Line offset on the chip`$flags``GPIO_V2_LINE_FLAG_*` bitmask (see [linux/gpio.h](https://elixir.bootlin.com/linux/latest/source/include/uapi/linux/gpio.h))`$event_buffer_size`Kernel-side edge event buffer size; `0` for default`$consumer`Label shown in `/sys/kernel/debug/gpio`---

#### `chipInfo(int $fd): array`

[](#chipinfoint-fd-array)

Returns chip metadata, or `[]` on failure.

```
['name' => string, 'label' => string, 'lines' => int]
```

---

#### `lineInfo(int $fd, int $offset): array`

[](#lineinfoint-fd-int-offset-array)

Returns line metadata, or `[]` on failure.

```
['name' => string, 'consumer' => string, 'offset' => int, 'num_attrs' => int, 'flags' => int]
```

---

#### `lineInfoWatch(int $fd, int $offset): int`

[](#lineinfowatchint-fd-int-offset-int)

Subscribes to line-info change events for `$offset` on the chip FD. Returns `0` on success, `-1` on failure. Read events with `lineInfoChanged()`.

---

#### `lineInfoUnwatch(int $fd, int $offset): int`

[](#lineinfounwatchint-fd-int-offset-int)

Cancels a watch registered with `lineInfoWatch()`. Returns `0` on success, `-1` on failure.

---

#### `lineInfoChanged(int $fd): array`

[](#lineinfochangedint-fd-array)

Blocking read of one `gpio_v2_line_info_changed` event from the chip FD. Returns `[]` if no event or on error.

```
[
    'info'         => ['name' => string, 'consumer' => string, 'offset' => int, 'num_attrs' => int, 'flags' => int],
    'timestamp_ns' => int,
    'event_type'   => int,  // 1 = REQUESTED, 2 = RELEASED, 3 = CONFIG_CHANGED
]
```

---

### `Gpio\GPIOLine`

[](#gpiogpioline)

Operations on a line file descriptor returned by `GPIOChip::getLine()`.

---

#### `getValues(int $fd): int`

[](#getvaluesint-fd-int)

Returns `1` (HIGH), `0` (LOW), or `-1` on failure.

---

#### `setValues(int $fd, int $value): bool`

[](#setvaluesint-fd-int-value-bool)

Drives the line HIGH (`$value != 0`) or LOW (`$value == 0`). Returns `true` on success.

---

#### `readEdgeEvents(int $fd): array`

[](#readedgeeventsint-fd-array)

Blocking read of one edge event from a line configured with `EDGE_RISING` and/or `EDGE_FALLING`. Returns `[]` if no event or on error.

```
[
    'timestamp_ns' => int,
    'id'           => int,  // 1 = RISING_EDGE, 2 = FALLING_EDGE
    'offset'       => int,
    'seqno'        => int,
    'line_seqno'   => int,
]
```

---

Usage
-----

[](#usage)

```
use Fd\FD;
use Gpio\GPIOChip;
use Gpio\GPIOLine;
```

GPIO v2 flag values for `getLine`. These map directly to `linux/gpio.h` — PHP does not define them, so pass the values directly or define your own constants:

ConstantValueDescription`GPIO_V2_LINE_FLAG_INPUT``0x04`Configure line as input`GPIO_V2_LINE_FLAG_OUTPUT``0x08`Configure line as output`GPIO_V2_LINE_FLAG_EDGE_RISING``0x10`Detect rising edges`GPIO_V2_LINE_FLAG_EDGE_FALLING``0x20`Detect falling edges`GPIO_V2_LINE_FLAG_BIAS_PULL_UP``0x100`Enable pull-up resistor`GPIO_V2_LINE_FLAG_BIAS_PULL_DOWN``0x200`Enable pull-down resistorCombine with `|` as needed.

---

### Digital output (LED, relay, fan)

[](#digital-output-led-relay-fan)

```
$fd = FD::open('/dev/gpiochip4', 2); // O_RDWR

$lineFd = GPIOChip::getLine($fd, 6, 0x08, 0, 'my-app'); // OUTPUT
if ($lineFd < 0) {
    throw new RuntimeException('Failed to request line');
}

GPIOLine::setValues($lineFd, 1); // HIGH — device on
sleep(1);
GPIOLine::setValues($lineFd, 0); // LOW  — device off

FD::close($lineFd);
FD::close($fd);
```

### Digital input (button, sensor)

[](#digital-input-button-sensor)

```
$fd = FD::open('/dev/gpiochip0', 2);

$lineFd = GPIOChip::getLine($fd, 27, 0x04, 0, 'my-app'); // INPUT

$state = GPIOLine::getValues($lineFd);
echo $state === 1 ? "HIGH\n" : "LOW\n";

FD::close($lineFd);
FD::close($fd);
```

### Edge detection (interrupt)

[](#edge-detection-interrupt)

```
// INPUT | EDGE_RISING | EDGE_FALLING
$fd = FD::open('/dev/gpiochip0', 2);

$lineFd = GPIOChip::getLine($fd, 27, 0x04 | 0x10 | 0x20, 64, 'my-app');

while (true) {
    $event = GPIOLine::readEdgeEvents($lineFd);
    if ($event !== []) {
        $edge = $event['id'] === 1 ? 'RISING' : 'FALLING';
        echo "{$edge} on offset {$event['offset']} at {$event['timestamp_ns']} ns\n";
    }
}
```

### Query chip and line metadata

[](#query-chip-and-line-metadata)

```
$fd = FD::open('/dev/gpiochip0', 2);

$chip = GPIOChip::chipInfo($fd);
echo "Chip: {$chip['name']} — {$chip['label']} ({$chip['lines']} lines)\n";

$line = GPIOChip::lineInfo($fd, 6);
echo "Line 6: consumer={$line['consumer']} flags={$line['flags']}\n";

FD::close($fd);
```

License
-------

[](#license)

MIT License. See [LICENSE](LICENSE).

###  Health Score

38

—

LowBetter than 83% of packages

Maintenance94

Actively maintained with recent releases

Popularity6

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity40

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~25 days

Total

2

Last Release

28d ago

PHP version history (2 changes)v0.1.0PHP &gt;=8.0

v0.1.1PHP &gt;=8.3

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/10563160?v=4)[Angel Gonzalez](/maintainers/projectsaturnstudios)[@projectsaturnstudios](https://github.com/projectsaturnstudios)

---

Top Contributors

[![projectsaturnstudios](https://avatars.githubusercontent.com/u/10563160?v=4)](https://github.com/projectsaturnstudios "projectsaturnstudios (3 commits)")

### Embed Badge

![Health badge](/badges/php-io-extensions-gpio/health.svg)

```
[![Health](https://phpackages.com/badges/php-io-extensions-gpio/health.svg)](https://phpackages.com/packages/php-io-extensions-gpio)
```

###  Alternatives

[sskaje/mqtt

sskaje's MQTT Class

8769.9k1](/packages/sskaje-mqtt)[zfr/hydrator

Zend Framework 3 prototype for a better hydrator component

1512.0k](/packages/zfr-hydrator)[agence-adeliom/easy-editor-bundle

A Symfony bundle for EasyAdmin that provide a flexible content editor

1111.3k1](/packages/agence-adeliom-easy-editor-bundle)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
