当前位置:网站首页>Alibaba big fish SMS interface PHP version, simplified version Alibaba big fish SMS sending interface PHP instance
Alibaba big fish SMS interface PHP version, simplified version Alibaba big fish SMS sending interface PHP instance
2022-06-22 09:03:00 【YUJIANYUE】
201803 Updated to the official compact version
There are only two documents , No compression 6KB about
file 1 :Signature.php
<?php
/*
* Please make sure that the file is utf-8 code , And replace the corresponding parameters with your own information , And uncomment the related calls
* come from 2017/11/30 Alibaba cloud SMS official DEMO
*/
namespace Aliyun\DySDKLite;
/**
* Signature assistant 2017/11/19
*
* Class SignatureHelper
*/
class SignatureHelper {
/**
* Generate a signature and initiate a request
*
* @param $accessKeyId string AccessKeyId (https://ak-console.aliyun.com/)
* @param $accessKeySecret string AccessKeySecret
* @param $domain string API The domain name of the interface
* @param $params array API Specific parameters
* @param $security boolean Use https
* @return bool|\stdClass return API Interface call result , Returns... When an error occurs false
*/
public function request($accessKeyId, $accessKeySecret, $domain, $params, $security=false) {
$apiParams = array_merge(array (
"SignatureMethod" => "HMAC-SHA1",
"SignatureNonce" => uniqid(mt_rand(0,0xffff), true),
"SignatureVersion" => "1.0",
"AccessKeyId" => $accessKeyId,
"Timestamp" => gmdate("Y-m-d\TH:i:s\Z"),
"Format" => "JSON",
), $params);
ksort($apiParams);
$sortedQueryStringTmp = "";
foreach ($apiParams as $key => $value) {
$sortedQueryStringTmp .= "&" . $this->encode($key) . "=" . $this->encode($value);
}
$stringToSign = "GET&%2F&" . $this->encode(substr($sortedQueryStringTmp, 1));
$sign = base64_encode(hash_hmac("sha1", $stringToSign, $accessKeySecret . "&",true));
$signature = $this->encode($sign);
$url = ($security ? 'https' : 'http')."://{$domain}/?Signature={$signature}{$sortedQueryStringTmp}";
try {
$content = $this->fetchContent($url);
return json_decode($content);
} catch( \Exception $e) {
return false;
}
}
private function encode($str)
{
$res = urlencode($str);
$res = preg_replace("/\+/", "%20", $res);
$res = preg_replace("/\*/", "%2A", $res);
$res = preg_replace("/%7E/", "~", $res);
return $res;
}
private function fetchContent($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"x-sdk-client" => "php/2.0.0"
));
if(substr($url, 0,5) == 'https') {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}
$rtn = curl_exec($ch);
if($rtn === false) {
trigger_error("[CURL_" . curl_errno($ch) . "]: " . curl_error($ch), E_USER_ERROR);
}
curl_close($ch);
return $rtn;
}
}file 2:sendsms.php
<?php
/*
* This file is used to verify the SMS service API Interface , For reference during development
* Please make sure that the file is utf-8 code , And replace the corresponding parameters with your own information , And uncomment the related calls
* come from 2017/11/30 Alibaba cloud SMS official DEMO
*/
namespace Aliyun\DySDKLite\Sms;
require_once "../Signature.php";
use Aliyun\DySDKLite\SignatureHelper;
/**
* Send a text message
*/
function sendSms() {
$params = array ();
// *** The part to be filled in by the user ***
// fixme Required : see also https://ak-console.aliyun.com/ Get your AK Information
$accessKeyId = "*****************";
$accessKeySecret = "**********************";
// fixme Required : SMS number
$params["PhoneNumbers"] = "19999999999";
// fixme Required : SMS signature , We should strictly abide by " Signature name " Fill in , Please refer to : https://dysms.console.aliyun.com/dysms.htm#/develop/sign
$params["SignName"] = " Check the score ";
// fixme Required : SMS template Code, We should strictly abide by " Templates CODE" Fill in , Please refer to : https://dysms.console.aliyun.com/dysms.htm#/develop/template
$params["TemplateCode"] = "SMS_13740380";
// fixme Optional : Set template parameters , If there are variables in the template that need to be replaced, it is required
$params['TemplateParam'] = Array (
"code" => "12345",
"product" => " User registration verification code "
);
// fixme Optional : Set the serial number of sending SMS
$params['OutId'] = "12345";
// fixme Optional : Uplink SMS extension code , The extended code field is controlled in 7 Bit or less , Please ignore this field if there is no special requirement
$params['SmsUpExtendCode'] = "1234567";
// *** The part to be filled in by the user ends , The following code does not need to be changed if not necessary ***
if(!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
$params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
}
// initialization SignatureHelper Instance is used to set parameters , Sign and send request
$helper = new SignatureHelper();
// An exception may be thrown here , Be careful catch
$content = $helper->request(
$accessKeyId,
$accessKeySecret,
"dysmsapi.aliyuncs.com",
array_merge($params, array(
"RegionId" => "cn-hangzhou",
"Action" => "SendSms",
"Version" => "2017-05-25",
))
);
return $content;
}
ini_set("display_errors", "on"); // Display error message , It is only used to troubleshoot problems during testing
set_time_limit(0); // Prevent script timeouts , For testing purposes only , Please set the production environment according to the actual situation
header("Content-Type: text/plain; charset=utf-8"); // Output is utf-8 Text format , For testing only
// Verify sending SMS (SendSms) Interface
print_r(sendSms());边栏推荐
- 15 command mode
- 炒股致富之curl抓股票信息
- Introduction to MySQL database Basics
- Flask博客实战 - 创建后台管理应用
- Flask blog practice - display the navigation menu and home page data of the whole site
- 文件小能手---multer
- Report: in the technical field, men are more likely to get job interviews
- In the monorepo learning, execute NPM run build to report error[err\u require\esm] of ES module
- pip3 install xxx报错:Command 'lsb_release -a' returned non-zero exit status 1.
- 一学就会的tensorflow断点续训(原理+代码详解)
猜你喜欢

