×ðÁú¿­Ê±¹ÙÍøµÇ¼

laravelÔõô·¢¶ÌÐÅ

laravelÊÇÒ»¸öºÜÊÇÊ¢ÐеĿªÔ´php¿ò¼Ü£¬ËüÌṩÁËһϵÁÐÀû±ãµÄ¹¦Ð§£¬ÉÁ¿ª·¢Ö°Ô±¿ÉÒÔ¸ü¿ìËٵع¹½¨webÓ¦ÓóÌÐò¡£ÆäÖÐÖ®Ò»¾ÍÊÇ·¢ËͶÌÐÅ¡£ÔÚÕâƪÎÄÕÂÖУ¬ÎÒÃǽ«Ñ§Ï°ÔõÑùʹÓÃlaravel·¢ËͶÌÐÅ¡£

ÔÚLaravelÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýʹÓöÌÐÅЧÀÍÌṩÉ̵ÄAPIÀ´·¢ËͶÌÐÅ¡£³£ÓõĶÌÐÅЧÀÍÌṩÉÌÓÐTwilio¡¢NexmoºÍPlivoµÈ¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«½ÓÄÉTwilio×÷ΪÎÒÃǵĶÌÐÅЧÀÍÌṩÉÌ¡£

ʹÓÃTwilio·¢ËͶÌÐÅÐèҪװÖÃTwilio SDK£¬Äã¿ÉÒÔʹÓÃComposerÀ´×°ÖÃËü¡£ÔÚÄãµÄÖÕ¶ËÉÏ£¬ÊäÈëÒÔÏÂÏÂÁ

composer require twilio/sdk

µÇ¼ºó¸´ÖÆ

×°ÖÃÍê³Éºó£¬ÔÚconfig/app.phpÉèÖÃÎļþÖÐÌí¼ÓÒÔÏÂÄÚÈÝ£º

'providers' => [
  ...
  Twilio\Rest\ClientServiceProvider::class,
],

'aliases' => [
  ...
  'Twilio' => Twilio\Rest\ClientFacade::class,
],

µÇ¼ºó¸´ÖÆ

Õâ»á½«Twilio SDKÌí¼Óµ½LaravelÓ¦ÓóÌÐòÖС£

½ÓÏÂÀ´£¬ÎÒÃÇÐèÒªÉèÖÃTwilioÕÊ»§µÄÐÅÏ¢¡£ÔÚ.envÎļþÖÐÌí¼ÓÒÔÏÂÄÚÈÝ£º

TWILIO_SID=your-account-sid
TWILIO_AUTH_TOKEN=your-auth-token
TWILIO_PHONE_NUMBER=your-twilio-phone-number

µÇ¼ºó¸´ÖÆ

ÕâÀïµÄyour-account-sidºÍyour-auth-tokenÊÇÄãµÄTwilioÕÊ»§µÄSIDºÍAuth Token£¬your-twilio-phone-numberÊÇÄãµÄTwilioÕÊ»§µÄµç»°ºÅÂë¡£

½ÓÏÂÀ´£¬ÎÒÃÇ¿ÉÒÔ±àд·¢ËͶÌÐŵĴúÂë¡£ÔÚÎÒÃŵÄLaravelÓ¦ÓóÌÐòÖУ¬ÎÒÃÇ¿ÉÒÔÔÚ¿ØÖÆÆ÷ÖÐʹÓÃÒÔÏ´úÂëÀ´·¢ËͶÌÐÅ£º

use Illuminate\Http\Request;
use Twilio\Rest\Client;

class SMSController extends Controller
{
  public function sendSMS(Request $request)
  {
    $accountSid = env('TWILIO_SID');
    $authToken = env('TWILIO_AUTH_TOKEN');
    $twilioPhoneNumber = env('TWILIO_PHONE_NUMBER');

    $client = new Client($accountSid, $authToken);

    $client->messages->create(
        $request->input('recipient'), // ÎüÊÕÈ˵ĵ绰ºÅÂë
        [
            'from' => $twilioPhoneNumber,
            'body' => $request->input('message') // ¶ÌÐÅÄÚÈÝ
        ]
    );
  }
}

