ktamas77 / firebase-php

Firebase PHP Client
792 stars 215 forks source link

_getJsonPath function doesn't allow query strings #19

Closed solyoung closed 8 years ago

solyoung commented 9 years ago

The Firebase API offers orderBy and advanced query options. The _getJsonPath function currently appends ".json" to the end of the path without regard to the $path. This should be corrected so that query strings can be passed in the $path.

I may fork and solve, but at least wanted the issue reported in case I don't get back here.

markdrake commented 9 years ago

I'm having the same issue as solyoung. I wanted to use the query options but as you hard code the auth variable as the only query param available you make it impossible to use this without modification. I know it's a very simple wrapper but a more dynamic params approach would be ideal.

ktamas77 commented 9 years ago

Thank you for your suggestion, this is a very important feature. I marked it as a todo.

reedgz commented 9 years ago

I would love to see this feature as well. Then we can use shallow copy and other query options. 'https://samplechat.firebaseio-demo.com/.json?shallow=true'

solyoung commented 9 years ago

Here's a suggestion that would allow query functionality to get through that function (it's how I have the code modified in the meantime): private function _getJsonPath($path) { $url = $this->_baseURI; $path = ltrim($path, '/'); $qaToken = (strpos($path, '?') == FALSE) ? '?' : '&'; $auth = ($this->_token == '') ? '' : $qaToken . 'auth=' . $this->_token; $incJSON = (strpos($path, '.json') == FALSE) ? '.json' : ''; return $url . $path . $incJSON . $auth; }

*note, if you use the above, you need to include .json in your original request. e.g. $firebaseLib->get('test.json?orderBy=email'); If you don't include .json, the above will still append .json at the end of that path.

askinner2004 commented 8 years ago

I modified the firebaseLib.php file with the following lines to add some query functionality: ... private $_options; ... //This functions sets the options and ensures that the data is enclosed in QUOTATIONS //Just before submitting the data, works really great. public function setOptions($options = NULL) { if($options !== NULL) { foreach($options as $f => $v) { $new_options[$f] = '"' . $v . '"'; } } else { $new_options = $options; } $this->_options = $new_options; } ... // I modified this function to build the query string just before submitting // and it converts %22 to QUOTATION marks before returning. private function _getJsonPath($path) { $url = $this->_baseURI; $path = ltrim($path, '/'); $auth = ($this->_token == '') ? '' : 'auth=' . $this->_token; if($this->_options != NULL) { $data = http_build_query($this->_options); $auth = "?". str_replace('%22','"',$data) . "&" . $auth; } else { $auth = "?" . $auth; } $new_url = $url . $path . '.json' . $auth; return $new_url;

}

ktamas77 commented 8 years ago

Query parameters are supported in version v2.1.0

https://github.com/ktamas77/firebase-php/releases/tag/v2.1.0