PHP发送短信

羽化
2016-05-24 / 0 评论 / 394 阅读 / 正在检测是否收录...

1、首先本人使用的是卡洛思短信平台,用了2年了,感觉很好600元10000条。
2、官网网址:http://www.karlos.com.cn/
3、卡洛思短信发送API文档链接: http://pan.baidu.com/s/1miCju8g 密码: vpeh
4、本人使用的是ThinkPHP,先在\ThinkPHP\Common\function.php中加入get和post的方法

/**
 * get method
 */
function get($url, $param = array())
{
    if (!is_array($param)) {
        throw new Exception("参数必须为array");
    }
    $p = '';
    foreach ($param as $key => $value) {
        $p = $p . $key . '=' . $value . '&';
    }
    if (preg_match('/\?[\d\D]+/', $url)) { //matched ?c
        $p = '&' . $p;
    } else if (preg_match('/\?$/', $url)) { //matched ?$
        $p = $p;
    } else {
        $p = '?' . $p;
    }
    $p = preg_replace('/&$/', '', $p);
    $url = $url . $p;
    //echo $url;
    $httph = curl_init($url);
    curl_setopt($httph, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($httph, CURLOPT_SSL_VERIFYHOST, 1);
    curl_setopt($httph, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($httph, CURLOPT_USERAGENT, 
    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
 
    curl_setopt($httph, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($httph, CURLOPT_HEADER, 1);
    $rst = curl_exec($httph);
    curl_close($httph);
    return $rst;
}
 
/**
 * post method
 */
function post($url, $param = array())
{
    if (!is_array($param)) {
        throw new Exception("参数必须为array");
    }
    $httph = curl_init($url);
    curl_setopt($httph, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($httph, CURLOPT_SSL_VERIFYHOST, 1);
    curl_setopt($httph, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($httph, CURLOPT_USERAGENT, 
    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
    curl_setopt($httph, CURLOPT_POST, 1); //设置为POST方式
    curl_setopt($httph, CURLOPT_POSTFIELDS, $param);
    curl_setopt($httph, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($httph, CURLOPT_HEADER, 1);
    $rst = curl_exec($httph);
    curl_close($httph);
    return $rst;
};

5、发送短信的代码

/**
 * 发送短信
 * @param $mobile       手机号码
 * @param $content      发送内容
 * @return mixed
 */
function sendSms($mobile, $content)
{
    $obj =  array();
 
    if(!$_SESSION['SMS']){
        $_SESSION['SMS']['Count'] = 0;
    }else{
        $_SESSION['SMS']['Count'] = $_SESSION['SMS']['Count'] + 1;
    }
 
    if(time()>$_SESSION['SMS']['SendTime']){
        if( $_SESSION['SMS']['Count']<5){
            $_SESSION['SMS']['SendTime'] = time();
            $config = C('THINK_SMS');
            $option = array(
                'userid' => $config['USER_ID'],
                'account' => $config['ACCOUNT'],
                'password' => $config['PASSWORD'],
                'mobile' => $mobile,
                'content' => $content,
                'sendTime' => $config['SEND_TIME'],
                'action' => $config['ACTION'],
                'extno' => $config['EXTNO']
            );
            $result =  post('http://www.smsok.cn/sms.aspx', $option);
            $obj = array(
                'success'   => true,
                'info'      => $result
            );
 
        }else{
            $_SESSION['SMS']['SendTime'] = time()+1800;
            $_SESSION['SMS']['Count'] = 0;
            $obj = array(
                'success'   => false,
                'info'      => "短信验证发送过去频繁!"
            );
 
        }
    }else{
        $minTime = ($_SESSION['SMS']['SendTime'] - time());
        $obj = array(
            'success'   => false,
            'info'      => "等待".$minTime."秒后才可以发送!"
        );
 
    }
 
    return $obj;
};

6、ThinkPHP中Config中配置发送短信的账号密码

    //短信配置
    'THINK_SMS' => array(
        'USER_ID'   => '****',
        'ACCOUNT'   => '****',
        'PASSWORD'   => '****',
        'SEND_TIME'   => '',
        'ACTION' => 'send',
        'EXTNO'  => ''
    ),
0

评论 (0)

取消