A quick Internet search for Symfony Twitter API service will reveal Gonzalo Ayuso’s two year old article How to configure Symfony’s Service Container to use Twitter API (DZone mirror) for Guzzle 3.0. Unfortunately these config files are now out-of-date with a PSR-7 rewrite of Guzzle‘s HTTP Client in version 6.0 published one month ago.

These configurations only cover OAuth 1.0. The goal is to setup the service by Symfony’s dependency injection without any PHP required. Then you can invoke a Twitter REST API call within a controller action (or another service):

So here are the packages and YAML changes for each major revision.

Guzzle 4.*

For simplicity I’m showing these files in the /app/config/ directory although if your Twitter service is app-specific, place them under src/*Bundle/Resources/config/. After modifying composer.json required packages, run composer update. To make the Twitter service container configuration available in the Symfony application, import the below YAML.

Changes from 3.*

  • The namespace changed from Guzzle\Http to GuzzleHttp.
  • addSubscriber() method no longer exists so instead instantiate an Emitter and attach() your OAuth plugin to it.
  • Arguments to the Client constructor are now named properties passed as an associative array.

Guzzle 5.*

There were no major architectural changes in this version so you can keep the above configuration. However you must upgrade oauth-subscriber to 0.2.0 since 0.1.* is only compatible with 4.0.

Guzzle 6.*

Disappointingly, architecture changes in 6.0 have now broken most available OAuth plugin packages. You must upgrade oauth-subscriber to 0.3.0 for middleware support, then change the YAML configs likewise.

Note for user-defined parameters, base_url has become base_uri to match changes to the Client constructor’s named arguments. To define this argument, 6.0 uses UriInterface which doesn’t support placeholders so I’ve just placed the Twitter version directly in the base_uri configuration string.

Changes from 5.*

  • Middleware HandlerStack::create() must be used for the OAuth1 instance and passed into the Client constructor.

From my example Twitter REST API call at the start of this post, JSON handling must now be changed since 6.0 uses a PSR-7 Response without a json() method. Instead pass the result of the response’s getBody() into PHP library function json_decode().