Show
Reading Data with GET We can read data from our Firebase database by issuing a curl 'https://docs-examples.firebaseio.com/fireblog/posts.json?print=pretty' A successful request will be indicated by a Adding URI ParametersThe REST API accepts several query parameters when reading data from our Firebase database. Listed below are the most commonly used parameters. For a full list, refer to the REST API Reference. auth The curl 'https://docs-examples.firebaseio.com/auth-example.json?auth=CREDENTIAL' Specifying curl 'https://docs-examples.firebaseio.com/fireblog/posts.json?print=pretty' Specifying curl 'https://docs-examples.firebaseio.com/fireblog/posts.json?print=silent' callback To make REST calls from a web browser across domains you can use JSONP to wrap the response in a JavaScript callback function. Add <script> function gotData(data) { console.log(data); } </script> <script src="https://docs-examples.firebaseio.com/fireblog/posts.json?callback=gotData"> shallow This is an advanced feature, designed to help you work with large datasets
without needing to download everything. To use it, add { "message": { "user": { "name": "Chris" }, "body": "Hello!" } } // A request to /message.json?shallow=true // would return the following: { "user": true, "body": true } // A request to /message/body.json?shallow=true // would simply return: "Hello!" Try it out with this curl 'https://docs-examples.firebaseio.com/rest/retrieving-data.json?shallow=true&print=pretty' timeoutUse this to limit how long the read takes on the server side. If a read request doesn't finish within the allotted time, it terminates with an HTTP 400 error. This is particularly useful when you expect a small data transfer and don't want to wait too long to fetch a potentially huge subtree. Actual read time might vary based on data size and caching. Specify curl 'https://docs-examples.firebaseio.com/rest/retrieving-data.json?timeout=10s' Filtering Data We can construct queries to filter data based on various factors. To start, you specify how you want your data to be filtered using the Since all of us at Firebase think dinosaurs are pretty cool, we'll use a snippet from a sample database of dinosaur facts to demonstrate how you can filter data: { "lambeosaurus": { "height": 2.1, "length": 12.5, "weight": 5000 }, "stegosaurus": { "height": 4, "length": 9, "weight": 2500 } } We can filter data in one of three ways: by child key, by key, or by value. A query starts with one of these parameters, and then must be
combined with one or more of the following parameters: Filtering by a specified child key We can filter nodes by a common child key by passing that key to the curl 'https://dinosaur-facts.firebaseio.com/dinosaurs.json?orderBy="height"&startAt=3&print=pretty' Any node that does not have the child key we're
filtering on will be sorted with a value of Firebase also supports queries ordered by deeply nested children, rather than only children one level down. This is useful if you have deeply nested data like this: { "lambeosaurus": { "dimensions": { "height" : 2.1, "length" : 12.5, "weight": 5000 } }, "stegosaurus": { "dimensions": { "height" : 4, "length" : 9, "weight" : 2500 } } } To query the height now, we use the full path to the object rather than a single key: curl 'https://dinosaur-facts.firebaseio.com/dinosaurs.json?orderBy="dimensions/height"&startAt=3&print=pretty' Queries can only filter by one key at a time. Using the Filtering by key We can also filter nodes by their keys using the curl 'https://dinosaur-facts.firebaseio.com/dinosaurs.json?orderBy="$key"&startAt="a"&endAt="m"&print=pretty' Filtering by value We can filter nodes by the value of their child keys using the { "scores": { "bruhathkayosaurus": 55, "lambeosaurus": 21, "linhenykus": 80, "pterodactyl": 93, "stegosaurus": 5, "triceratops": 22 } } To retrieve all dinosaurs with a score higher than 50, we could make the following request: curl 'https://dinosaur-facts.firebaseio.com/scores.json?orderBy="$value"&startAt=50&print=pretty' See How Data is Ordered for an explanation on
how Complex FilteringWe can combine multiple parameters to construct more complex queries. Limit Queries The Using our dinosaur facts database and curl 'https://dinosaur-facts.firebaseio.com/dinosaurs.json?orderBy="weight"&limitToLast=2&print=pretty' Similarly, we can find the two shortest dinosaurs by using
curl 'https://dinosaur-facts.firebaseio.com/dinosaurs.json?orderBy="height"&limitToFirst=2&print=pretty' We can also conduct limit queries with curl 'https://dinosaur-facts.firebaseio.com/scores.json?orderBy="$value"&limitToLast=3&print=pretty' Range Queries Using curl 'https://dinosaur-facts.firebaseio.com/dinosaurs.json?orderBy="height"&startAt=3&print=pretty' We can use curl 'https://dinosaur-facts.firebaseio.com/dinosaurs.json?orderBy="$key"&endAt="pterodactyl"&print=pretty' We can combine curl 'https://dinosaur-facts.firebaseio.com/dinosaurs.json?orderBy="$key"&startAt="b"&endAt="b\uf8ff"&print=pretty' Range queries are also useful when you need to paginate your data. Putting it all togetherWe can combine all of these techniques to create complex queries. For example, maybe you want to find the name of all dinosaurs that are shorter than or equal in height to our favorite kind, Stegosaurus: MY_FAV_DINO_HEIGHT=`curl "https://dinosaur-facts.firebaseio.com/dinosaurs/stegosaurus/height.json"` curl "https://dinosaur-facts.firebaseio.com/dinosaurs.json?orderBy=\"height\"&endAt=${MY_FAV_DINO_HEIGHT}&print=pretty" How Data is OrderedThis section explains how your data is ordered when using each of the three filtering parameters. orderBy
When using
The filtered results are returned unordered. If the order of your data is important you should sort the results in your application after they are returned from Firebase. orderBy="$key" When using the
orderBy="$value" When using the orderBy="$priority" When using the
For more information on priorities, see the API reference. Streaming from the REST APIFirebase REST endpoints support the EventSource / Server-Sent Events protocol, making it easy to stream changes to a single location in our Firebase database. To get started with streaming, we will need to do the following:
In return, the server will send named events as the state of the data at the requested URL changes. The structure of these messages conforms to the EventSource protocol: event: event name data: JSON encoded data payload The server may send the following events:
Below is an example of a set of events that the server may send: // Set your entire cache to {"a": 1, "b": 2} event: put data: {"path": "/", "data": {"a": 1, "b": 2}} // Put the new data in your cache under the key 'c', so that the complete cache now looks like: // {"a": 1, "b": 2, "c": {"foo": true, "bar": false}} event: put data: {"path": "/c", "data": {"foo": true, "bar": false}} // For each key in the data, update (or add) the corresponding key in your cache at path /c, // for a final cache of: {"a": 1, "b": 2, "c": {"foo": 3, "bar": false, "baz": 4}} event: patch data: {"path": "/c", "data": {"foo": 3, "baz": 4}} If you're using Go, check out Firego, a third-party wrapper around the Firebase REST and Streaming APIs. Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Last updated 2022-09-13 UTC. [{ "type": "thumb-down", "id": "missingTheInformationINeed", "label":"Missing the information I need" },{ "type": "thumb-down", "id": "tooComplicatedTooManySteps", "label":"Too complicated / too many steps" },{ "type": "thumb-down", "id": "outOfDate", "label":"Out of date" },{ "type": "thumb-down", "id": "samplesCodeIssue", "label":"Samples / code issue" },{ "type": "thumb-down", "id": "otherDown", "label":"Other" }] [{ "type": "thumb-up", "id": "easyToUnderstand", "label":"Easy to understand" },{ "type": "thumb-up", "id": "solvedMyProblem", "label":"Solved my problem" },{ "type": "thumb-up", "id": "otherUp", "label":"Other" }] How do I access Firebase with PHP?Now, You are ready to integrate PHP with Firebase.. Create a Project in Firebase.. Setup read and write rules for users.. Move to user and permissions to generate API keys.. Download secret Json file and save inside your project.. How do I get data from Firebase?Firebase data is retrieved by either a one time call to GetValueAsync() or attaching to an event on a FirebaseDatabase reference. The event listener is called once for the initial state of the data and again anytime the data changes.
Can we host PHP website on Firebase?PHP is a dynamic server-side language. You can definitely use Firebase with PHP hosting, and PHP hosting is very easy to come by, but Firebase Hosting isn't meant for server-side dynamic content (like PHP).
|