PHP code example of initphp / queue

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

    

initphp / queue example snippets



namespace App\Jobs;

use InitPHP\Queue\Job;

class MailJob extends Job
{
    protected string $channel = 'mailChannel';
    
    protected string $queue = 'mailQueue';
    
    public function handle(): bool
    {
        $payload = $this->getPayload();
        try {
            if (mail($payload['to'], $payload['subject'])) {
                return true;
            } else {
                return false;;
            }
        } catch (\Throwable $e) {
            return false;
        }
    }
}

$adapter = new \InitPHP\Queue\Adapters\RabbitMQAdapter('127.0.0.1', 5267, 'guest', 'guest');

$job = new App\Jobs\MailJob($adapter);

// Add Queue Job
$job->push([
    'to'        => '[email protected]',
    'subject'   => 'Subject Mail',
]);

$adapter = new \InitPHP\Queue\Adapters\RabbitMQAdapter('127.0.0.1', 5267, 'guest', 'guest');

$adapter->handle('mailChannel', 'mailQueue');

$adapter->close();

$pdo = new PDO('mysql:host=localhost;port=3307;dbname=queue_db', 'root', 'root');
$adapter = new \InitPHP\Queue\Adapters\PDOAdapter($pdo, 'queue');

$adapter = new \InitPHP\Queue\Adapters\RabbitMQAdapter('127.0.0.1', 5267, 'guest', 'guest');

php consumer.php

composer