|
|
|
|
@ -359,6 +359,35 @@ function curl($url, $post = array(), $arr_return = true)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Submail xsend POST,返回解码结果与原始响应(便于日志与排错)。
|
|
|
|
|
*
|
|
|
|
|
* @return array{decoded: array|null, raw: string, curl_errno: int, curl_error: string}
|
|
|
|
|
*/
|
|
|
|
|
function submail_message_xsend_post($url, $postBody)
|
|
|
|
|
{
|
|
|
|
|
$curl = curl_init();
|
|
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
|
|
|
|
|
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
|
|
|
|
|
curl_setopt($curl, CURLOPT_URL, $url);
|
|
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
|
curl_setopt($curl, CURLOPT_POST, true);
|
|
|
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $postBody);
|
|
|
|
|
$raw = curl_exec($curl);
|
|
|
|
|
$errno = curl_errno($curl);
|
|
|
|
|
$error = curl_error($curl);
|
|
|
|
|
curl_close($curl);
|
|
|
|
|
$rawStr = ($raw === false) ? '' : (string) $raw;
|
|
|
|
|
$decoded = json_decode($rawStr, true);
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'decoded' => is_array($decoded) ? $decoded : null,
|
|
|
|
|
'raw' => $rawStr,
|
|
|
|
|
'curl_errno' => $errno,
|
|
|
|
|
'curl_error' => $error,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function is_wechat()
|
|
|
|
|
{
|
|
|
|
|
return strpos($_SERVER["HTTP_USER_AGENT"], "MicroMessenger");
|
|
|
|
|
@ -372,23 +401,70 @@ function sms($to, $vars, $template_id, $belongs_type = null, $belongs_id = null)
|
|
|
|
|
is_array($vars) ? $vars = json_encode($vars) : '';
|
|
|
|
|
$data = "appid=" . $sms_app_id . "&signature=" . $sms_app_key . "&project=" . $template_id . "&vars=" . $vars . "&to=" . $to;
|
|
|
|
|
$url = "https://api.submail.cn/message/xsend.json";
|
|
|
|
|
$return = curl($url, $data);
|
|
|
|
|
|
|
|
|
|
\Illuminate\Support\Facades\Log::info('sms.submail.request', [
|
|
|
|
|
'to' => $to,
|
|
|
|
|
'template_id' => $template_id,
|
|
|
|
|
'vars' => $vars,
|
|
|
|
|
'appid_set' => ($sms_app_id !== null && $sms_app_id !== ''),
|
|
|
|
|
'appkey_set' => ($sms_app_key !== null && $sms_app_key !== ''),
|
|
|
|
|
'belongs_type' => $belongs_type,
|
|
|
|
|
'belongs_id' => $belongs_id,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$http = submail_message_xsend_post($url, $data);
|
|
|
|
|
$return = $http['decoded'];
|
|
|
|
|
|
|
|
|
|
if ($http['curl_errno'] !== 0) {
|
|
|
|
|
\Illuminate\Support\Facades\Log::error('sms.submail.curl_failed', [
|
|
|
|
|
'to' => $to,
|
|
|
|
|
'template_id' => $template_id,
|
|
|
|
|
'curl_errno' => $http['curl_errno'],
|
|
|
|
|
'curl_error' => $http['curl_error'],
|
|
|
|
|
'body_preview' => mb_substr($http['raw'], 0, 800),
|
|
|
|
|
]);
|
|
|
|
|
$return = ['status' => 'error', 'code' => 'curl', 'msg' => $http['curl_error'] ?: 'curl_exec failed'];
|
|
|
|
|
} elseif (!is_array($return)) {
|
|
|
|
|
\Illuminate\Support\Facades\Log::error('sms.submail.invalid_json', [
|
|
|
|
|
'to' => $to,
|
|
|
|
|
'template_id' => $template_id,
|
|
|
|
|
'json_error' => json_last_error_msg(),
|
|
|
|
|
'body_preview' => mb_substr($http['raw'], 0, 800),
|
|
|
|
|
]);
|
|
|
|
|
$return = ['status' => 'error', 'code' => 'json', 'msg' => 'invalid json response'];
|
|
|
|
|
} else {
|
|
|
|
|
if (($return['status'] ?? '') === 'success') {
|
|
|
|
|
\Illuminate\Support\Facades\Log::info('sms.submail.response', [
|
|
|
|
|
'to' => $to,
|
|
|
|
|
'template_id' => $template_id,
|
|
|
|
|
'status' => $return['status'] ?? null,
|
|
|
|
|
]);
|
|
|
|
|
} else {
|
|
|
|
|
\Illuminate\Support\Facades\Log::warning('sms.submail.api_rejected', [
|
|
|
|
|
'to' => $to,
|
|
|
|
|
'template_id' => $template_id,
|
|
|
|
|
'status' => $return['status'] ?? null,
|
|
|
|
|
'code' => $return['code'] ?? null,
|
|
|
|
|
'msg' => $return['msg'] ?? null,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$sms['mobile'] = $to;
|
|
|
|
|
$sms['vars'] = $vars;
|
|
|
|
|
$sms['template_id'] = $template_id;
|
|
|
|
|
$sms['ip'] = request()->getClientIp();
|
|
|
|
|
$sms['status'] = $return['status'];
|
|
|
|
|
$sms['status'] = $return['status'] ?? 'error';
|
|
|
|
|
$sms['belongs_type'] = $belongs_type;
|
|
|
|
|
$sms['belongs_id'] = $belongs_id;
|
|
|
|
|
if ($return['status'] != "success") {
|
|
|
|
|
$sms['result_code'] = $return['code'];
|
|
|
|
|
$sms['result_msg'] = $return['msg'];
|
|
|
|
|
if (($return['status'] ?? '') != "success") {
|
|
|
|
|
$sms['result_code'] = $return['code'] ?? null;
|
|
|
|
|
$sms['result_msg'] = $return['msg'] ?? null;
|
|
|
|
|
}
|
|
|
|
|
$sms['created_at'] = date("Y-m-d H:i:s");
|
|
|
|
|
(new \App\Models\Sms())->insert($sms);
|
|
|
|
|
|
|
|
|
|
return $return['status'] == "success";
|
|
|
|
|
return ($return['status'] ?? '') == "success";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function get_http_type()
|
|
|
|
|
|