Detailed explanation and Simulation of string and memory operation functions
![[tensorboard] step on all minefields and solve all your problems](/img/35/fc0f7ed311bf7c0321e1257ff6a1a6.png)
[tensorboard] step on all minefields and solve all your problems

【node】快收下爬虫,我们不再为数据发愁

ffmpeg之volumedetect

Navicat for MySQL连接MySQL数据库时各种错误解决

When easypoi imports the secondary header data of an excel file, the data in the first column of the @excelentity entity class is null

字符串与内存操作函数详解与模拟实现

Solidity from introduction to practice (V)

The third-party libraries commonly used in golang development are not the most complete, but more complete

PHP login registration page
随机推荐
Flask blog practice - realize article management
Basic knowledge of DDL operation database and operation table and explanation of use format
Manually mining XSS vulnerabilities
微表情数据集汇总(全)
OpenCV每日函数 直方图相关(3)
luogu P4292 [WC2010]重建计划
Yolov5 export GPU inference model export
【node】理论+实践让你拿下session、cookie
Flask blog practice - integrated rich text editor quill
Instanceinforeplicator class of Eureka (service registration assistant)
16 interpreter mode
IP address (IPv4)
XSS vulnerability attack
Pytorch oserror: DLL load failed: problem solving
CF1267G Game Relics
Yolov5 reports an error: attributeerror: 'upsample' object has no attribute 'recommend_ scale_ Solution of 'factor'
Interview shock 59: can there be multiple auto increment columns in a table?
Interview shock 59: can there be multiple auto increment columns in a table?
无线路由攻击和WiFi密码破解实战[渗透技术]
Guide to quick withdrawal and withdrawal of us and Hong Kong stocks