GuzzleBundle: easily consume APIs
What is Guzzle ?
Guzzle is a PHP HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services :
- Manages things like persistent connections, represents query strings as collections, simplifies sending streaming POST requests with fields and files, and abstracts away the underlying HTTP transport layer;
- Can send both synchronous and asynchronous requests using the same interface without requiring a dependency on a specific event loop;
- Pluggable HTTP handlers allows Guzzle to integrate with any method you choose for sending HTTP requests over the wire (e.g., cURL, sockets, PHP’s stream wrapper, non-blocking event loops like React, etc.);
- Guzzle makes it so that you no longer need to fool around with cURL options, stream contexts, or sockets.
GuzzleBundle by MISD Service Development
A) What makes that Bundle interesting ?
GuzzleBundle allows you to simply consume APIs. You declare them as services then you can call them as simply as any services. The Bundle can also work with JMSSerializer wich allow you to transform the response body into usable object reprensted in your application as if those objects would have come from your database. The Bundle works for all common HTTP methods (GET, POST, PUT, DELETE, etc.). The only problem I could not solve was the upload of files from POST. I think that it could be due to the version of Guzzle that the Bundle uses.
B) Installation, configuration, usage
I usually describe the process of installation and usage of Bundle that I present, but since the documentation is very well done and very complete, I guess it is more efficient that you directly use it. As I have mentioned before, you can use (or not) GuzzleBundle with JMSSerializer. In my experience, the two of them work very well together and if you have already used JMSSerializer with FOSRestBundle (Bundle to expose Rest API), you will see that the process is pretty much the same.
Limitation of GuzzleBundle
The main limitation of the Bundle is due to the fact that it is currently working with the version 3 of Guzzle (the last version is 5.0). The differences bewteen the two versions are available on the CHANGELOG.md. Another problem that I had to face, is that GuzzleBundle raises an exception for response code 204 as if the code is an HTTP error. Analysing the code, it seems to me that only 200 was understand as a success for the Bundle. If like I do (if like me, you use…) you use the 204 (for example when you want a list of users an none match you search parameters), you will have to fork the Bundle to fix this yourself. The good news about this, is that people from MISD know about that particular issue and have accepted to fix it in their next release.
- Guzzle documentation
- Github repository for GuzzleBundle
- Github repository for JMSSerializerBundle
- Github repository for FOSRestBundle