嗨,我仍在尝试理解为什么我没有从短信 API 收到任何信息,我只是编写了一些代码来将数据保存在数据库中,并为用户提供了一个视图以查看错误,但是我遇到了一些问题,我看到了徽标错误但 DLR url 中没有错误
我不确定我是否可以发布一些关于 API 的信息,以防万一我感到抱歉。
那是我的代码:
DeliverReport 控制器
<?php
namespace App\Http\Controllers;
use App\DeliveryReport;
use Illuminate\Http\Request;
class DeliveryReportController extends Controller
{
public function index() {
$delivery_reports = DeliveryReport::all();
return view('reports.index', compact('delivery_reports'));
}
public function store(Request $request){
$data = $request->all();
try {
$report = [];
if(!empty($data['custom'])) {
$report['contact_id'] = $data['contact_id'] ?? "";
$report['msg'] = $data['msg'] ?? "";
$report['contact_no'] = $data['contact_no'] ?? "";
}
$report['message_id'] = $data['msgId'];
$report['status'] = $data['event'];
$report['message'] = $data['errorMessage'] ?? "";
$report['error_code'] = $data['errorCode'] ?? "";
DeliveryReport::create($report);
} catch (\Exception $e) {
info('something went wrong in webhook');
info($data);
info($e->getMessage());
}
}
}
我的共同点
<?php
namespace App\Http\Traits;
use App\SmsHistory;
use SMSGate\Client;
use SMSGate\SMSRequest;
use Carbon\Carbon;
use DB;
trait CommonServices {
public function sms_gate_request($number, $msg, $contact_id){
$gate = new Client(env('SMS_GATE_SUBMIT_URL'));
$sms = new SMSRequest;
$sms->setType(Client::TYPE_TEXT)
->setAuthUsername(env('SMS_GATE_USERNAME'))
->setAuthPassword(env('SMS_GATE_PASSWORD'))
->setSender(env('SMS_GATE_SENDER'))
->setReceiver($number)
->setText($msg)
->setCustom(['contact_id' => $contact_id, 'msg' => $msg, 'contact_number' => $number])
->setDlrUrl(route('sms.delivery.store'))
->setDlrMask(Client::DLR_MASK_STANDARD);
try {
$gate->send($sms);
SmsHistory::create([
'phone_number' => $number,
'message' => $msg,
'contact_id' => $contact_id
]);
} catch (\Exception $exc) {
info("Error sending SMS with code: " . $exc->getCode() . " and message: " . $exc->getMessage());
return false;
}
return true;
}
public function checkMessageCooldown($phone_number, $message) {
$days = env('DUPLICATE_SMS_COOLDOWN_DAYS');
$last_sent = Carbon::now()->subDays($days)->toDateTimeString();
$sent_before = DB::table('sms_histories')
->where('phone_number', $phone_number)
->where('message', $message)
->where('created_at', '>', $last_sent)
->exists();
return $sent_before;
}
}
我的看法
@extends('layouts.app')
@section('content')
<head>
<meta name="_token" content="{{ csrf_token() }}">
<title>Kontaktliste</title>
<link rel="stylesheet" href="//cdn.datatables.net/1.10.25/css/jquery.dataTables.min.css">
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js" ></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="panel panel-default">
<div class="panel-heading">
<div class="row">
<div class="col-md-6">
<h3>Delivery Reports</h3>
</div>
</div>
</div>
<div class="panel-body">
<table id="reports-table" class="table table-bordered table-hover">
<thead>
<tr>
<th>Message ID</th>
<th>Contact</th>
<th>Message</th>
<th>Status</th>
<th>Reason (if any error)</th>
</tr>
</thead>
<tbody>
@foreach($delivery_reports as $report)
<tr>
<td>{{ $report->message_id }}</td>
<td>{{ $report->contact_id }}</td>
<td>{{ $report->message }}</td>
<td>{{ $report->status }}</td>
<td>{{ $report->error_message }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
<script src="//cdn.datatables.net/1.10.24/js/jquery.dataTables.min.js" defer></script>
<script type="text/javascript">
$(function () {
$('#reports-table').DataTable();
});
</script>
<script type="text/javascript">
$.ajaxSetup({ headers: { 'csrftoken' : '{{ csrf_token() }}' } });
</script>
</body>
@endsection
我的路线: Route::get('/delivery_reports/store','DeliveryReportController@store')->name('sms.delivery.store');
如果我去页面(url sms.delivery.store) 我有一个空白页面,显然没有数据保存在 Db 中,也没有在视图中保存