We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to parse an image from JSON in REST Api

Hello guys,

I'm working on a API for a mobile app. It will be quite big so I have two questions that I don't find any answear.

  1. How to pares images that the mobile app will send to my server? Images will be sent in the JSON with POST
  2. It's normal to have all the routes of the API in the index file, like in the tutorial No.3 "RESTful API"? This will be a big API so the index file will get very big.

Here is my Post code:

$app->post('/image/{id:[0-9]+}', function() use ($app){ $user = $app->request->getJsonRawBody();

Thank you so much!

  1. You have to base64_encode on your mobile app, and base64_decode in your server app. You can learn about BSON (binary json), maybe you can use it too.

  2. example is only example not one and only patter which you have to setup, you can split index.php as you like, include your routes from other files, lile in example for full MVC.

Thank you for your answear! I know about BSON but I think, for me, is more suitable to do it with URL.

I wonder If you have a pice of code about how save image into a folder and return the url? Or if you have the documentation about this?

I will write evening, now i am at work. Please write what you want to do step by step.

Ok, it wil be very helpful

  1. First thing I want to ask if I can create an API from a Simple application (the one that is generate by the dev tool from command line)? Can I use controllers to manage the requests? It's very hard to understand for me. Where will I put the PhQL querries?

  2. Second: the mobile app will send me 3 photoes with http. What is the best way to send or parse them? In json? or with parameters? If with parameters how will I parse it with Phalcon.

  3. Is there a book or something about phalcon? The documentation is good but it gives to many ways to do the same thing that really confuses me. I just need an example of how to do a good API and a website in one project.

Thank you very much again!

I posted some resources for you in Stack Overflow that would help you with your task.

https://stackoverflow.com/questions/22931201/how-to-parse-an-image-from-json-in-phalcon



39.3k
Accepted
answer

Answers to your questions

  1. First thing I want to ask if I can create an API from a Simple application (the one that is generate by the dev tool from command line)? Can I use controllers to manage the requests? It's very hard to understand for me. Where will I put the PhQL querries?

You can do anything you like :) You can use the micro framework or the full blown framework that Phalcon offers. Where you put your PHQL queries is entirely up to you. Some people put them in their models, while others put them in their controllers. There are endless discussions as to which approach is the coorrect one. For me, whatever works for you.

  1. Second: the mobile app will send me 3 photoes with http. What is the best way to send or parse them? In json? or with parameters? If with parameters how will I parse it with Phalcon.

If the mobile app sends you binary data (aka photos) you can get them from the POST array and then parse them appropriately (see my previous reply about JSON resources in SO). You can create a class that will do just that i.e. parse binary data from responses or put that functionality in a controller that would do this.

  1. Is there a book or something about phalcon? The documentation is good but it gives to many ways to do the same thing that really confuses me. I just need an example of how to do a good API and a website in one project.

You can have a look at the available tutorials. Additionally you can google the API creation terms and see implementations from different languages. The code of those languages can be easily adapted to work with Phalcon.

Any other question - we are here to help :)

Thank you again Nikolaos. It seems that in Phalcon comunity, are very nice people.

I already started doing the api with simple application and it works perfectly. I putted the queries in controllers.

Have a very nice day!

https://stackoverflow.com/questions/22931201/how-to-parse-an-image-from-json-in-phalcon

Here is the url with answears to stackoverflow, if someone else is seeking to resolve the same problem.