public function sendPost()
{
$url = 'http://xxx.xxxx.com';
$post_data = [
'data1' => '',
'data2' => '',
'data3' =>''
];
$options = array(
'http' => array(
'method' => 'POST',
// header 需要设置为 JSON
'header' => 'Content-type:application/json',
'content' => json_encode($post_data,true),
// 超时时间
'timeout' => 60
)
);
$context = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
return $result;//返回数据格式为json
}

本文介绍了一个使用PHP发送POST请求的示例代码,详细展示了如何设置请求头为JSON格式,构造请求数据,设置超时时间,并接收返回的JSON数据。
1080

被折叠的 条评论
为什么被折叠?



