PHP code example of michyaraque / g-php

1. Go to this page and download the library: Download michyaraque/g-php 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/ */

    

michyaraque / g-php example snippets




PHP\Extension\{Extension, ExtensionInfo, HostInfo};
use GPHP\Protocol\{HMessage, HPacket};
use GPHP\Packets\Outgoing;
use GPHP\Packets\Incoming;

$extInfo = new ExtensionInfo(
    title: "My Extension",
    description: "A cool PHP extension",
    version: "1.0",
    author: "You"
);

$ext = new Extension($extInfo);

$ext->onConnect(function (HostInfo $hostInfo) {
    echo "Connected to " . $hostInfo->host . "\n";
});

$ext->run();

$ext->intercept(Incoming::Chat, function (HMessage $msg) {
    $packet = $msg->getPacket();
    $text = $packet->readString();
    
    echo "User said: $text\n";
});

$packet = new HPacket(Outgoing::Chat);
$packet->appendString("Hello World!");
$packet->appendInt(0);
$packet->appendInt(-1);

$ext->sendPacket($packet);

// Reading
$val = $packet->readInt();
$str = $packet->readString();
[$x, $y] = $packet->read('ii');

// Writing
$packet->appendInt(123);
$packet->appendString("foo");
$packet->appendBool(true);

use GPHP\Util\Scheduler;

Scheduler::run(function() use ($ext) {
    for ($i = 0; $i < 10; $i++) {
        // Send a packet
        $ext->sendPacket(new HPacket(Outgoing::AvatarExpression)->appendInt(1));
        
        // Wait 1 second (non-blocking)
        $start = microtime(true);
        while (microtime(true) - $start < 1.0) {
            \Fiber::suspend();
        }
    }
});
bash
php extension.php