尊龙凯时人生就是博

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
sitemap¡¢ÍøÕ¾µØͼ