一般都是用curl扩展来完成,看了手册的通过stream的方式更加简单。
请求脚本stream.php
$url = 'http://localhost/stream_api.php'; $body = [
'name'=>'lemon',
'age'=>20,
'sex'=>'gener'
]; $opts = [
'http'=>[
'method'=>'POST', //这里区分大小写
'content'=>json_encode($body),
'header'=>'Content-type:application/x-www-form-urlencoded'
]
];
//创建流上下文
$context = stream_context_create($opts);
//将上下文传入
$res = file_get_contents($url,false,$context);
print_r($res);
接收 stream_api.php
<?php
header('Content-type:application/json');
$body = @file_get_contents('php://input');
//file_put_contents('2.php',$body);
$data = json_decode($body,true);
$data['name'] = 'jack'; echo json_encode($data);
最后打印出['name'=>'jack',......]