Using Guzzle to Interact with the Windows Azure Management API

Guzzle is a very simple abstraction over cURL which provides a great HTTP client for working with web services. This provides a great way of interacting with the Windows Azure Management API with PHP. In this example, I’m going to show how you can enable and disable an SSL Certificate in a Windows Azure Web Site using the Windows Azure Management API.

The majority of Windows Azure Web Site tasks can be automated using either the Windows Azure PowerShell cmdlets or the Windows Azure Cross Platform Command Line tools. This includes the ability to upload an SSL Certificate for your Web Site, however at this point there is no ability to bind the SSL Certificate to the Web Site itself. That’s where Guzzle and the Windows Azure Management API come into the picture.

You can do the following exercise from the Windows Azure Management Portal by following the instructions in the article Enable HTTPS for a Windows Azure web site. This entry is to demonstrate how you can achieve SSL Certificate binding as part of an automated environment script.

Automating SSL Certificate Upload to Azure Web Sites

Before we start interacting with the Management API let’s get the Windows Azure Web Site ready by adding the SSL Certificate to the Environment. This task is incredibly simple to accomplish using the Windows Azure Cross Platform tools.

SSL certificates can be uploaded only in Standard mode. Learn more about configuring custom domains.

azure site cert add [ssl-cert-path].pfx --key [cert-password] [web-site-name]

That’s it! The cert will now be added to the Windows Azure Web Site.

Create and Upload Export a Windows Azure Management Certificate

You could very well create and upload a management certificate of your own by creating one with OpenSSL and uploading it via the Windows Azure Management Portal (Settings > Management Certificates). However, there is a much easier way to achieve this without having to generate your own certificates, by using the Windows Azure Cross Platform Tools.

azure account cert export [--subscription]

The optional subscription parameter can be either the name of the subscription name or id from the azure account list command output.

Create a PHP Application with Guzzle to interact with the Windows Azure Management API

This is where things get fun! Let’s start by creating a composer file to acquire Guzzle.

Next we’ll want to take a look at the documentation for how to Enable or Disable SSL in Windows Azure Web Sites with the Management API. This gives us the information for the rest endpoint, http verb and xml/json payload to enable or disable the SSL Certificate.

Warning! At the time of writing SSL Certificates are only available for upload/binding if the Web Site is in Standard Mode. If the site isn’t in Standard Mode your requests will return with the status code HTTP 400 Bad Request.

Enable SSL JSON Playload

Disable SSL JSON Playload

PHP Source Code

To enable or disable SSL in a Web Site you will need to make a PUT request against the management API pass in your SubscriptionId, the webspace of the site, the site name and the Client Certificate. In Guzzle the request is constructed by the client, which you can pass an array of values into for replacement when you create the request by calling the PUT method.

Conclusion

This is how simple it is to make calls to the Windows Azure Management API from PHP using Guzzle. This blog post covers the usage of JSON for the request payload, however, there is a full example available as a Gist if you’d like to see how this can be done with XML.

Leave a Reply