How to recover variables sent by Angular in PHP?


I'm trying to send a data by getting from an Angular 5 service to an API in PHP.

The way I send it is like this:

public getMembers$(id): Observable<any> {
  return this.http.get(this.URL + 'read_records.php?id=1');

The variable this.URL comes from the environments and it's not a problem. The API is reached by the service, that's what I see in the network call.

The problem is that in the API I can not recover the id variable sent. I've tried it with $_GET and with file_get_contents("php://input") and there's no way.

What is the correct way to retrieve variables in the API when sent from Angular? For the method if the php://input works for me (provided that what I send is serialized in a json), but there are more types of submissions (send by post of files, send by get, or put or delete).

How can I recover that data in PHP?

asked by Chefito 23.06.2018 в 14:12

1 answer


I do not see that it shows the code of its API but ...

Try with:

$request = json_decode(file_get_contents('php://input'));

You can put this at the beginning of your PHP script. $request then it will be a stdClass object with the data as properties.

$id = $request->id;

Alternatively, if you prefer to work with it as an associative matrix, use:

$request = json_decode(file_get_contents('php://input'), TRUE);

and access data such as:

$id = $request['id'];

I hope it helps you!

answered by 23.06.2018 / 14:36