20,360
社区成员
发帖
与我相关
我的任务
分享
$data = array("name" => "testname", "age" => "25");
$data_string = json_encode($data);
$ch = curl_init('http://localhost/test/a.php');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen($data_string)));
$data = curl_exec($ch);
print_r($data);
b.php
1.echo file_get_contents("php://input");
2.echo $GLOBALS["HTTP_RAW_POST_DATA"];
1或2均可获取到由a.php页面传过来的json数据
$post_data =
array(
'name'=>'aaa',
'age'=>45,
);
$url='http://www.example.com/example.php';
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result=curl_exec($ch);
echo $result;