Source for file Client.php
Documentation is available at Client.php
* Copyright (c) 2009 - 2011, RealDolmen
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of RealDolmen nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY RealDolmen ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL RealDolmen BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
* @package Microsoft_WindowsAzure
* @copyright Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
* @license http://phpazure.codeplex.com/license
* @version $Id: Storage.php 51671 2010-09-30 08:33:45Z unknown $
* @see Microsoft_AutoLoader
require_once dirname(__FILE__ ) . '/../../AutoLoader.php';
* @package Microsoft_SqlAzure
* @copyright Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
* @license http://phpazure.codeplex.com/license
const URL_MANAGEMENT = "https://management.database.windows.net:8443";
const OP_OPERATIONS = "operations";
const OP_SERVERS = "servers";
const OP_FIREWALLRULES = "firewallrules";
* Management certificate path (.PEM)
* Management certificate passphrase
* Microsoft_Http_Client channel used for communication with REST services
* @var Microsoft_Http_Client
* Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract instance
* @var Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract
* Returns the last request ID
* Creates a new Microsoft_SqlAzure_Management_Client instance
* @param string $subscriptionId Subscription ID
* @param string $certificatePath Management certificate path (.PEM)
* @param string $certificatePassphrase Management certificate passphrase
* @param Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy Retry policy to use when making requests
Microsoft_WindowsAzure_RetryPolicy_RetryPolicyAbstract $retryPolicy = null
// Setup default Microsoft_Http_Client channel
'adapter' => 'Microsoft_Http_Client_Adapter_Socket',
// Set cURL options if cURL is used afterwards
$options['curloptions'] = array(
CURLOPT_FOLLOWLOCATION => true,
* Set the HTTP client channel to use
* @param Microsoft_Http_Client_Adapter_Interface|string$adapterInstance Adapter instance or adapter class name.
* Retrieve HTTP client channel
* @return Microsoft_Http_Client_Adapter_Interface
* Returns the Windows Azure subscription ID
* Returns the last request ID.
* Get base URL for creating requests
* Perform request using Microsoft_Http_Client channel
* @param string $path Path
* @param array $query Query parameters
* @param string $httpVerb HTTP verb the request will use
* @param array $headers x-ms headers to add
* @param mixed $rawData Optional RAW HTTP data to be sent over the wire
* @return Microsoft_Http_Response
$httpVerb = Microsoft_Http_Client::GET,
if (strpos($path, '/') !== 0) {
// Ensure cUrl will also work correctly:
// - disable Content-Type if required
// - disable Expect: 100 Continue
if (!isset ($headers["Content-Type"])) {
$headers["Content-Type"] = '';
//$headers["Expect"] = '';
// Generate URL and sign request
$requestHeaders = $headers;
foreach ($query as $key => $value) {
$requestUrl .= $queryString;
* Parse result from Microsoft_Http_Response
* @param Microsoft_Http_Response $response Response from HTTP call
* @throws Microsoft_WindowsAzure_Exception
protected function _parseResponse(Microsoft_Http_Response $response = null)
$namespaces = array_merge($xml->getNamespaces(true), $xml->getDocNamespaces(true));
// Register all namespace prefixes
foreach ($namespaces as $prefix => $ns) {
$xml->registerXPathNamespace($prefix, $ns);
* Get error message from Microsoft_Http_Response
* @param Microsoft_Http_Response $response Repsonse
* @param string $alternativeError Alternative error message
protected function _getErrorMessage(Microsoft_Http_Response $response, $alternativeError = 'Unknown error.')
if ($response && $response->Message) {
return (string) $response->Message;
return $alternativeError;
* The Create Server operation adds a new SQL Azure server to a subscription.
* @param string $administratorLogin Administrator login.
* @param string $administratorPassword Administrator password.
* @param string $location Location of the server.
* @return Microsoft_SqlAzure_Management_ServerInstance Server information.
* @throws Microsoft_SqlAzure_Management_Exception
public function createServer($administratorLogin, $administratorPassword, $location)
if ($administratorLogin == '' || is_null($administratorLogin)) {
if ($administratorPassword == '' || is_null($administratorPassword)) {
array('Content-Type' => 'application/xml; charset=utf-8'),
'<Server xmlns="http://schemas.microsoft.com/sqlazure/2010/12/"><AdministratorLogin>' . $administratorLogin . '</AdministratorLogin><AdministratorLoginPassword>' . $administratorPassword . '</AdministratorLoginPassword><Location>' . $location . '</Location></Server>');
if ($response->isSuccessful()) {
* The Get Servers operation enumerates SQL Azure servers that are provisioned for a subscription.
* @return array An array of Microsoft_SqlAzure_Management_ServerInstance.
* @throws Microsoft_SqlAzure_Management_Exception
if ($response->isSuccessful()) {
if (count($xml->Server) > 1) {
$xmlServices = $xml->Server;
$xmlServices = array($xml->Server);
for ($i = 0; $i < count($xmlServices); $i++ ) {
(string) $xmlServices[$i]->Name,
(string) $xmlServices[$i]->AdministratorLogin,
(string) $xmlServices[$i]->Location
* The Drop Server operation drops a SQL Azure server from a subscription.
* @param string $serverName Server to drop.
* @throws Microsoft_SqlAzure_Management_Exception
if ($serverName == '' || is_null($serverName)) {
if (!$response->isSuccessful()) {
* The Set Server Administrator Password operation sets the administrative password of a SQL Azure server for a subscription.
* @param string $serverName Server to set password for.
* @param string $administratorPassword Administrator password.
* @throws Microsoft_SqlAzure_Management_Exception
if ($serverName == '' || is_null($serverName)) {
if ($administratorPassword == '' || is_null($administratorPassword)) {
$response = $this->_performRequest(self::OP_SERVERS . '/' . $serverName, array('op' => 'ResetPassword'),
array('Content-Type' => 'application/xml; charset=utf-8'),
'<AdministratorLoginPassword xmlns="http://schemas.microsoft.com/sqlazure/2010/12/">' . $administratorPassword . '</AdministratorLoginPassword>');
if (!$response->isSuccessful()) {
* The Set Server Firewall Rule operation updates an existing firewall rule or adds a new firewall rule for a SQL Azure server that belongs to a subscription.
* @param string $serverName Server name.
* @param string $ruleName Firewall rule name.
* @param string $startIpAddress Start IP address.
* @param string $endIpAddress End IP address.
* @return Microsoft_SqlAzure_Management_FirewallRuleInstance
* @throws Microsoft_SqlAzure_Management_Exception
public function createFirewallRule($serverName, $ruleName, $startIpAddress, $endIpAddress)
if ($serverName == '' || is_null($serverName)) {
if ($ruleName == '' || is_null($ruleName)) {
if ($startIpAddress == '' || is_null($startIpAddress) || !filter_var($startIpAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
if ($endIpAddress == '' || is_null($endIpAddress) || !filter_var($endIpAddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
$response = $this->_performRequest(self::OP_SERVERS . '/' . $serverName . '/' . self::OP_FIREWALLRULES . '/' . $ruleName, array(),
array('Content-Type' => 'application/xml; charset=utf-8'),
'<FirewallRule xmlns="http://schemas.microsoft.com/sqlazure/2010/12/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://schemas.microsoft.com/sqlazure/2010/12/ FirewallRule.xsd"><StartIpAddress>' . $startIpAddress . '</StartIpAddress><EndIpAddress>' . $endIpAddress . '</EndIpAddress></FirewallRule>');
if ($response->isSuccessful()) {
* The Get Server Firewall Rules operation retrieves a list of all the firewall rules for a SQL Azure server that belongs to a subscription.
* @param string $serverName Server name.
* @return Array of Microsoft_SqlAzure_Management_FirewallRuleInstance.
* @throws Microsoft_SqlAzure_Management_Exception
if ($serverName == '' || is_null($serverName)) {
$response = $this->_performRequest(self::OP_SERVERS . '/' . $serverName . '/' . self::OP_FIREWALLRULES);
if ($response->isSuccessful()) {
if (!$xml->FirewallRule) {
if (count($xml->FirewallRule) > 1) {
$xmlServices = $xml->FirewallRule;
$xmlServices = array($xml->FirewallRule);
for ($i = 0; $i < count($xmlServices); $i++ ) {
(string) $xmlServices[$i]->Name,
(string) $xmlServices[$i]->StartIpAddress,
(string) $xmlServices[$i]->EndIpAddress
* The Delete Server Firewall Rule operation deletes a firewall rule from a SQL Azure server that belongs to a subscription.
* @param string $serverName Server name.
* @param string $ruleName Rule name.
* @throws Microsoft_SqlAzure_Management_Exception
if ($serverName == '' || is_null($serverName)) {
if ($ruleName == '' || is_null($ruleName)) {
$response = $this->_performRequest(self::OP_SERVERS . '/' . $serverName . '/' . self::OP_FIREWALLRULES . '/' . $ruleName, array(),
if (!$response->isSuccessful()) {
* Creates a firewall rule for Microsoft Services. This is required if access to SQL Azure is required from other services like Windows Azure.
* @param string $serverName Server name.
* @param boolean $allowAccess Allow access from other Microsoft Services?
* @throws Microsoft_SqlAzure_Management_Exception
|