0

I've created two php apis. One of them (API-1) needs to call the other (API-2) through post method. But when the second api is called,, the data is lost.

Website architecture Website architecture

CODE

API 1 : http://mylocalsite.com/auth/register

// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

// get posted data
$data = json_decode(file_get_contents("php://input"));

$url = "http://mylocalsite.com/user/create";
$client = curl_init($url);
curl_setopt($client, CURLOPT_RETURNTRANSFER, true);
curl_setopt($client, CURLOPT_POST, true);
curl_setopt($client, CURLOPT_POSTFIELDS, $data);
curl_setopt($client, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$response = curl_exec($client);
curl_close($client);

API 2 : http://mylocalsite.com/user/create

// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

// get posted data
$data = json_decode(file_get_contents("php://input"));

// display data
var_dump($data);

Data sent

{
    "username" : "anonymous_api",
    "password" : "secret123!",
    "firstname" : "anonymous_api",
    "lastname" : "anonymous_api",
    "email" : "anonymous_api@bookingpro.com",
    "role" : "USER"
}

Expected data

{
    "username" : "anonymous_api",
    "password" : "secret123!",
    "firstname" : "anonymous_api",
    "lastname" : "anonymous_api",
    "email" : "anonymous_api@bookingpro.com",
    "role" : "USER"
}

Actual data

"NULL\n"

Thank you for your help :)

M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
  • Are those two API's using the same application, or are `/auth` and `/user` two different apps that will live on different locations? – M. Eriksson Jul 01 '19 at 14:55
  • You should also pass the post data "as is" (which seems to be serialized json?) instead of doing json_decode() before adding it to the post fields. [Here's a post with a lot of answers/examples](https://stackoverflow.com/questions/11079135/how-to-post-json-data-with-php-curl) – M. Eriksson Jul 01 '19 at 14:58
  • Why not use $_POST instead of json_decode(file_get_contents("php://input")); ? – Tofandel Jul 01 '19 at 18:24
  • Also why make two api calls? Can't you execute the endpoint without using curl? – Tofandel Jul 01 '19 at 18:26
  • They are two different applications but in the same server. – Mino Lazzi Jul 02 '19 at 03:13
  • _“Why not use $_POST”_ - because PHP will not populate $_POST, if the Content-Type of the request was not either `application/x-www-form-urlencoded` or `multipart/form-data`. – 04FS Jul 02 '19 at 07:29

1 Answers1

0

It works. Thank you for your comments And many thanks to @Magnus Eriksson. Indeed the error was that the data is decoded before sending to the second api.