Windows Azure SDK for PHP
This project provides a set of PHP client libraries that make it easy to access
Windows Azure tables, blobs, queues, service bus (queues and topics), service runtime and service management APIs. For documentation on how to host PHP applications on Windows Azure, please see the
Windows Azure PHP Developer Center., (*1)
Features
- Tables
- create and delete tables
- create, query, insert, update, merge, and delete entities
- batch operations
- REST API Version: 2011-08-18
- Blobs
- create, list, and delete containers, work with container metadata and permissions, list blobs in container
- create block and page blobs (from a stream or a string), work with blob blocks and pages, delete blobs
- work with blob properties, metadata, leases, snapshot a blob
- REST API Version: 2011-08-18
- Storage Queues
- create, list, and delete queues, and work with queue metadata and properties
- create, get, peek, update, delete messages
- REST API Version: 2011-08-18
- Service Bus
- Queues: create, list and delete queues; send, receive, unlock and delete messages
- Topics: create, list, and delete topics; create, list, and delete subscriptions; send, receive, unlock and delete messages; create, list, and delete rules
- Service Runtime
- discover addresses and ports for the endpoints of other role instances in your service
- get configuration settings and access local resources
- get role instance information for current role and other role instances
- query and set the status of the current role
- REST API Version: 2011-03-08
- Service Management
- storage accounts: create, update, delete, list, regenerate keys
- affinity groups: create, update, delete, list, get properties
- locations: list
- hosted services: create, update, delete, list, get properties
- deployment: create, get, delete, swap, change configuration, update status, upgrade, rollback
- role instance: reboot, reimage
- REST API Version: 2011-10-01
- Media Services
- Connection
- Ingest asset, upload files
- Encoding / process asset, create job, job templates
- Manage media services entities: create / update / read / delete / get list
- Delivery SAS and Streaming media content
- REST API Version: 2.2
Getting Started
Download Source Code
To get the source code from GitHub, type, (*2)
git clone https://github.com/WindowsAzure/azure-sdk-for-php.git
cd ./azure-sdk-for-php
Note, (*3)
The PHP Client Libraries for Windows Azure have a dependency on the HTTP_Request2, Mail_mime, and Mail_mimeDecode PEAR packages. The recommended way to resolve these dependencies is to install them using the PEAR package manager., (*4)
Install via Composer
-
Create a file named composer.json in the root of your project and add the following code to it:, (*5)
{
"require": {
"microsoft/windowsazure": "*"
},
"repositories": [
{
"type": "pear",
"url": "http://pear.php.net"
}
],
"minimum-stability": "dev"
}
-
Download composer.phar in your project root., (*6)
-
Open a command prompt and execute this in your project root, (*7)
php composer.phar install
Note, (*8)
On Windows, you will also need to add the Git executable to your PATH environment variable., (*9)
Install as a PEAR package
To install the PHP Client Libraries for Windows Azure as a PEAR package, follow these steps:, (*10)
-
Install PEAR.
-
Set-up the Windows Azure PEAR channel:, (*11)
pear channel-discover pear.windowsazure.com
-
Install the PEAR package:, (*12)
pear install pear.windowsazure.com/WindowsAzure-0.4.0
Usage
Getting Started
There are four basic steps that have to be performed before you can make a call to any Windows Azure API when using the libraries., (*13)
-
First, include the autoloader script:, (*14)
If installed via PEAR or Git:, (*15)
require_once "WindowsAzure/WindowsAzure.php";
If installed via Composer:, (*16)
require_once "vendor/autoload.php";
-
Include the namespaces you are going to use., (*17)
To create any Windows Azure service client you need to use the ServicesBuilder class:, (*18)
use WindowsAzure\Common\ServicesBuilder;
To process exceptions you need:, (*19)
use WindowsAzure\Common\ServiceException;
-
To instantiate the service client you will also need a valid connection string. The format is:, (*20)
-
For accessing a live storage service (tables, blobs, queues):, (*21)
DefaultEndpointsProtocol=[http|https];AccountName=[yourAccount];AccountKey=[yourKey]
-
For accessing the emulator storage:, (*22)
UseDevelopmentStorage=true
-
For accessing the Service Bus:, (*23)
Endpoint=[yourEndpoint];SharedSecretIssuer=[yourWrapAuthenticationName];SharedSecretValue=[yourWrapPassword]
Where the Endpoint is typically of the format https://[yourNamespace].servicebus.windows.net
., (*24)
-
For accessing Service Management APIs:, (*25)
SubscriptionID=[yourSubscriptionId];CertificatePath=[filePathToYourCertificate]
-
Instantiate a "REST Proxy" - a wrapper around the available calls for the given service., (*26)
-
For the Storage services:, (*27)
$tableRestProxy = ServicesBuilder::getInstance()->createTableService($connectionString);
$blobRestProxy = ServicesBuilder::getInstance()->createBlobService($connectionString);
$queueRestProxy = ServicesBuilder::getInstance()->createQueueService($connectionString);
-
For Service Bus:, (*28)
$serviceBusRestProxy = ServicesBuilder::getInstance()->createServiceBusService($connectionString);
-
For Service Management:, (*29)
$serviceManagementRestProxy = ServicesBuilder::getInstance()->createServiceManagementService($connectionString);
-
For Media Services:, (*30)
$mediaServicesRestProxy = ServicesBuilder->getInstance()->createMediaServicesService(new MediaServicesSettings([YourAccountName], [YourPrimaryOrSecondaryAccessKey]));
Table Storage
The following are examples of common operations performed with the Table serivce. For more please read How-to use the Table service., (*31)
Create a table
To create a table call createTable:, (*32)
try {
// Create table.
$tableRestProxy->createTable("mytable");
} catch(ServiceException $e){
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message."<br />";
}
Error Codes and Messages for Tables, (*33)
Insert an entity
To add an entity to a table, create a new Entity object and pass it to TableRestProxy->insertEntity. Note that when you create an entity you must specify a PartitionKey
and RowKey
. These are the unique identifiers for an entity and are values that can be queried much faster than other entity properties. The system uses PartitionKey
to automatically distribute the tableâs entities over many storage nodes., (*34)
use WindowsAzure\Table\Models\Entity;
use WindowsAzure\Table\Models\EdmType;
$entity = new Entity();
$entity->setPartitionKey("pk");
$entity->setRowKey("1");
$entity->addProperty("PropertyName", EdmType::STRING, "Sample");
try{
$tableRestProxy->insertEntity("mytable", $entity);
} catch(ServiceException $e){
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message."<br />";
}
Query entities
To query for entities you can call queryEntities. The subset of entities you retrieve will be determined by the filter you use (for more information, see Querying Tables and Entities). You can also provide no filter at all., (*35)
$filter = "RowKey eq '2'";
try {
$result = $tableRestProxy->queryEntities("mytable", $filter);
} catch(ServiceException $e){
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message."<br />";
}
$entities = $result->getEntities();
foreach($entities as $entity){
echo $entity->getPartitionKey().":".$entity->getRowKey()."<br />";
}
Blob Storage
To get started using the Blob service you must include the BlobService
and BlobSettings
namespaces and set the ACCOUNT_NAME
and ACCOUNT_KEY
configuration settings for your credentials. Then you instantiate the wrapper using the BlobService
factory., (*36)
The following are examples of common operations performed with the Blob serivce. For more please read How-to use the Blob service., (*37)
Create a container
// OPTIONAL: Set public access policy and metadata.
// Create container options object.
$createContainerOptions = new CreateContainerOptions();
// Set public access policy. Possible values are
// PublicAccessType::CONTAINER_AND_BLOBS and PublicAccessType::BLOBS_ONLY.
// CONTAINER_AND_BLOBS: full public read access for container and blob data.
// BLOBS_ONLY: public read access for blobs. Container data not available.
// If this value is not specified, container data is private to the account owner.
$createContainerOptions->setPublicAccess(PublicAccessType::CONTAINER_AND_BLOBS);
// Set container metadata
$createContainerOptions->addMetaData("key1", "value1");
$createContainerOptions->addMetaData("key2", "value2");
try {
// Create container.
$blobRestProxy->createContainer("mycontainer", $createContainerOptions);
} catch(ServiceException $e){
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message."<br />";
}
Error Codes and Messages for Blobs, (*38)
For more information about container ACLs, see Set Container ACL (REST API)., (*39)
Upload a blob
To upload a file as a blob, use the BlobRestProxy->createBlockBlob method. This operation will create the blob if it doesnât exist, or overwrite it if it does. The code example below assumes that the container has already been created and uses fopen to open the file as a stream., (*40)
$content = fopen("myfile.txt", "r");
$blob_name = "myblob";
try {
//Upload blob
$blobRestProxy->createBlockBlob("mycontainer", $blob_name, $content);
} catch(ServiceException $e){
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message."<br />";
}
While the example above uploads a blob as a stream, a blob can also be uploaded as a string., (*41)
List blobs in a container
To list the blobs in a container, use the BlobRestProxy->listBlobs method with a foreach loop to loop through the result. The following code outputs the name and URI of each blob in a container., (*42)
try {
// List blobs.
$blob_list = $blobRestProxy->listBlobs("mycontainer");
$blobs = $blob_list->getBlobs();
foreach($blobs as $blob)
{
echo $blob->getName().": ".$blob->getUrl()."<br />";
}
} catch(ServiceException $e){
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message."<br />";
}
Storage Queues
To get started using the Queue service you must include the QueueService
and QueueSettings
namespaces and set the ACCOUNT_NAME
and ACCOUNT_KEY
configuration settings for your credentials. Then you instantiate the wrapper using the QueueService
factory., (*43)
The following are examples of common operations performed with the Queue serivce. For more please read How-to use the Queue service., (*44)
Create a queue
A QueueRestProxy object lets you create a queue with the createQueue method. When creating a queue, you can set options on the queue, but doing so is not required., (*45)
$createQueueOptions = new CreateQueueOptions();
$createQueueOptions->addMetaData("key1", "value1");
$createQueueOptions->addMetaData("key2", "value2");
try {
// Create queue.
$queueRestProxy->createQueue("myqueue", $createQueueOptions);
} catch(ServiceException $e){
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message."<br />";
}
Error Codes and Messages for Queues, (*46)
Add a message to a queue
To add a message to a queue, use QueueRestProxy->createMessage. The method takes the queue name, the message text, and message options (which are optional)., (*47)
try {
// Create message.
$queueRestProxy->createMessage("myqueue", "Hello World!");
} catch(ServiceException $e){
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message."<br />";
}
Peek at the next message
You can peek at a message (or messages) at the front of a queue without removing it from the queue by calling QueueRestProxy->peekMessages., (*48)
// OPTIONAL: Set peek message options.
$message_options = new PeekMessagesOptions();
$message_options->setNumberOfMessages(1); // Default value is 1.
try {
$peekMessagesResult = $queueRestProxy->peekMessages("myqueue", $message_options);
} catch(ServiceException $e){
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message."<br />";
}
$messages = $peekMessagesResult->getQueueMessages();
// View messages.
$messageCount = count($messages);
if($messageCount <= 0){
echo "There are no messages.<br />";
}
else{
foreach($messages as $message) {
echo "Peeked message:<br />";
echo "Message Id: ".$message->getMessageId()."<br />";
echo "Date: ".date_format($message->getInsertionDate(), 'Y-m-d')."<br />";
echo "Message text: ".$message->getMessageText()."<br /><br />";
}
}
De-queue the next message
Your code removes a message from a queue in two steps. First, you call QueueRestProxy->listMessages, which makes the message invisible to any other code reading from the queue. By default, this message will stay invisible for 30 seconds (if the message is not deleted in this time period, it will become visible on the queue again). To finish removing the message from the queue, you must call QueueRestProxy->deleteMessage., (*49)
// Get message.
$listMessagesResult = $queueRestProxy->listMessages("myqueue");
$messages = $listMessagesResult->getQueueMessages();
$message = $messages[0];
// Process message
// Get message Id and pop receipt.
$messageId = $message->getMessageId();
$popReceipt = $message->getPopReceipt();
try {
// Delete message.
$queueRestProxy->deleteMessage("myqueue", $messageId, $popReceipt);
} catch(ServiceException $e){
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message."<br />";
}
Service Bus Queues
Create a Queue
try {
$queueInfo = new QueueInfo("myqueue");
// Create queue.
$serviceBusRestProxy->createQueue($queueInfo);
} catch(ServiceException $e){
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message."<br />";
}
Error Codes and Messages, (*50)
Send a Message
To send a message to a Service Bus queue, your application will call the ServiceBusRestProxy->sendQueueMessage method. Messages sent to (and received from ) Service Bus queues are instances
of the BrokeredMessage class., (*51)
try {
// Create message.
$message = new BrokeredMessage();
$message->setBody("my message");
// Send message.
$serviceBusRestProxy->sendQueueMessage("myqueue", $message);
} catch(ServiceException $e){
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message."<br />";
}
Receive a Message
The primary way to receive messages from a queue is to use a ServiceBusRestProxy->receiveQueueMessage method. Messages can be received in two different modes: ReceiveAndDelete (mark message as consumed on read) and PeekLock (locks message for a period of time, but does not delete)., (*52)
The example below demonstrates how a message can be received and processed using PeekLock mode (not the default mode)., (*53)
try {
// Set the receive mode to PeekLock (default is ReceiveAndDelete).
$options = new ReceiveMessageOptions();
$options->setPeekLock(true);
// Receive message.
$message = $serviceBusRestProxy->receiveQueueMessage("myqueue", $options);
echo "Body: ".$message->getBody()."<br />";
echo "MessageID: ".$message->getMessageId()."<br />";
// *** Process message here ***
// Delete message.
$serviceBusRestProxy->deleteMessage($message);
} catch(ServiceException $e){
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message."<br />";
}
Service Bus Topics
Create a Topic
try {
// Create topic.
$topicInfo = new TopicInfo("mytopic");
$serviceBusRestProxy->createTopic($topicInfo);
} catch(ServiceException $e){
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message."<br />";
}
Create a subscription with the default (MatchAll) filter
try {
// Create subscription.
$subscriptionInfo = new SubscriptionInfo("mysubscription");
$serviceBusRestProxy->createSubscription("mytopic", $subscriptionInfo);
} catch(ServiceException $e){
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message."<br />";
}
Send a message to a topic
Messages sent to Service Bus topics are instances of the BrokeredMessage class., (*54)
try {
// Create message.
$message = new BrokeredMessage();
$message->setBody("my message");
// Send message.
$serviceBusRestProxy->sendTopicMessage("mytopic", $message);
} catch(ServiceException $e){
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message."<br />";
}
Receive a message from a topic
The primary way to receive messages from a subscription is to use a ServiceBusRestProxy->receiveSubscriptionMessage method. Received messages can work in two different modes: ReceiveAndDelete (the default) and PeekLock similarly to Service Bus Queues., (*55)
The example below demonstrates how a message can be received and processed using ReceiveAndDelete mode (the default mode)., (*56)
try {
// Set receive mode to PeekLock (default is ReceiveAndDelete)
$options = new ReceiveMessageOptions();
$options->setReceiveAndDelete();
// Get message.
$message = $serviceBusRestProxy->receiveSubscriptionMessage("mytopic",
"mysubscription",
$options);
echo "Body: ".$message->getBody()."<br />";
echo "MessageID: ".$message->getMessageId()."<br />";
} catch(ServiceException $e){
$code = $e->getCode();
$error_message = $e->getMessage();
echo $code.": ".$error_message."<br />";
}
Service Management
Set-up certificates
You need to create two certificates, one for the server (a .cer file) and one for the client (a .pem file). To create the .pem file using OpenSSL, execute this:, (*57)
openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem
To create the .cer certificate, execute this:, (*58)
openssl x509 -inform pem -in mycert.pem -outform der -out mycert.cer
List Available Locations
$serviceManagementRestProxy->listLocations();
$locations = $result->getLocations();
foreach($locations as $location){
echo $location->getName()."<br />";
}
Create a Storage Service
To create a storage service, you need a name for the service (between 3 and 24 lowercase characters and unique within Windows Azure), a label (a base-64 encoded name for the service, up to 100 characters), and either a location or an affinity group. Providing a description for the service is optional., (*59)
$name = "mystorageservice";
$label = base64_encode($name);
$options = new CreateStorageServiceOptions();
$options->setLocation('West US');
$result = $serviceManagementRestProxy->createStorageService($name, $label, $options);
Create a Cloud Service
A cloud service is also known as a hosted service (from earlier versions of Windows Azure). The createHostedServices method allows you to create a new hosted service by providing a hosted service name (which must be unique in Windows Azure), a label (the base 64-endcoded hosted service name), and a CreateServiceOptions object which allows you to set the location or the affinity group for your service., (*60)
$name = "myhostedservice";
$label = base64_encode($name);
$options = new CreateServiceOptions();
$options->setLocation('West US');
// Instead of setLocation, you can use setAffinityGroup to set an affinity group.
$result = $serviceManagementRestProxy->createHostedService($name, $label, $options);
Create a Deployment
To make a new deployment to Azure you must store the package file in a Windows Azure Blob Storage account under the same subscription as the hosted service to which the package is being uploaded. You can create a deployment package with the Windows Azure PowerShell cmdlets, or with the cspack commandline tool., (*61)
$hostedServiceName = "myhostedservice";
$deploymentName = "v1";
$slot = DeploymentSlot::PRODUCTION;
$packageUrl = "URL_for_.cspkg_file";
$configuration = file_get_contents('path_to_.cscfg_file');
$label = base64_encode($hostedServiceName);
$result = $serviceManagementRestProxy->createDeployment($hostedServiceName,
$deploymentName,
$slot,
$packageUrl,
$configuration,
$label);
$status = $serviceManagementRestProxy->getOperationStatus($result);
echo "Operation status: ".$status->getStatus()."<br />";
Create new asset with file
To create an asset with a file you need to create an empty asset, create access policy with write permission, create a locator joining your asset and access policy, perform actual upload and generate file info., (*62)
$asset = new Asset(Asset::OPTIONS_NONE);
$asset = $restProxy->createAsset($asset);
$access = new AccessPolicy('[Some access policy name]');
$access->setDurationInMinutes([Munites AccessPolicy is valid]);
$access->setPermissions(AccessPolicy::PERMISSIONS_WRITE);
$access = $restProxy->createAccessPolicy($access);
$locator = new Locator($asset, $access, Locator::TYPE_SAS);
$locator->setStartTime(new \DateTime('now -5 minutes'));
$locator = $restProxy->createLocator($locator);
$restProxy->uploadAssetFile($locator, '[file name]', '[file content]');
$restProxy->createFileInfos($asset);
Encode asset
To perform media file encoding you will need input asset ($inputAsset) with a file in it (something like in previous chapter). Also you need to create an array of task data objects and a job data object. To create a task object use a media processor, task XML body and configuration name., (*63)
$mediaProcessor = $this->restProxy->getLatestMediaProcessor('[Media processor]');
$task = new Task('[Task XML body]', $mediaProcessor->getId(), TaskOptions::NONE);
$task->setConfiguration('[Configuration name]');
$restProxy->createJob(new Job(), array($inputAsset), array($task));
Get public URL to encoded asset
After youâve uploaded a media file and encode it you can get a download URL for that file. Create a new access policy with read permission and link it with job output asset via locator., (*64)
$accessPolicy = new AccessPolicy('[Some access policy name]');
$accessPolicy->setDurationInMinutes([Munites AccessPolicy is valid]);
$accessPolicy->setPermissions(AccessPolicy::PERMISSIONS_READ);
$accessPolicy = $restProxy->createAccessPolicy($accessPolicy);
$locator = new Locator($asset, $accessPolicy, Locator::TYPE_SAS);
$locator->setStartTime(new \DateTime('now -5 minutes'));
$locator = $restProxy->createLocator($locator);
// Azure needs time to publish media
sleep(30);
$url = $locator->getBaseUri() . '/' . '[File name]' . $locator->getContentAccessComponent()
Media services CRUD operations are performed through media services rest proxy class. It has methods like âcreateAssetâ, âcreateLocatorâ, âcreateJobâ and etc. for entities creations., (*65)
To retrieve all entities list you may use methods âgetAssetListâ, âgetAccessPolicyListâ, âgetLocatorListâ, âgetJobListâ and etc. For getting single entity data use methods âgetAssetâ, âgetJobâ, âgetTaskâ and etc. passing the entity identifier or entity data model object with non-empty identifier as a parameter., (*66)
Update entities with methods like âupdateLocatorâ, âupdateAssetâ, âupdateAssetFileâ and etc. passing the entity data model object as a parameter. It is important to have valid entity identifier specified in data model object., (*67)
Erase entities with methods like âdeleteAssetâ, âdeleteAccessPolicyâ, âdeleteJobâ and etc. passing the entity identifier or entity data model object with non-empty identifier as a parameter., (*68)
Also you could get linked entities with methods âgetAssetLocatorsâ, âgetAssetParentAssetsâ, âgetAssetStorageAccountâ, âgetLocatorAccessPolicyâ, âgetJobTasksâ and etc. passing the entity identifier or entity data model object with non-empty identifier as a parameter., (*69)
The complete list of all methods available you could find in IMediaServices interface., (*70)
For more examples please see the Windows Azure PHP Developer Center, (*71)
Need Help?
Be sure to check out the Windows Azure Developer Forums on Stack Overflow if you have trouble with the provided code., (*72)
Contribute Code or Provide Feedback
If you would like to become an active contributor to this project please follow the instructions provided in Windows Azure Projects Contribution Guidelines., (*73)
To setup your development environment, follow the instructions in this wiki page., (*74)
If you encounter any bugs with the library please file an issue in the Issues section of the project., (*75)
Learn More
Windows Azure PHP Developer Center, (*76)