PHP code example of cclilshy / p-ripple-core

1. Go to this page and download the library: Download cclilshy/p-ripple-core 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/ */

    

cclilshy / p-ripple-core example snippets


\Co\async(static function (){
    \Co\sleep(1);
    
    echo 'Coroutine 1' , PHP_EOL;
});

\Co\async(static function (){
    \Co\sleep(1);
    
    echo 'Coroutine 2' , PHP_EOL;
});

\Co\async(static function (){
    \Co\sleep(1);
    
    echo 'Coroutine 3' , PHP_EOL;
});

\Co\sleep(2); // Wait for all coroutines to complete execution

use GuzzleHttp\Exception\GuzzleException;
use Psc\Utils\Output;

$client = Co\Plugin::Guzzle()->newClient();

for ($i = 0; $i < 10; $i++) {
    \Co\async(static function (){
        try {
            $response = $client->get('https://www.google.com/');
            echo $response->getStatusCode(), \PHP_EOL;
        } catch (GuzzleException $e) {
            Output::exception($e);
        }
    });
}

\Co\wait();

use GuzzleHttp\Exception\GuzzleException;
use Psc\Core\Http\Client\Capture\ServerSentEvents;

if (!$key = $argv[1] ?? null) {
    echo 'Please enter the key' .\PHP_EOL;
    exit(1);
}

//Create interceptor
$sse = new ServerSentEvents();
$sse->onEvent(function ($event) {
    \var_dump($event);
});

// Refer to the documentation and ask questions
$client = Co\Plugin::Guzzle()->newClient();
$header = [];
$header['Content-Type'] = 'application/json';
$header['Accept'] = 'text/event-stream';
$header['Authorization'] = 'Bearer ' . $key;
$body = [
    'model' => 'qwen-max',
    'input' => [
        'messages' => [
            ['role' => 'system', 'content' => 'Your name is ripple knowledge base'],
            ['role' => 'user', 'content' => 'Who are you?'],
        ],
    ],
];

try {
    $response = $client->post('https://dashscope.aliyuncs.com/api/v1/services/aigc/text-generation/generation', [
        'headers' => $header,
        'body' => \json_encode($body, \JSON_UNESCAPED_UNICODE),

        'timeout' => 10,

        //Injection interceptor
        'capture_write' => $sse->getWriteCapture(),
        'capture_read' => $sse->getReadCapture(),
    ]);
} catch (GuzzleException $e) {
    echo $e->getMessage();
}

use Psc\Core\Http\Server\Chunk;
use Psc\Core\Http\Server\Request;
use Psc\Core\Http\Server\Response;

$server = Co\Net::Http()->server('http://127.0.0.1:8008', \stream_context_create([
    'socket' => [
        'so_reuseport' => true,
        'so_reuseaddr' => true,
    ]
]));

$server->onRequest(static function (Request $request, Response $response) {
    switch (\trim($request->getRequestUri(), '/')) {
        case 'sse':
            $response->headers->set('Transfer-Encoding', 'chunked');
            $generator = static function () {
                foreach (\range(1, 10) as $i) {
                    Co\sleep(0.1);
                    yield Chunk::event('message', \json_encode(['id' => $i, 'content' => 'content']));
                }
                yield '';
            };
            $response->setContent($generator());
            $response->respond();
            break;
        default:
            $response->setContent('Hello, World!')->respond();
            break;
    }
});

$server->listen();

Co\wait();