Thursday, 11 April 2013

Query Azure Configuration Setting from PHP

I'm quite excited about the fact that Microsoft are starting to embrace PHP as a standard programming language for web apps but as with many new things, the help is sparse and things seem to change between SDK releases. The first of my posts here is the last thing I had to do on my project and involves querying the value of an Azure configuration setting from PHP in code.

Why might you do this? In my case, I have connection string data and encryption passwords and I don't want them to swim around in code. In my case, by putting them into an Azure configuration setting, not only could they be changed on the fly, but they are hidden from developers who are either maintaining or reviewing your code and who you don't want to see these values.

I will cover other PHP in future posts but assuming you have a PHP app up and running on azure and you simply want to retrieve a value from the Configuration Settings, the first thing to note is that currently (April 2013(, the latest github version of the Azure PHP SDK does NOT support the RoleEnvironment, which is pretty rubbish. It as a class and a function but these throw exceptions, both locally and on the cloud. The good news is that you can still obtain the older SDK from http://phpazure.codeplex.com/ and this works just fine. Currrently, I am only using this for configuration settings so obviously other things might not work as well.

  1. Download the zip file from codeplex and unpack the library folder to a relevant place in your web root. I am using Yii so I copied it into protected/vendors/microsoft.
  2. Use whatever you need to in order to locate this package from within your app, again for Yii, i had to use the line: Yii::import('application.vendors.*'); (note that the word application is required here even though it is not part of the path). If you have a vanilla PHP app, you will not need this.
  3. require_once('microsoft/windowsazure/WindowsAzure/RoleEnvironment.php'); (or whatever the path is!)
  4. Access the config setting thus: Microsoft_WindowsAzure_RoleEnvironment::getConfigurationSettingValue("SettingKey");
That's it! You can deploy away and this is all the work required. This also works locally so you can test/debug before you deploy.

A quick note on adding these settings. Normally, you would do this in Visual Studio (and I don't know if this is possible just for the azure project in VS when using PHP) but basically, you need to edit the csdef file and add a section called and this will contain one or more entries. The values are not set here. The ConfigurationSettings element is directly under the WebRole element. You then edit the cscfg files for each of your local and cloud configurations. These should already contain an empty ConfigurationSettings element. Open this out and add the same Setting elements as in the csdef but this time add values for them (value="something"). Naturally, you can use different values for local and cloud.
Post a Comment