how to upload a file from php to amqp

被刻印的时光 ゝ 提交于 2019-12-22 10:06:19

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/17402024/how-to-upload-a-file-from-php-to-amqp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!