function request_post($url, $data) {
$str = json_encode($data); //如果传的不是json字符串的话需要转一下
$context = array(
'http' =>
array(
'method' => 'POST',
'header' => 'Content-type:application/json;charset=utf-8' . "\r\n" .
// "Authorization:Basic".base64_encode($con['userid'].":".$con['password'])."\r\n".
'Content-length' . strlen($str),
'content' => $str
)
);
$contextid = stream_context_create($context);
$scok = fopen($url, 'r', false, $contextid);
if ($scok) {
$result = '';
while (!feof($scok)) {
$result .= fgets($scok, 4096);
}
fclose($scok);
return $result;
} else {
return $str;
}
}
调用
$new_platform_data=[];
$new_platform_data['imei'] = $device_id;
$new_platform_data['sensor_status'] = $sensor_status;
$new_platform_data['battery_value'] = $battery_value;
$new_platform_data['temperature'] = $temperature;
$new_platform_data['pressure'] = $pressure;
$new_platform_data['wake_up'] = "{$wakehour}:{$wakeminute}:{$wakesecond}";
try {
request_post('http://127.0.0.1:1991/index/getData', $new_platform_data);
} catch (\Exception $e) {
echo_content("旧设备推送数据到新平台出错");
}
正在学习Go语言的PHP程序员。