-1

Im currently working on a back-end for an Ionic App. The App sends following URL request

api/media_objects.php?lat=44.3664658&lng=41.899801&radius=1500&tags=italian%2Crestaurant

My JSON output looks like this

{
"type": "FeatureCollection",
"features": {
    "type": "Feature",
    "geometry": {
        "type": "Point",
        "coordinates": [
            "44.366466",
            "41.898110"
        ]
    },
    "properties": {
        "ID": "3",
        "icon": "www.someurl.com",
        "tags": [
            "italian"
        ],
        "title": "test",
        "description": "test"
    }
}

}

My question is: how can I return the json to the App so the App is actually able to read and use it?

Huondui
  • 383
  • 1
  • 3
  • 12

1 Answers1

0

You can simple use echo.

echo $myJson;

So when your api is executed successfully the end FrontEnd will be able to see the data coming from you in json format and handle it.

If you were using a framework like Laravel, Symfony etc etc you could return a response which would contain your json data but also the HTTP status etc etc but if you are using plain php i guess you are not looking for something complicated.

What you could do to make it better and avoid some issues with the receiver's side would be to add some basic headers:

    header("Access-Control-Allow-Origin: *");  //to avoid CORS issues with the FE
    header("Content-Type: application/json; charset=UTF-8"); 
    header("HTTP/1.1 200 OK");
pr1nc3
  • 8,108
  • 3
  • 23
  • 36
  • Thanks alot! Would you recommend learning laravel for such things? Could you brievly explain how such code would look like in laravel or symfony (if it's not too complicated)? – Huondui Jul 16 '19 at 15:33
  • 1
    Access-Control-Allow-Origin has nothing to do with XSS. You probably meant CORS, but that exists for a reason. Blindly responding with Access-Control-Allow-Origin: * is just as bad as setting 0777 permissions whenever encountering any kind of permission issue. – Peter Jul 16 '19 at 17:22
  • Yes you are right i meant CORS. I agree with your comment that it is bad practise but i was just answering OP's question about the issue. He can and should investigate further on the header/headers he is using. – pr1nc3 Jul 16 '19 at 17:30
  • @php_n00b i would recommend to learn laravel/symfony but not for "such things" but for many many more things. But first focus on getting a good grasp of php and then you can jump to a framework but moving to laravel without a solid understanding of php i would say can cause a lot of trouble and blank spots in your learning path. – pr1nc3 Jul 16 '19 at 17:32