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.
- 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. - 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
- 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);
} - 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 );
} - 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); - 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();
} - There you go. If you have any questions feel free to leave a comment or email me directly.