PHP code example of phuze / php-cosmos

1. Go to this page and download the library: Download phuze/php-cosmos 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/ */

    

phuze / php-cosmos example snippets


use Phuze\PhpCosmos\CosmosDb;
use Phuze\PhpCosmos\QueryBuilder;

# connect, and select a database and collection
$conn = new CosmosDb('https://myaccount.documents.azure.com', 'your-key');
$database = $conn->selectDB('databaseName');
$collection = $database->selectCollection('Users', '/country');

# find the users in Canada who are over 30
$users = QueryBuilder::instance()
    ->setCollection($collection)
    ->setPartitionValue('Canada')
    ->where("c.age > @age")
    ->params(['@age' => 30])
    ->findAll()
    ->toArray();

  $conn = new CosmosDb($host, $key);
  $conn->setHttpClientOptions(['timeout' => 120]); # in seconds
  

  $res = QueryBuilder::instance()
      ->setCollection($collection)
      ->setPartitionValue((string)$id)
      ->findAll()
      ->toArray();
  

  $res = QueryBuilder::instance()
      ->setCollection($collection)
      ->where("c.active = @active")
      ->params(['@active' => true]) # v3 sent "1", v4 sends true
      ->findAll(true)
      ->toArray();
  

      ->params(['@active' => '1'])
  

  # v3 needed quotes escaped by hand
  ->whereStartsWith('c.name', "O\\'Brien")

  # v4 escapes them for you
  ->whereStartsWith('c.name', "O'Brien")
  
bash
composer 
bash
composer