PHP code example of m3m0r7 / php-libusb

1. Go to this page and download the library: Download m3m0r7/php-libusb library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

m3m0r7 / php-libusb example snippets




sb = new \Libusb\Libusb(
    new \Libusb\LibusbHandle(
        new \Libusb\Connector\Libusb1_0(
            new \Libusb\Loader\FileLoader(
                // Set libusb header file here:
                '/path/to/libusb.h',

                // Set libusb library
                // Replace "so" to "dylib" if you use macOS:
                '/path/to/libusb-1.0.so',
            ),
        )
    ),
);

/**
 * @var \Libusb\Device\DeviceInterface $device
 */
foreach ($libusb->devices() as $device) {
    printf(
        "%s (serial: %s)\n",
        $device->descriptor()->product(),
        $device->descriptor()->serialNumber(),
    );
}



sb = new \Libusb\Libusb(
    new \Libusb\LibusbHandle(
        new \Libusb\Connector\Libusb1_0(
            new \Libusb\Loader\FileLoader(
                // Set libusb header file here:
                '/path/to/libusb.h',

                // Set libusb library
                // Replace "so" to "dylib" if you use macOS:
                '/path/to/libusb-1.0.so',
            ),
        )
    ),
);

/**
* @var \Libusb\Device\DeviceInterface $device
*/
[$device] = $libusb->devices(
    // Specify vendor id is here
    0x0101,

    // Specify product id is here
    0xabab,
);

$device
    // Set configuration
    ->setConfiguration(1)
    // Set claim interface
    ->setClaimInterface(0);

// Create bulk transfer endpoint
$bulkTransfer = $device->bulkTransferEndpoints();

// Send packet
$bulkTransfer
    ->send(
        (string) new \Libusb\Stream\Packet([0x01, 0x02, 0x03, 0x04]),
    );

// Received packet
$received = $bulkTransfer
    ->receive();


$ composer 

$ php test.php

USB Single Serial (serial: XXX)
YubiKey OTP+FIDO+CCID (serial: XXX)
USB2.0 Hub (serial: XXX)
USB3.1 Hub (serial: XXX)
4-Port USB 2.0 Hub (serial: XXX)
Logitech StreamCam (serial: XXX)