dlh_test_server/app/common/server/CloudAudioServer.php
2024-12-07 16:31:00 +08:00

81 lines
2.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\common\server;
class CloudAudioServer
{
const URI = 'https://speaker.17laimai.cn';
const TOKEN = 'HK1733377641';
const MESSAGE = '商家收款,';
public static function send($data)
{
try {
$api = '/notify.php';
if (empty($data)) {
throw new \Exception('请填写参数');
}
if (!isset($data['id'])) {
// 指该云音箱标签上的的 SN/ID
throw new \Exception('请输入设备上的SN码');
}
if (!isset($data['version']) && $data['version'] <= 0) {
// 接口版本 按音箱标签上标注的VERSION版本,由用户配置时选择,预留1-9供选择
throw new \Exception('设备版本号输入错误: 1-9');
}
if (empty($data['message'])) {
$data['message'] = self::MESSAGE;
}
$result = self::curlPost($api, $data);
if ($result['errcode'] == 0 && $result['errmsg'] == 'ok') {
return [
'code' => 1,
];
}
return [
'code' => 0,
'msg' => $result['errmsg']
];
} catch (\Exception $e) {
return [
'code' => 0,
'msg' => $e->getMessage()
];
}
}
private static function curlPost($api, $postData)
{
$postData['token'] = self::TOKEN;
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => self::URI . $api,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => http_build_query($postData),
CURLOPT_HTTPHEADER => [
"content-type: application/x-www-form-urlencoded"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
if ($err) {
return "CURL Error #:" . $err;
}
return json_decode($response,true);
}
}