douou9786 2019-07-01 14:52
浏览 142

Php api使用curl调用另一个 - 数据丢失

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
"

Thank you for your help :)

  • 写回答

1条回答 默认 最新

  • douju2474 2019-07-02 12:51
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波