Language:

Search

Handling file Inputs in PHP with GuzzleHTTP

  • Share this:
Handling file Inputs in PHP with GuzzleHTTP

You may know how to handle file inputs in PHP with cURL, as we've discussed earlier.

But GuzzleHTTP Client comes for the rescue to overcome messy code and get things done in more cleaner manner.

You may also like

Snippet

$file      = Input::file( 'file_name' );
$imagePath = $file->getPathName();
$tmp       = tempnam( sys_get_temp_dir(), 'php' );

$client = new Client();
$result = $client->post( 'mydomain.com/api/endpoint', [

   'form_params' =>
      [
         'file_name'     => $tmp,
         'name'     => $file,
         'contents' => file_put_contents( $tmp, file_get_contents( $imagePath ) ),
         'filename' => $file
      ],
   'headers'     =>
      [
         'authorization' => 'Bearer ' . $token
      ]
] );

$results = \GuzzleHttp\json_decode( $result->getBody() );

Conclusion

Easy peasy ? Ever wondered on handling multiple set of files with GuzzleHTTP over an API call? We've got you covered, do give it a read at

Try this yourself & leave us a comment below if you get stuck somewhere in between. Do follow us on Twitter.

Usama Muneer

Usama Muneer

A web enthusiastic, self-motivated & detail-oriented professional Full-Stack Web Developer from Karachi, Pakistan with experience in developing applications using JavaScript, WordPress & Laravel specifically. Loves to write on different web technologies with an equally useful skill to make some sense out of it.