legend3---PHP使用阿里云短信服务
生活随笔
收集整理的這篇文章主要介紹了
legend3---PHP使用阿里云短信服务
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
legend3---PHP使用阿里云短信服務
一、總結
一句話總結:
使用步驟照官方文檔,代碼拷貝即可
?
1、php使用阿里云短信服務的步驟?
入駐阿里云->開通短信服務->獲取AccessKey->創建簽名和模版->短信接口配置->發送短信
?
2、lavarel使用阿里云短信服務需要注意的是?
1、使用按照官方文檔一步步進行就行:地址為https://help.aliyun.com/document_detail/59210.html?spm=a2c4g.11186623.6.558.90af56e03gHGJk
2、代碼官方有示例代碼,不需要你寫,拷貝代碼替換accessKey相關參數即可
3、原理是阿里云根據你的accessKey找到你的阿里云賬號,從而找到短信的簽名和模板,短信的驗證碼我們自己指定即可
4、核心步驟是項目中composer安裝阿里云客戶端:composer require alibabacloud/client
?
3、lavarel使用阿里云短信服務 核心步驟是什么?
1、項目中composer安裝阿里云客戶端:composer require alibabacloud/client
2、將示例代碼拷貝進項目,替換accessKey相關:
<?php use AlibabaCloud\Client\AlibabaCloud; use AlibabaCloud\Client\Exception\ClientException; use AlibabaCloud\Client\Exception\ServerException;// Download:https://github.com/aliyun/openapi-sdk-php // Usage:https://github.com/aliyun/openapi-sdk-php/blob/master/README.md AlibabaCloud::accessKeyClient('<accessKeyId>', '<accessSecret>')->regionId('cn-hangzhou') // replace regionId as you need->asDefaultClient();try {$result = AlibabaCloud::rpc()->product('Dysmsapi')// ->scheme('https') // https | http->version('2017-05-25')->action('SendSms')->method('POST')->host('dysmsapi.aliyuncs.com')->options(['query' => ['RegionId' => "default",'PhoneNumbers' => "11111111111",'SignName' => "讀書編程筆記",'TemplateCode' => "SMS_172220636",'TemplateParam' => "{\"code\":\"222\"}",],])->request();print_r($result->toArray()); } catch (ClientException $e) {echo $e->getErrorMessage() . PHP_EOL; } catch (ServerException $e) {echo $e->getErrorMessage() . PHP_EOL; }記得將<accessKeyId>和<accessSecret>替換為你自己的
?
?
?
二、PHP使用阿里云短信服務
1、使用按照官方文檔一步步進行就行:地址為https://help.aliyun.com/document_detail/59210.html?spm=a2c4g.11186623.6.558.90af56e03gHGJk
2、項目代碼示例:
<?phpnamespace App\Http\Controllers\Home\Login;use App\Http\Controllers\Controller; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB;use Gregwar\Captcha\CaptchaBuilder;//阿里云短信驗證 use AlibabaCloud\Client\AlibabaCloud; use AlibabaCloud\Client\Exception\ClientException; use AlibabaCloud\Client\Exception\ServerException; // Download:https://github.com/aliyun/openapi-sdk-php // Usage:https://github.com/aliyun/openapi-sdk-php/blob/master/README.mdclass RegController extends Controller {//獲取手機驗證碼操作public function phone_code(Request $request){AlibabaCloud::accessKeyClient('<accessKeyId>', '<accessSecret>')->regionId('cn-hangzhou') // replace regionId as you need->asDefaultClient();try {$result = AlibabaCloud::rpc()->product('Dysmsapi')// ->scheme('https') // https | http->version('2017-05-25')->action('SendSms')->method('POST')->host('dysmsapi.aliyuncs.com')->options(['query' => ['RegionId' => "default",'PhoneNumbers' => "11111111111",'SignName' => "讀書編程筆記",'TemplateCode' => "SMS_172220636",'TemplateParam' => "{\"code\":\"88888\"}",],])->request();print_r($result->toArray());} catch (ClientException $e) {echo $e->getErrorMessage() . PHP_EOL;} catch (ServerException $e) {echo $e->getErrorMessage() . PHP_EOL;}dd($request->all());}}核心代碼不需要自己寫,直接拷貝官方的代碼,將下面等參數換成自己的即可,還有電話號碼,短信的簽名和模板等信息
'<accessKeyId>', '<accessSecret>'?
3、發送結果
?
?
?
轉載于:https://www.cnblogs.com/Renyi-Fan/p/11333449.html
總結
以上是生活随笔為你收集整理的legend3---PHP使用阿里云短信服务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ubuntu16.04上安装valgri
- 下一篇: 后盾网lavarel视频项目---php