PHPackages                             phuze/php-cosmos - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. phuze/php-cosmos

ActiveLibrary[HTTP &amp; Networking](/categories/http)

phuze/php-cosmos
================

PHP wrapper for Azure Cosmos DB using SQL rest api

3.0.4(2y ago)02.1k1MITPHPPHP &gt;=7.0

Since Feb 2Pushed 2y ago2 watchersCompare

[ Source](https://github.com/phuze/php-cosmos)[ Packagist](https://packagist.org/packages/phuze/php-cosmos)[ Docs](https://github.com/phuze/php-cosmos)[ RSS](/packages/phuze-php-cosmos/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (1)Versions (6)Used By (0)

php-cosmos
==========

[](#php-cosmos)

PHP wrapper for Azure Cosmos DB

Installation
------------

[](#installation)

Install phuze/php-cosmos in your project:

```
composer require phuze/php-cosmos
```

Changelog
---------

[](#changelog)

### v3.0.4

[](#v304)

- bug fixes and other minor changes

### v3.0.0

[](#v300)

- restore support for PHP 7.x -- this library can be used with both 7.x and 8.x
- improved how nested partition keys are handled
- improved how partition values are matched
- fixed an issue which prevented document deletion when a container used a nested partition key
- fixed an issue with `partitionkeyrangeid` headers, when a cross-partition query needs to be retried with PK ranges

### v2.6.0

[](#v260)

- code refactor. min PHP verion supported is now 8.0
- selectCollection no longer creates a colletion if not exist. use createCollection for that
- bug fixes

### v2.5.0

[](#v250)

- support partitioned queries using new method `setPartitionValue()`
- support creating partitioned collections
- support for nested partition keys

### v2.0.0

[](#v200)

- support for cross partition queries
- selectCollection method removed from all methods for performance improvements

### v1.4.4

[](#v144)

- replaced pear package http\_request2 by guzzle
- added method to provide guzzle configuration

### v1.3.0

[](#v130)

- added support for parameterized queries

Notes
-----

[](#notes)

- Currently uses Microsoft API version `2018-12-31`
- Based on [AzureDocumentDB-PHP](https://github.com/cocteau666/AzureDocumentDB-PHP) and [CosmosDb](https://github.com/jupitern/cosmosdb).
- Planned updates include:
    - support for new [PATCH](https://learn.microsoft.com/en-us/azure/cosmos-db/partial-document-update) api operations
    - enhanced debug and logging support

Usage
-----

[](#usage)

### Connecting

[](#connecting)

```
$conn = new \Phuze\PhpCosmos\CosmosDb('host', 'key');
$conn->setHttpClientOptions(['verify' => false]); # optional: set guzzle client options.
$db = $conn->selectDB('databaseName');
$collection = $db->selectCollection('collectionName');

# if a collection does not exist, it will be created when you
# attempt to select the collection. however, if you have created
# your database with shared throughput, then all collections require a partition key.
# selectCollection() supports a second parameter for this purpose.
$conn = new \Phuze\PhpCosmos\CosmosDb('host', 'key');
$conn->setHttpClientOptions(['verify' => false]); # optional: set guzzle client options.
$db = $conn->selectDB('dbName');
$collection = $db->selectCollection('collectionName', 'myPartitionKey');
```

### Inserting Records

[](#inserting-records)

```
# consider a existing collection called "Users" with a partition key "country"

# insert a record
$rid = \Phuze\PhpCosmos\QueryBuilder::instance()
    ->setCollection($collection)
    ->setPartitionKey('country')
    ->save(['id' => '1', 'name' => 'John Doe', 'age' => 22, 'country' => 'Portugal']);

# insert a record against a collection with a nested partition key
$rid = \Phuze\PhpCosmos\QueryBuilder::instance()
    ->setCollection($collection)
    ->setPartitionKey('billing.country')
    ->save([
        'id' => '2',
        'name' => 'Jane Doe',
        'age' => 35,
        'billing' => ['country' => 'Portugal']
    ]);
```

### Updating Records

[](#updating-records)

```
# update a record
$rid = \Phuze\PhpCosmos\QueryBuilder::instance()
    ->setCollection($collection)
    ->setPartitionKey('country')
    ->save([
        '_rid'    => $rid, // existing document _rid
        'id'      => '2',
        'name'    => 'Jane Doe',
        'age'     => 36,
        'country' => 'Portugal'
    ]);
```

### Querying Records

[](#querying-records)

```
# query a document and return it as an array
$res = \Phuze\PhpCosmos\QueryBuilder::instance()
    ->setCollection($collection)
    ->select("c.id, c.name")
    ->where("c.age > @age and c.country = @country")
    ->params(['@age' => 30, '@country' => 'Portugal'])
    ->find(true)
    ->toArray();

# query a document using a known partition value,
# and return as an array. note: setting a known
# partition value will result in a more efficient
# query against your database as it will not rely
# on cross-partition querying.
$res = \Phuze\PhpCosmos\QueryBuilder::instance()
    ->setCollection($collection)
    ->setPartitionKey('country')
    ->setPartitionValue('Portugal')
    ->select("c.id, c.name")
    ->where("c.age > @age and c.country = @country")
    ->params(['@age' => 30, '@country' => 'Portugal'])
    ->find()
    ->toArray();

# query the top 5 documents as an array, with the
# document ID as the array key.
$res = \Phuze\PhpCosmos\QueryBuilder::instance()
    ->setCollection($collection)
    ->select("c.id, c.username")
    ->where("c.age > @age and c.country = @country")
    ->params(['@age' => 10, '@country' => 'Portugal'])
    ->limit(5)
    ->findAll(true)
    ->toArray('id');

# query a document using a collection alias and cross partition query
$res = \Phuze\PhpCosmos\QueryBuilder::instance()
    ->setCollection($collection)
    ->select("HelloWorld.id, HelloWorld.name")
    ->from("HelloWorld")
    ->where("HelloWorld.age > 30")
    ->findAll(true)
    ->toArray();
```

### Deleting Records

[](#deleting-records)

```
# delete one document that matches criteria (single partition)
$res = \Phuze\PhpCosmos\QueryBuilder::instance()
    ->setCollection($collection)
    ->setPartitionKey('country')
    ->where("c.age > 30 and c.country = 'Portugal'")
    ->delete();

# delete all documents that match criteria (cross partition)
$res = \Phuze\PhpCosmos\QueryBuilder::instance()
    ->setCollection($collection)
    ->setPartitionKey('country')
    ->where("c.age > 20")
    ->deleteAll(true);
```

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity19

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity40

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 75% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~1 days

Total

5

Last Release

832d ago

PHP version history (2 changes)3.0.0PHP &gt;=8.0

3.0.1PHP &gt;=7.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/8f703f6079f0e040f85f12bdb68e8bd6e67d6399fd33ccd0ef9f3ff5f2b01f9e?d=identicon)[phuze](/maintainers/phuze)

---

Top Contributors

[![bl-lfl](https://avatars.githubusercontent.com/u/222427138?v=4)](https://github.com/bl-lfl "bl-lfl (24 commits)")[![phuze](https://avatars.githubusercontent.com/u/9218035?v=4)](https://github.com/phuze "phuze (8 commits)")

---

Tags

apirestsqlazurenosqlcosmosdb

### Embed Badge

![Health badge](/badges/phuze-php-cosmos/health.svg)

```
[![Health](https://phpackages.com/badges/phuze-php-cosmos/health.svg)](https://phpackages.com/packages/phuze-php-cosmos)
```

###  Alternatives

[xeroapi/xero-php-oauth2

Xero official PHP SDK for oAuth2 generated with OpenAPI spec 3

1054.3M14](/packages/xeroapi-xero-php-oauth2)[onesignal/onesignal-php-api

A powerful way to send personalized messages at scale and build effective customer engagement strategies. Learn more at onesignal.com

34170.2k2](/packages/onesignal-onesignal-php-api)[zenditplatform/zendit-php-sdk

PHP client for Zendit API

1204.3k](/packages/zenditplatform-zendit-php-sdk)[whatarmy/fedex-rest

New FedEx Rest API wrapper

2440.5k1](/packages/whatarmy-fedex-rest)[ory/hydra-client-php

Documentation for all of Ory Hydra's APIs.

1710.8k](/packages/ory-hydra-client-php)[meteocontrol/vcom-api-client

HTTP Client for meteocontrol's VCOM API - The VCOM API enables you to directly access your data on the meteocontrol platform.

175.7k1](/packages/meteocontrol-vcom-api-client)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
