How to you use Azure documentdb using php

1.7k views Asked by At

Is any one know how to work with azure document db using php.

i want to create collection add the data in the document db using php.

please send any sample code for it.

http://azure.microsoft.com/en-in/services/documentdb/

Thanks

Thanigaivelan

2

There are 2 answers

0
Andrew Liu On

DocumentDB currently does not have an official PHP client SDK available.

You can interact with DocumentDB using the REST API.

Alternatively, you could look for unofficial 3rd-party SDKs like this one (I'm not sure how well supported this project is - but the source code looks to be good reference for interacting with the REST API).

0
Ioannis Kokkinis On

Documentdb offers a REST API, so what you need is a REST API client. You can write one on your own, or use one of the 3 available on github (search for php documentdb)

Basically you need to request the resource you need via POST and add the required headers using CURL. The only tricky part is the authorization token you need to create based on various information. Here is a code snippet for the token :

function gettoken($master_key,$vrb,$rtype,$rid,$da_date) {
$key = base64_decode($master_key);
$st_to_sign = $vrb . "\n" .
$rtype . "\n" .
$rid . "\n" .
$da_date . "\n" .
"\n";
$sig = base64_encode(hash_hmac('sha256', strtolower($st_to_sign), $key, true));
return $sig;
}

My repo on github is a good , simple way to start but not oop. (https://github.com/upggr/documentdb-for-php)

There are currently 2 more repos that provide the full API functionality via a class : The one from cocteau666 as mentioned in the previous comment and one from crassaert (https://github.com/crassaert/php-azure-documentdb)

For some reason, there is not much interest yet on people implementing this with php, I hope this changes soon.