Reputation: 53
i want to know... how can i send a file to a rabbitmq queue from php. i have gone through many examples most of them didnt work. below is a consumer producer example which is near to working. Below is a the publisher.php
<?php
require_once('../php-amqplib/amqp.inc');
include('../config.php');
$conn = new AMQPConnection(HOST, PORT, USER, PASS, VHOST);
$channel = $conn->channel();
$channel->exchange_declare('upload-pictures','direct', false, true, false);
$metadata = json_encode(array(
'image_id' => $argv[1],
'user_id' => $argv[2],
'image_path' => $argv[3]
));
$msg = new AMQPMessage($metadata, array('content_type' => 'text/plain','delivery_mode' => 2));
$channel->basic_publish($msg, 'upload-pictures');
$channel->close();
$conn->close();
?>
consumer.php
<?php
require_once('../php-amqplib/amqp.inc');
include('../config.php');
$conn = new AMQPConnection(HOST, PORT, USER, PASS,VHOST);
$channel = $conn->channel();
$queue = 'add-points';
$consumer_tag = 'consumer';
$channel->exchange_declare('upload-pictures','direct', false, true, false);
$channel->queue_declare('add-points',false, true, false, false);
$channel->queue_bind('add-points', 'upload-pictures');
$consumer = function($msg){};
$channel->basic_consume($queue,$consumer_tag,false,false,false,false,$consumer);
?>
according to the example provided in the rabbitmq manual, i need to run consumer(php consumer.php) first in one terminal and the publisher (php publisher.php 1 2 file/path.png) in another terminal, i will get a "Adding points to user: 2" message in the consumer terminal. iam not getting this message at all. can you suggest where iam going wrong
Upvotes: 3
Views: 3739
Reputation: 12859
IMPORTANT NOTICE:
AMQP protocol is not aimed for large content transfer (but it can do such job, I tried up to 128Mb for testing). I will suggest you to send path to file in message and process it in worker rather than sending whole file, while storing large message eat memory very fast and when it exceed disk will be used, which is pretty slow, comparing to RAM.
Anyway, if you still want to send file:
You have to send file content as regular message body. If it is a binary file, then encode it with base64 or whatever you want. There is no other way to do what you want. In your code I didn't see that you are reading file.
Upvotes: 6