PHP对接 创蓝短信

//无需鉴权
const API_SEND_URL = 'http://smssh1.253.com/msg/send/json'; //创蓝发送短信接口URL
const API_ACCOUNT = '*****'; // 创蓝API账号
const API_PASSWORD = '*****';// 创蓝API密码
function sendPay($mobile, $name)
{
    $result = $this->sendSMS1($mobile, '短信模板');
    if (!$result) {
        $this->error('短信发送失败');
    }
    if (!is_null(json_decode($result))) {
        $output = json_decode($result, true);
        if (isset($output['code']) && $output['code'] == '0') {
            echo $result;
        } else {
            echo $result;
        }
    } else {
        echo $result;
    }
}
public function sendSMS1($mobile, $msg, $needstatus = 'true')
{
    //创蓝接口参数
    $postArr = array(
        'account' => self::API_ACCOUNT,
        'password' => self::API_PASSWORD,
        'msg' => urlencode($msg),
        'phone' => $mobile,
        'report' => $needstatus,
    );
    $result = $this->curlPost1(self::API_SEND_URL, $postArr);
    return $result;
}
// 通过CURL发送HTTP请求
private function curlPost1($url, $postFields)
{
    $postFields = json_encode($postFields);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json; charset=utf-8'   //json版本需要填写  Content-Type: application/json;
        )
    );
    curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); //若果报错 name lookup timed out 报错时添加这一行代码
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
    curl_setopt($ch, CURLOPT_TIMEOUT, 60);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $ret = curl_exec($ch);
    if (false == $ret) {
        $result = curl_error($ch);
    } else {
        $rsp = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if (200 != $rsp) {
            $result = "请求状态 " . $rsp . " " . curl_error($ch);
        } else {
            $result = $ret;
        }
    }
    curl_close($ch);
    return $result;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
对接极光短信可以使用Java SDK提供的API来实现。以下是一个简单的示例代码,演示了如何使用Java SDK发送短信: ```java import cn.jiguang.common.resp.APIConnectionException; import cn.jiguang.common.resp.APIRequestException; import cn.jiguang.common.resp.ResponseWrapper; import cn.jiguang.common.utils.Base64; import cn.jiguang.common.utils.StringUtils; import cn.jiguang.common.utils.Preconditions; import cn.jiguang.common.utils.RandomGenerator; import cn.jiguang.common.utils.TimeUtils; import cn.jiguang.common.utils.URIUtil; import cn.jiguang.common.utils.VersionUtils; import cn.jiguang.service.JSMSClient; import cn.jiguang.service.JSMSClientBuilder; import cn.jiguang.service.model.SMSConfig; import cn.jiguang.service.model.SMSResult; import cn.jiguang.service.model.SMSVerifyCodeParams; import cn.jiguang.service.model.SMSVerifyCodeResult; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.util.HashMap; import java.util.Map; public class JPushSMSExample { // 极光短信的AppKey和MasterSecret private static final String APP_KEY = "your_app_key"; private static final String MASTER_SECRET = "your_master_secret"; public static void main(String[] args) { // 创建JSMSClient实例 JSMSClient client = new JSMSClientBuilder() .setAppKey(APP_KEY) .setMasterSecret(MASTER_SECRET) .build(); // 设置短信配置 SMSConfig config = new SMSConfig(); config.setSignId(1L); // 签名ID config.setTempId(1L); // 模板ID // 设置短信参数 Map<String, String> params = new HashMap<>(); params.put("code", "123456"); // 模板中的参数 // 发送短信 try { SMSResult result = client.sendSMS("手机号码", config, params); System.out.println("发送短信成功,短信ID:" + result.getSmsId()); } catch (APIConnectionException e) { System.err.println("连接异常:" + e.getMessage()); } catch (APIRequestException e) { System.err.println("请求异常:" + e.getMessage()); } } } ``` 请注意,以上代码中的`your_app_key`和`your_master_secret`需要替换为你自己的极光短信的AppKey和MasterSecret。另外,还需要替换`手机号码`为实际的手机号码。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值