2

我正在尝试ENV从用户端动态设置一些值。我想使用多个短信网关服务,以便用户可以使用多个网关中的任何一个。多个服务有不同api的,sidtoken。我将这些服务提供商凭据保存在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;
}
4

1 回答 1

0

可能有更简单的方法。向 sms_gateways 表添加两个名为 sid 和 token 的字段怎么样?

Schema::table('sms_gateways', function (Blueprint $table) {
    $table->string('sid');
    $table->string('token');
})

因此,从控制器中,您可以这样说:

$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);
于 2019-06-20T04:36:03.703 回答