PHP code example of qranio / azure-sdk-for-php

1. Go to this page and download the library: Download qranio/azure-sdk-for-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

qranio / azure-sdk-for-php example snippets


try	{
	// Create table.
	$tableRestProxy->createTable("mytable");
} catch(ServiceException $e){
	$code = $e->getCode();
	$error_message = $e->getMessage();
	echo $code.": ".$error_message."<br />";
}

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 />";
}

$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 />";
}

// 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 />";
}

$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 />";
}

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 />";
}

$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 />";
}

try	{
	// Create message.
	$queueRestProxy->createMessage("myqueue", "Hello World!");
} catch(ServiceException $e){
	$code = $e->getCode();
	$error_message = $e->getMessage();
	echo $code.": ".$error_message."<br />";
}

// 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 />";
	}
}

// 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 />";
}

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 />";
}

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 />";
}

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 />";
}

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 />";
}

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 />";
}

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 />";
}

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 />";
}
	
$serviceManagementRestProxy->listLocations();
$locations = $result->getLocations();
foreach($locations as $location){
      echo $location->getName()."<br />";
}

$name = "mystorageservice";
$label = base64_encode($name);
$options = new CreateStorageServiceOptions();
$options->setLocation('West US');

$result = $serviceManagementRestProxy->createStorageService($name, $label, $options);

$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);

$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 />";

$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);

$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));

$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()