我正在尝试向URL发送POST请求,我可以通过设置URL和参数数组在POSTMAN中轻松完成。但我需要用PHP发送请求。最简单的方法是什么?下面是一个示例参数数组。
{
"receiver":"01234567890A=",
"min_api_version":1,
"sender":{
"name":"John McClane",
"avatar":"http://avatar.example.com"
},
"tracking_data":"tracking data",
"type":"url",
"media":"http://www.website.com/go_here"
}
答案 0 :(得分:1)
reference :: http://hayageek.com/php-curl-post-get/
$params = array(
"receiver" => "01234567890A=",
"min_api_version" => 1,
"sender" => array(
"name" => "John McClane",
"avatar" => "http://avatar.example.com"
),
"tracking_data" => "tracking data",
"type" => "url",
"media" => "http://www.website.com/go_here"
);
$postData = json_encode($params);
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "data=$postData");
$output=curl_exec($ch);
curl_close($ch);
var_dump($output);