我正在尝试从用户端动态设置一些ENV
值。我想使用多个短信网关服务,以便用户可以使用多个网关中的任何一个。多个服务具有不同的api
,sid
或token
。我将这些服务提供者凭据保存在sms_gateways
表中,并尝试使用此表中的这些凭据,而不是控制器方法中的手动代码。但是,这样使用我很麻烦。我正在尝试类似的东西-
public function storeSMS(Request $request)
{
$smsGateway = $request->smsgatewayname;
if ($smsGateway['value'] == 'Twilio') {
$twilio = SmsGateway::where('name', 'Twilio')->first();
//return $twilio->sid;
//*here I want to use $twilio->sid value inside env. Is it possible?
$sid = env( 'TWILIO_SID' );
$token = env( 'TWILIO_TOKEN' );
$client = new Client($sid, $token);
$request->validate([
'text'=>'required',
'contact'=>'required'
]);
$now = Carbon::now();
$datetime = explode(' ', $now);
$numbers_in_arrays = explode( ',' , $request->contact );
$message = $request->text;
$count = 0;
foreach( $numbers_in_arrays as $number )
{
$count++;
$client->messages->create(
'+'.$number,
[
'from' => env( 'TWILIO_FROM' ),
'body' => $message,
]
);
$outbox = new SmsOutbox();
$outbox->contact = '+'.$number;
$outbox->text = $message;
$outbox->send_date = $datetime[0];
$outbox->send_time = $datetime[1];
$outbox->is_send = 1;
$outbox->save();
}
}
return;
}
答案 0 :(得分:0)
可能有一种更简单的方法。要将两个名为sid和token的字段添加到sms_gateways表中,该怎么办?
$gateway = SmsGateway::where('name', $request->smsgatewayname)->first();
$client = new Client($gateway->sid, $gateway->token);
// Or you can also make it even simpler.
// $client = new Client($gateway);
因此,在控制器中,您可以说类似以下内容:
type