Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Post a blob using Guzzle

Is it possible to post a blob using Guzzle? The only methods I've been able to find are using @filename to upload a local file. The file is stored as a blob in a MySQL database and I would like to upload it to an api as a post field without the redundancy of saving the blob to disk (and the permissions/path issues that come with it), uploading @filename, and then unlinking the file. Here is the code I have that is working for everything but the blob. I need the 'file' field to save the data as a blob.

$data = array(
    'first_name' => $fname,
    'last_name' => $lname,
    'email' => $email,
    'partner_key' => 'qwerty',
    'secret_key' => 'qwerty',
    'file' => $fileblob
);

$curl = new \GuzzleHttp\Client();
return $curl->post('https://www.api.com',['verify'=>false,'body'=>$data])

The goal being to replace the existing cURL code using Guzzle:

'file' => "@".$localfile.";type=".mime_content_type($localfile)
like image 725
user2247281 Avatar asked Sep 27 '22 16:09

user2247281


1 Answers

I found the solution. Hopefully this helps others in the future:

$data = array(
    'first_name' => $fname,
    'last_name' => $lname,
    'email' => $email,
    'partner_key' => 'qwerty',
    'secret_key' => 'qwerty',
    'file' => new \GuzzleHttp\Post\PostFile('filename', $fileblob)
);

$curl = new \GuzzleHttp\Client();
return $curl->post('https://www.api.com',['verify'=>false,'body'=>$data])
like image 180
user2247281 Avatar answered Oct 02 '22 15:10

user2247281