µÇ¼ºó¸´ÖÆ

ÒÔÉÏ´úÂëÖУ¬ÎÒÃÇÊ×ÏÈ»ñÈ¡.envÎļþÖеÄTwilioÕÊ»§ÐÅÏ¢£¬È»ºóͨ¹ý½¨ÉèTwilio¿Í»§¶ËÀ´·¢ËͶÌÐÅ¡£

´úÂëÖеÄ$message±äÁ¿ÊÇÎÒÃÇÒª·¢Ë͵ĶÌÐÅÄÚÈÝ£¬$recipient±äÁ¿ÊÇÎÒÃÇÒª·¢ËͶÌÐŵÄÎüÊÕÈ˵绰ºÅÂë¡£ÕâÀïÎÒÃǼÙÉèÎÒÃÇµÄ±íµ¥ÖÐÓÐÒ»¸öÃûΪmessageµÄÊäÈë¿òºÍÒ»¸öÃûΪrecipientµÄÊäÈë¿ò¡£

LaravelµÄ±íµ¥ÑéÖ¤¹¦Ð§¿ÉÒÔÈ·±£ÎÒÃÇµÄ±íµ¥ÊäÈëÓÐÓá£ÀýÈ磬ÎÒÃÇ¿ÉÒÔÔÚ¿ØÖÆÆ÷µÄ´æ´¢ÒªÁìÖвåÈëÒÔÏ´úÂëÀ´ÑéÖ¤±íµ¥ÊäÈ룺

use Illuminate\Http\Request;
use Twilio\Rest\Client;
use Illuminate\Support\Facades\Validator;

class SMSController extends Controller
{
  public function sendSMS(Request $request)
  {
    $validator = Validator::make($request->all(), [
        'recipient' => 'required',
        'message' => 'required',
    ]);

    if ($validator->fails()) {
        return redirect('sms')
                    ->withErrors($validator)
                    ->withInput();
    }

    $accountSid = env('TWILIO_SID');
    $authToken = env('TWILIO_AUTH_TOKEN');
    $twilioPhoneNumber = env('TWILIO_PHONE_NUMBER');

    $client = new Client($accountSid, $authToken);

    $client->messages->create(
        $request->input('recipient'), // ÎüÊÕÈ˵ĵ绰ºÅÂë
        [
            'from' => $twilioPhoneNumber,
            'body' => $request->input('message') // ¶ÌÐÅÄÚÈÝ
        ]
    );
  }
}

µÇ¼ºó¸´ÖÆ

ͨ¹ýÕâ¸öÑéÖ¤£¬ÎÒÃÇ¿ÉÒÔÈ·±£recipient×ֶκÍmessage×ֶζ¼±»ÌîдÁË¡£

µ±ÎÒÃÇÔÚ±íµ¥ÖÐÌîдÍê¶ÌÐÅÄÚÈݲ¢µã»÷·¢ËÍ°´Å¥Ê±£¬SMSControllerÖеÄsendSMSÒªÁ콫±»Å²Óã¬sendSMSÒªÁ콫ʹÓÃTwilio SDK·¢ËͶÌÐÅ¡£

×ܽá

ÔÚ±¾ÎÄÖУ¬ÎÒÃÇѧϰÁËÔõÑùÔÚLaravelÖÐʹÓÃTwilio·¢ËͶÌÐÅ¡£Í¨¹ý¼¸ÐдúÂ룬ÎÒÃÇ¿ÉÒÔºÜÈÝÒ׵ؽ«¶ÌÐŹ¦Ð§Ìí¼Óµ½ÎÒÃǵÄÓ¦ÓóÌÐòÖУ¬Ê¹ÎÒÃǵÄÓ¦ÓóÌÐòÔ½·¢Ç¿Ê¢¡¢¸»ºñºÍÒ×ÓÚʹÓá£

ÒÔÉϾÍÊÇlaravelÔõô·¢¶ÌÐŵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
ÍøÕ¾µØͼ