php网云发短信

<?php

namespace lib;

/**
* Description of Qrcode
* 网云发送短信
* @author lsf
*/
class Wangyuncms {

/**
* 多个发送短信
* @param type $mobile
* @param type $setting
* @param type $content
*/
public function groupMessage($mobile, $setting, $content) {
header("Content-Type: text/html;charset=utf-8");
$url = 'http://api.smschn.cn';
$post = 'user=' . URLEncode($setting["wangyun_user"]) . '&key=' . $setting["wangyun_key"] . '&mobile=' . $mobile . '&content=' . $content;
$this->curl_request($url, $post);
}

/**
* 网云发送短信
*/
public function sendMessage($mobile, $setting, $content) {
header("Content-Type: text/html;charset=utf-8");
$url = 'http://api.smschn.cn';
$post = 'user=' . URLEncode($setting["wangyun_user"]) . '&key=' . $setting["wangyun_key"] . '&mobile=' . $mobile . '&content=' . $content;
$res = $this->curl_request($url, $post);
if (!$res["success"]) {
return array("success" => false, "msg" => $res["msg"]);
} else {
if ($res["data"] >= 0) {
return array("success" => true, "msg" => "");
} else {
return array("success" => false, "msg" => $this->errorCode($res["data"]));
}
}
}

/**
* 请求
* @param type $url
* @param type $post
* @return type
*/
function curl_request($url, $post = '') {
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //有请求的返回值
curl_setopt($curl, CURLOPT_AUTOREFERER, 1); //重定向时
curl_setopt($curl, CURLOPT_NOSIGNAL, 1); //以毫秒为超时计时单位一定要设置这个
curl_setopt($curl, CURLOPT_TIMEOUT_MS, 10000); //2秒超时PHP 5.2.3起可使用
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
$data = curl_exec($curl);
if (curl_errno($curl)) {
return array("success" => false, "msg" => curl_error($curl));
}
curl_close($curl);
return array("success" => true, "msg" => "", "data" => $data);
}

/**
* 错误代码
* @param type $code
* @return string
*/
public function errorCode($code) {
$data = array("-11" => "用户名不匹配",
"-12" => "接口鉴权KEY不匹配,非登录密码",
"-13" => "账户余额不足",
"-14" => "账户被屏蔽",
"-21" => "手机号码错误",
"-22" => "手机号码超过限制",
"-23" => "短信内容为空",
"-24" => "短信内容出现非法字符",
"-25" => "短信签名为空",
"-26" => "短信签名超长",
"-30" => "接口IP被限制访问");
try {
return$data[$code];
} catch (\Exception $exc) {
return "短信发送失败";
}
}

}

猜你喜欢

转载自www.cnblogs.com/-lsf/p/13198504.html