0

I am running into some strange problem with php firebase implementation, i am using ktamas77/firebase-php and i would like to populate the record via a json doc. But it seems every time i try to insert a json doc via

$success = $firebase->set(1 . '/AwayTeam/' . '123456', $myJSON);

i see a format string in firebase db which looks like this

"{\"user_id\" : \"jack\", \"text\" : \"Ahoy!\"}"

what am i missing here ?

2
  • $myObj->last = "Miller"; $myObj->name = "John"; $myObj->age = 30; $myObj->city = "New York"; $myJSON = json_encode($myObj); Commented Nov 8, 2017 at 21:13
  • Sorry, I deleted my earlier request for more info and posted an answer directly instead after looking up the source code of the firebase-php library. Commented Nov 8, 2017 at 22:28

1 Answer 1

1

As you can see in the firebase-php sourcecode it doesn't accept raw json strings to be entered into the database. Instead the library will perform a json_encode on whatever you put as $data.

Don't do any decoding/encoding if you want to store PHP objects into the database:

$success = $firebase->set(1 . '/AwayTeam/' . '123456', $myObj);

In order to insert raw JSON with said library, you'll have to decode it into a PHP object/array first:

$success = $firebase->set(1 . '/AwayTeam/' . '123456', json_decode($myJSON));
Sign up to request clarification or add additional context in comments.

3 Comments

With the info from your comment, I'd suggest not running any json_* functions at all. You already have a PHP object which you can pass into the set function. (i.e.: instead of $myJSON or json_decode($myJSON) just pass $myObj)
Edited my answer to show that option as well based on the example code form your question comment.
Thanks, but the object was just a sample to see why it didnt work, My real json is quite big. I tested it and it works fine

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.