Search

Programming
Programming

We provide developers and engineers the guides and tutorials they...

Content
Content

Content is king. It's not just about how you're going to write, b...

Events
Events

Looking for an engaging connection with thousands of other people...

Handling file Inputs in PHP with GuzzleHTTP

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 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

About author

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.

View all posts (110)
Your experience on this site will be improved by allowing cookies Cookie Policy