2

I am trying to create a API on PHP ( web service ) and almost all the requests i need to do are to get a list of items and those items may have objects with thier own fields or maybe other objects, the api will be used for diferents proyects and not all need the same information so im trying to create a flexible request to get the structure i need on json or xml.

I dont know how to make the request without creating a big url or a big parsers for ea parm.

How can i send a structure on the web resquest???

Actual request/response.

Example request:

Http://myphpapi.php/getEmploees.php

Example response:

{"employees":[
{"firstName":"John", "lastName":"Doe", "Departament":"1"},
{"firstName":"Anna", "lastName":"Smith", "Departament":"2"},
{"firstName":"Peter", "lastName":"Jones", "Departament":"1"}]}

What i want to archive is:

Example request:

Http://myphpapi.php/getEmploees.php?{employess.firstName,Departament.name}

Example response:

{"employees":[
{"firstName":"John", "Departament":{"name":"D1"}},
{"firstName":"Anna", "Departament":{"name":"D2"}},
{"firstName":"Peter", "Departament":{"name":"D1"}}]}

1 Answer 1

1

Just create parameters for the requests ( or use different urls ). At least that's how pretty much all the APIs work:

Request url for item:
GET: http://api.project.com/item/{id}

Request url for list:
GET: http://api.project.com/list/

Request parameter for list:
GET: http://api.project.com/list/
DATA: possible_fields:
[
    "employees":
    [
        "first_name",
        "last_name",
        "departament":
        [
            "name",
            "number",
            "floor",
        ]
    ],
    "bosses"
    [
        "first_name",
        "last_name",
        "departament":
        [
            "name",
            "number",
            "floor",
        ]
    ],
]

And return only the requested items.

$data = [
    "employees" => [
        "first_name",
        "departament" => [
            "name"
        ]
    ],
];

Guzzle::get('http://api.project.com/list/', [
    'headers' => [
        'Content-Type' => 'application/json',
    ],
    'json' => $data,
]);

This, ie, could return only employees first-names and department names.

Sign up to request clarification or add additional context in comments.

2 Comments

Thx!i will use POST and not GET and use data field to send the structure i need.
POST if for posting something, not GETing it. If you look at it as DB queries: SELECT = GET, INSERT = POST, UPDATE = PUT/PATCH, DELETE = DELETE. These are cURL requests, don't mix it up with plain FORM methods.

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.