Setting up Symfony2 with Guzzle Part 2

Sorry for the late post. I’ve been unbelievably busy lately and haven’t had time to update my blog. Needless to say here is part 2 of setting up Symfony2 with Guzzle.

  1. Now that we have Guzzle set up we need set which web services are going to be called through Guzzle.  Navigate to app/config in Symfony2 and create a new file called webservices.xml.  It should look something like this: 
    You can use inheritance for your web services.  Each client is a new instance or different URL of an interface to the web service.  The class property tells you what class to instanciate whenever you are making the API calls through Symfony2.  Extends is pretty self explanitory.  It’s the parent class or parent properties.  In this case the access_key and the secret_key are inherited to each of the clients that use abstract.aws.
  2. The next step is to create a client that you can use.  Each of the clients defined in your webservices.xml that have a class must point to a valid PHP class.  You can either use a Client or you can extend Guzzle\Service\Client and create your own custom client.  Just make sure that you have a constructor
  3. Now you need to create some commands for your client otherwise you won’t be able to call anything.  Commands are basically the specific calls you want to make.  For example you might have a book REST service.  You might have different API calls to do your basic CRUD functions.  You will need to create a command for each of those functions.  When you create these, they must extend from Guzzle\Service\Command\AbstractCommand.  What you want to do here is set several properties.  This might be an ID field. /**
    * Set the data to post
    *
    * @param bookId
    *
    * @return string
    */
    public function setBookId($bookId)
    {
    return $this->set('bookId', $bookId);
    }
  4. The requirement for this class is that you have a build() function.  This function will build your URL for you and set the data you need.  You will set whether you are sending a POST, GET, PUT or any other HTTP verbs.  In mine I have a custom function that will append the end of the URL and set the correct ID.protected function build()
    {
    $data = $this->get('bookId');
    $this->request = $this->client->getBookRequest('book/'.$data,RequestInterface::GET );
    }
  5. Now all we have to do is call Guzzle and everything will work!  Create a builder object and get it through Symfony2 service.  Use the builder to get the client you want.  This will be the client name you specified in webservices.xml above.  Then specify which command you want to execute.  Then execute the request and Guzzle will handle everything else for you.$builder = $this->get('guzzle.service_builder');
    $client = $builder['book'];
    $command = $client->getCommand('GetBook');
    $command->setBookId(1);
    $response = $client->execute($command);
  6. Now one thing to note is that you can return a custom object if you need to.  This will keep your code cleaner.  In order to do that you must definte a process() function and a getResult() function in your command.  In this example I want my response to be an array so I convert the response to an array and I’m done! /**
    * {@inheritdoc}
    */
    protected function process()
    {
    $this->result = json_decode($this->getResponse()->getBody(),true);
    }

    /**
    *
    *
    * @return array
    */
    public function getResult()
    {
    return parent::getResult();
    }

  7. There you go.  If you have any questions feel free to leave a comment or email me directly.

Setting up Symfony2 with Guzzle Part 1

So in working with my latest project I needed to make some REST calls through PHP.  After doing a bit of research I decided to use Guzzle http://guzzlephp.org/.  The reason why I decided to use Guzzle is that it’s simple, easily extensible, and has a testing framework.  So I’m going to make a few assumptions in this tutorial.  I’m assuming that you’ve already got Symfony2 up and running on your machine.  If not, go to http://symfony.com/ and follow the Get Started link to get up and running.  So now without further ado!

  1. Navigate to your Symfony2 location and run this:
    git submodule add git://github.com/guzzle/guzzle.git vendor/guzzle
    git submodule add git://github.com/ddeboer/GuzzleBundler vendor/bundles/Ddeboer/GuzzleBundle
  2. Now go to your Autoload.php (app/autoload.php) file and register namespaces.
    $loader->registerNamespaces(array(// ...
    'Guzzle' => __DIR__.'/../vendor/guzzle/src',
    'Ddeboer' => __DIR__.'/../vendor/bundles',
    // ...
    ));
  3. Add the GuzzleBundle to your application kernel. (app/AppKernel.php)
    public function registerBundles()
    {
    // ...
    new Ddeboer\GuzzleBundle\DdeboerGuzzleBundle(),
    // ...
    }
  4. The last step you need to do is set up your Guzzle service builder. (app/config/config.yml)

    ddeboer_guzzle:
    service_builder:
    configuration_file: "%kernel.root_dir%/config/webservices.xml"
    cache:
    adapter: doctrine
    driver: apc
    I had some problems with the cache section so you can remove that if you run into any problems.

Now you’re all set up to start sending REST requests through Guzzle in your Symfony2 application.  Next blog post I’ll show you how to use Guzzle’s extensibility (that should definitely be a word) to turn your REST calls into an API that’s super easy to use.