Python
import urllib.request import urllib.parse import json # 接口地址和参数 url = "https://api.juncyun.com/api/msgService/sendSms" headers = { "Accept": "application/json", 'content-type': "application/json;charset=utf-8" } data = { #接入账号,从云极个人中心获取 'accessKey': "这里填accessKey", #接入密钥,从云极个人中心获取 'accessSecret': "这里填accessSecret", #短信签名code,从云极个人中心获取 'signCode': "这里填短信签名code", #短信模板code,从云极个人中心获取 'templateCode': "这里填短信模版code", #手机号,仅支持单个 'phone': "这里填目标手机号码", #如模板有变量,根据自己模板数量按顺序填写,格式如下;如果模板无变量则为空 'params': [ "1234" ], #短信类型(必填, Integer类型)1:验证码;2:短信通知;3:会员营销;4:推广短信 'msgType': 这里填短信类型 } # 发送post请求,获取返回值 response = urllib.request.Request(url, headers=headers, data=json.dumps(data).encode('utf-8')) response = urllib.request.urlopen(response) result = response.read().decode('utf-8') # 打印返回值 print(result)
PHP
<?php function phpSendMessage(){ //接入账号,从云极个人中心获取 $accessKey = "这里填accessKey"; //接入密钥,从云极个人中心获取 $accessSecret = "这里填accessSecret"; //短信类型(必填, Integer类型)1:验证码;2:短信通知;3:会员营销;4:推广短信 $msgType = '这里填短信类型'; //短信签名code,从云极个人中心获取 $signCode = '这里填短信签名code'; //短信模板code,从云极个人中心获取 $templateCode = '这里填短信模版code'; //手机号,仅支持单个 $phone = '这里填目标手机号码'; //如模板有变量,根据自己模板数量按顺序填写,格式如下;如果模板无变量则为空 $params = [ "1234", ]; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// $data = [ "accessKey" => $accessKey, "accessSecret" => $accessSecret, "msgType" => $msgType, "signCode" => $signCode, "templateCode" => $templateCode, "phone" => $phone, "params" => $params ]; if(empty($params)){ unset($data['params']); } //curl请求 $ch = curl_init("https://api.juncyun.com/api/msgService/sendSms"); //请求的URL地址 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));//$data JSON类型字符串 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Content-Length: ' . strlen(json_encode($data)))); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $output = curl_exec($ch); if($output === false) { //请求失败,打印失败原因 echo 'Curl error: ' . curl_error($ch); } curl_close($ch); print_r($output."\n"); } ?>
Java
import cn.hutool.http.HttpRequest; import cn.hutool.http.HttpResponse; import com.alibaba.fastjson.JSON; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class SendMsgUtil { private static final String URL = "https://api.juncyun.com/api/msgService/sendSms"; public static String sendMsg(){ Map<String,Object> params = new HashMap<>(10); //接入账号(必填),从云极个人中心获取 params.put("accessKey", "这里填accessKey"); //接入密钥(必填),从云极个人中心获取 params.put("accessSecret", "这里填accessSecret"); //短信类型(必填, Integer类型)1:验证码;2:短信通知;3:会员营销;4:推广短信 params.put("msgType", "这里填短信类型"); //短信签名code(必填),从云极个人中心获取 params.put("signCode", "这里填短信签名code"); //短信模板code(必填),从云极个人中心获取 params.put("templateCode", "这里填短信模版code"); //发送手机号(必填) params.put("mobile", "这里填目标手机号码"); /* 变量参数使用list存储, 如果模板无变量则为空; 如模板有变量,根据自己模板数量 按顺序 填写,格式如下; */ // 验证码参数示例 List<String> list = new ArrayList<>(); list.add("这里填写验证码随机数"); // 变量参数存入json对象 params.put("params", list); // 添加头部信息(必填) Map<String,String> headers = new HashMap<>(1); headers.put("content-type", "application/json;charset=UTF-8"); // 发起请求 HttpResponse httpResponse = HttpRequest.post(URL) .addHeaders(headers).body(JSON.toJSONString(params)).execute(); return httpResponse.body(); } }
Go
package main import ( "bytes" "encoding/json" "fmt" "io" "net/http" ) const URL = "https://api.juncyun.com/api/msgService/sendSms" func sendMsg() string { // 替换为你实际的访问密钥、短信类型、签名代码、模板代码和手机号码 params := map[string]interface{}{ "accessKey": "YOUR_ACCESS_KEY", "accessSecret": "YOUR_ACCESS_SECRET", "msgType": "YOUR_MESSAGE_TYPE", // 例如:"1" 代表验证码 "signCode": "YOUR_SIGN_CODE", "templateCode": "YOUR_TEMPLATE_CODE", "mobile": "TARGET_PHONE_NUMBER", } // 如果有验证码等参数,添加到列表中 list := []string{"此处是随机验证码"} params["params"] = list jsonParams, err := json.Marshal(params) if err != nil { fmt.Println("Error marshalling JSON:", err) return "" } req, err := http.NewRequest("POST", URL, bytes.NewBuffer(jsonParams)) if err != nil { fmt.Println("Error creating request:", err) return "" } req.Header.Set("Content-Type", "application/json;charset=UTF-8") client := &http.Client{} resp, err := client.Do(req) if err != nil { fmt.Println("Error sending request:", err) return "" } defer func(Body io.ReadCloser) { err := Body.Close() if err != nil { } }(resp.Body) var bodyBytes []byte _, err = resp.Body.Read(bodyBytes) if err != nil { return "" } return string(bodyBytes) } func main() { response := sendMsg() fmt.Println(response) }
C
#include <windows.h> #include <wininet.h> #include <stdio.h> #include <string.h> #define URL "https://api.juncyun.com/api/msgService/sendSms" #define BUFFER_SIZE 4096 char *sendMsg(const char *accessKey, const char *accessSecret, const char *msgType, const char *signCode, const char *templateCode, const char *mobile, const char *param) { HINTERNET hInternet = InternetOpen("HTTPPOST", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0); if (hInternet == NULL) { printf("Failed to initialize WinINet\n"); return NULL; } HINTERNET hConnect = InternetConnect(hInternet, "api.juncyun.com", INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0); if (hConnect == NULL) { printf("Failed to connect to server\n"); InternetCloseHandle(hInternet); return NULL; } HINTERNET hRequest = HttpOpenRequest(hConnect, "POST", "/api/msgService/sendSms", NULL, NULL, NULL, INTERNET_FLAG_SECURE, 0); if (hRequest == NULL) { printf("Failed to open HTTP request\n"); InternetCloseHandle(hConnect); InternetCloseHandle(hInternet); return NULL; } // 构建请求体数据 char postData[BUFFER_SIZE]; snprintf(postData, BUFFER_SIZE, "{\"accessKey\":\"%s\",\"accessSecret\":\"%s\",\"msgType\":\"%s\",\"signCode\":\"%s\",\"templateCode\":\"%s\",\"mobile\":\"%s\",\"params\":[\"%s\"]}", accessKey, accessSecret, msgType, signCode, templateCode, mobile, param); // 设置 HTTP 请求头 char headers[] = "content-Type: application/json;charset=UTF-8"; if (!HttpAddRequestHeaders(hRequest, headers, strlen(headers), HTTP_ADDREQ_FLAG_ADD)) { printf("Failed to set request headers\n"); InternetCloseHandle(hRequest); InternetCloseHandle(hConnect); InternetCloseHandle(hInternet); return NULL; } // 发送请求 if (!HttpSendRequest(hRequest, NULL, 0, postData, strlen(postData))) { printf("Failed to send request\n"); InternetCloseHandle(hRequest); InternetCloseHandle(hConnect); InternetCloseHandle(hInternet); return NULL; } // 读取服务器响应 char *response = (char *)malloc(BUFFER_SIZE); if (response == NULL) { printf("Memory allocation error\n"); InternetCloseHandle(hRequest); InternetCloseHandle(hConnect); InternetCloseHandle(hInternet); return NULL; } DWORD bytesRead; if (!InternetReadFile(hRequest, response, BUFFER_SIZE, &bytesRead)) { printf("Failed to read response\n"); free(response); InternetCloseHandle(hRequest); InternetCloseHandle(hConnect); InternetCloseHandle(hInternet); return NULL; } response[bytesRead] = '\0'; // 清理资源 InternetCloseHandle(hRequest); InternetCloseHandle(hConnect); InternetCloseHandle(hInternet); return response; } int main() { // 替换为你实际的访问密钥、短信类型、签名代码、模板代码和手机号码 const char *accessKey = "这里填accessKey"; const char *accessSecret = "这里填accessSecret"; const char *msgType = "1"; const char *signCode = "这里填短信签名code"; const char *templateCode = "这里填短信模版code"; const char *mobile = "这里填写目标手机号"; //如模板有变量,根据自己模板数量按顺序填写,格式如下;如果模板无变量则无需传递 const char *param = "这里填写随机验证码"; char *response = sendMsg(accessKey, accessSecret, msgType, signCode, templateCode, mobile, param); if (response != NULL) { printf("%s\n", response); free(response); } return 0; }
C++
#include <windows.h> #include <wininet.h> #include <iostream> #include <string> #define URL "https://api.juncyun.com/api/msgService/sendSms" #define BUFFER_SIZE 4096 std::string sendMsg(const char *accessKey, const char *accessSecret, const char *msgType, const char *signCode, const char *templateCode, const char *mobile, const char *param) { HINTERNET hInternet = InternetOpen("HTTPPOST", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0); if (hInternet == NULL) { std::cerr << "Failed to initialize WinINet" << std::endl; return ""; } HINTERNET hConnect = InternetConnect(hInternet, "api.juncyun.com", INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0); if (hConnect == NULL) { std::cerr << "Failed to connect to server" << std::endl; InternetCloseHandle(hInternet); return ""; } HINTERNET hRequest = HttpOpenRequest(hConnect, "POST", "/api/msgService/sendSms", NULL, NULL, NULL, INTERNET_FLAG_SECURE, 0); if (hRequest == NULL) { std::cerr << "Failed to open HTTP request" << std::endl; InternetCloseHandle(hConnect); InternetCloseHandle(hInternet); return ""; } // 构建请求体数据 char postData[BUFFER_SIZE]; snprintf(postData, BUFFER_SIZE, "{\"accessKey\":\"%s\",\"accessSecret\":\"%s\",\"msgType\":\"%s\",\"signCode\":\"%s\",\"templateCode\":\"%s\",\"mobile\":\"%s\",\"params\":[\"%s\"]}", accessKey, accessSecret, msgType, signCode, templateCode, mobile, param); // 设置 HTTP 请求头 char headers[] = "content-Type: application/json;charset=UTF-8"; if (!HttpAddRequestHeaders(hRequest, headers, strlen(headers), HTTP_ADDREQ_FLAG_ADD)) { std::cerr << "Failed to set request headers" << std::endl; InternetCloseHandle(hRequest); InternetCloseHandle(hConnect); InternetCloseHandle(hInternet); return ""; } // 发送请求 if (!HttpSendRequest(hRequest, NULL, 0, postData, strlen(postData))) { std::cerr << "Failed to send request" << std::endl; InternetCloseHandle(hRequest); InternetCloseHandle(hConnect); InternetCloseHandle(hInternet); return ""; } // 读取服务器响应 char response[BUFFER_SIZE]; DWORD bytesRead; if (!InternetReadFile(hRequest, response, BUFFER_SIZE, &bytesRead)) { std::cerr << "Failed to read response" << std::endl; InternetCloseHandle(hRequest); InternetCloseHandle(hConnect); InternetCloseHandle(hInternet); return ""; } response[bytesRead] = '\0'; // 清理资源 InternetCloseHandle(hRequest); InternetCloseHandle(hConnect); InternetCloseHandle(hInternet); return std::string(response); } int main() { // 替换为你实际的访问密钥、短信类型、签名代码、模板代码和手机号码 const char *accessKey = "这里填accessKey"; const char *accessSecret = "这里填accessSecret"; const char *msgType = "1"; const char *signCode = "这里填短信签名code"; const char *templateCode = "这里填短信模版code"; const char *mobile = "这里填写目标手机号"; //如模板有变量,根据自己模板数量按顺序填写,格式如下;如果模板无变量则无需传递 const char *param = "这里填写随机验证码"; std::string response = sendMsg(accessKey, accessSecret, msgType, signCode, templateCode, mobile, param); if (!response.empty()) { std::cout << response << std::endl; } return 0; }
.NET
using System; using System.Net.Http; using System.Text; using System.Threading.Tasks; partial class Program { static readonly string URL = "https://api.juncyun.com/api/msgService/sendSms"; static async Task<string> SendMsgAsync(string accessKey, string accessSecret, string msgType, string signCode, string templateCode, string mobile, string param) { using (var httpClient = new HttpClient()) { // 构建请求体数据 var postData = $"{{\"accessKey\":\"{accessKey}\",\"accessSecret\":\"{accessSecret}\",\"msgType\":\"{msgType}\",\"signCode\":\"{signCode}\",\"templateCode\":\"{templateCode}\",\"mobile\":\"{mobile}\",\"params\":[\"{param}\"]}}"; // 创建 HttpRequestMessage 实例并设置请求头 var request = new HttpRequestMessage(HttpMethod.Post, URL); request.Content = new StringContent(postData, Encoding.UTF8, "application/json"); // 手动添加 charset=UTF-8 到请求头 request.Content.Headers.ContentType.CharSet = "UTF-8"; // 发送请求并获取响应 var response = await httpClient.SendAsync(request); // 处理响应 if (response.IsSuccessStatusCode) { return await response.Content.ReadAsStringAsync(); } else { Console.WriteLine($"Failed to send request. Status code: {response.StatusCode}"); return null; } } } static async Task Main(string[] args) { // 替换为你实际的访问密钥、短信类型、签名代码、模板代码和手机号码 string accessKey = "这里填accessKey"; string accessSecret = "这里填accessSecret"; string msgType = "这里填短信类型"; string signCode = "这里填短信签名code"; string templateCode = "这里填短信模版code"; string mobile = "这里填目标号码"; //如模板有变量,根据自己模板数量按顺序填写,格式如下;如果模板无变量则为空 string param = "这里填写参数"; var response = await SendMsgAsync(accessKey, accessSecret, msgType, signCode, templateCode, mobile, param); Console.WriteLine(response); } }
一对一服务,更有优惠惊喜
公众号
Copyright © 浙江卓见云科技有限公司 All Rights Reserved 浙ICP备16013515号-16