0

How can I insert data (a document) to CosmosDB (using the SQL API) from a PHP web app?

Is it possible or not to process the insert query?

2
  • Welcome to SO. Please provide a minimal reproducible example of what you have tried already. It would be a good idea to read How to Ask and help center. Commented May 25, 2018 at 12:54
  • if the answer was sufficient then please consider accepting it. If not, then please edit to clarify you problem in more detail. Commented May 28, 2018 at 20:11

1 Answer 1

0

Azure CosmosDB supports multiple clients you can choose from. Currently it does not have native php client, but you can use REST API:

The Azure Cosmos DB REST API provides programmatic access to Azure Cosmos DB resources to create, query, and delete databases, document collections, and documents. To perform operations on Azure Cosmos DB resources, you send HTTPS requests with a supported method: GET, POST, PUT, or DELETE to an endpoint that targets a resource collection or a specific resource.

Read more from here: Azure Cosmos DB: REST API Reference.

To check if your preferred language has an official client library available, check the official CosmosDB documentation page "How can I develop apps with the SQL API?".

If by "Insert query" you mean INSERT INTO .. SQL syntax then note that CosmosDB SQL API is not ANSI SQL. See also another SO answer on the subject of SQL syntax in CosmosDB.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks. Its OK the creation of the new document from my php source code to cosmos db(means document db with SQL API) as you advised on using REST API. And then i got it cosmos db's query is absolutely not the same of ANSI SQL query. :-) But, one point leads to confuse to me, why 'id' is user created property, it is not auto generated by system. When i directly created new document without id in Azure Cosmos DB Explorer is successfully completed by owing the system generated id value. I want to create documents with system generated id. It can be or not? //store JSON document ("id" needed)
Id is required to be unique but you are given a choice for which values to use. If you don't provide one yourself, a system will provide a globally unique value itself. But you CAN use values which are unique for your application. Such values can existing natural key or some narrower artificial key. If some client API's don't assign automatic id then you can always assign a new guid yourself.
Thanks for ur comment and that's right id is unique and requires for each document. But, unfortunately i'm using PHP language and i think not supported automatic system generated id. Now, i make a try with node.js SDK.
Every language has tools to generate a guid. That's just cosmetic sugar. You don't have to switch languages because of that.
Well....now I'm using azure functions for adding documents to cosmos with my previous PHP source code...it works well..That's absolutely great....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.