I trying to run a task in PHP using gearman I created 2 scripts: client.php
<?
$mail = array(
'to' => 'test@gmail.com',
'subject' => Hi',
'body' => 'Test message',
);
# Connect to Gearman server
$client = new GearmanClient();
$client->addServer('127.0.0.1', '4730');
# Send message
$client->doBackground('sendmail', json_encode($mail));
worker.php
<?php
$worker = new GearmanWorker();
$worker->addServer();
$worker->addFunction('sendmail', 'send_mail');
while (1)
{
$worker->work();
if ($worker->returnCode() != GEARMAN_SUCCESS) break;
}
function send_mail($job)
{
$workload = $job->workload();
$data = json_decode($workload, true);
mail($data['to'], $data['subject'], $data['body']);
}
when I run my worker from comand line : php worker.php &
and run my client.php file I get the below error:
GearmanClient::doBackground(): send_packet(GEARMAN_COULD_NOT_CONNECT) Failed to send server-options packet -> libgearman/connection.cc:485
Any help please?
Thanks
Try to change this:
$client->addServer('127.0.0.1', '4730');
to
$client->addServer();
If still no working, try to take a look in the get started page of this tutorial on php. It worked fine for me
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.