'Is it possible to proxy a stream with Guzzle?

On my cron batch I am getting images from one server and posting them to another server.

Currently I am doing this:

  1. GET an image with guzzle and store in on disk inside /tmp folder
  2. POST the image to another server using multipart from the /tmp folder.

I would like to know if there is any way to avoid storing the image in the /tmp folder on the server, so insetad to something like:

  • GET image with guzzle as a stream and POST the stream to another server without saving it on disk.

I tried using 'stream' => true with guzzle (https://docs.guzzlephp.org/en/stable/request-options.html#stream) for getting the image and posting the stream to another server, however the other server is responding with a 400.

I dont have control of the other server, but using the multipart POST with fopen('/tmp/image.png', 'r') works.

Is there any way to accomplish proxying a stream to another server?

This is my code currently that returns 400:

// Frist GET
$image = Http::withOptions([
  'stream' => true
])->get($filename);

$multipart[] = [
  'name' => $name,
  'contents' => $image
];
// Then POST
Http::withOptions([
  'query' => $query,
])
->attach($multipart)
->post(env('ENDPOINT_URL'));

This is previous code that works storing image to disk:

// Frist GET
$image = Http::get($filename);
file_put_contents("/tmp/$filename", $image);

$multipart[] = [
  'name' => $name,
  'contents' => fopen("/tmp/$filename", 'r')
];
// Then POST
Http::withOptions([
  'query' => $query,
])
->attach($multipart)
->post(env('ENDPOINT_URL'));


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source