如何在laravel中使用SMS webservice?

时间:2016-11-01 18:04:02

标签: web-services laravel sms laravel-5.2

我有通过webservice发送短信的代码(例如sms类文件),我希望在laravel中使用此代码。
如何在我的应用程序中使用此Web服务实现。

1 个答案:

答案 0 :(得分:1)

我不知道你究竟是什么意思,但这是一个通过plivo发送短信的工作类

namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\User;
use Plivo\RestAPI;
class SendSms implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;
    protected $sender;
    protected $list;

    public function __construct($sender,$list)
    {
         $this->sender = $sender;
         $this->list = $list;
         $this->from = env('plivo_from');
         $this->authid = env('plivo_authid');
         $this->token = env('plivo_token');
    }

    public function handle()
    {
         $auth_id = $this->authid;
         $auth_token = $this->token;
         $p = new RestAPI($auth_id, $auth_token);
          Send a message
         $params = array(
                 'src' => $this->from, // Sender's phone number with country code
                 'dst' => $this->list, // receiver's phone number with country code
                 'text' => 'Hi, Message from Plivo' // Your SMS text message
             );
         // Send message
         $response = $p->send_message($params); 
// you can add more code to save error to db or something since this is a job, 

    }
}

然后当你想发送短信时

 $this->dispatch(new SendSms($sender,$list);

你仍然可以在正常的课程/功能中做到这一点,但我希望你有这个想法