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Ôõô·¢¶ÌÐŵÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