美文网首页
PHP发送短信

PHP发送短信

作者: ONEDAYLOG | 来源:发表于2020-05-12 08:31 被阅读0次

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'  => ''
    ),

原来的站点停运,整合到简书
2016年8月16日

相关文章

  • PHP发送短信

    1、首先本人使用的是卡洛思短信平台,用了2年了,感觉很好600元10000条。2、官网网址:http://www....

  • PhpSms

    PhpSms 可能是目前最聪明、优雅的php短信发送库了。从此不再为各种原因造成的个别短信发送失败而烦忧! php...

  • PHP封装发送短信类!

    调用示例:

  • php发送短信验证码

    业务: 手机端点击发送验证码,请求发送到php端,由php调用第三方平台(我们使用的是榛子云短信http://sm...

  • 阿里云短信发送【PHP版】

    最近公司切换阿里云短信服务,只能用新的sdk,官方的api接口说明太简单了,sdk太乱了 官方提供的属实太乱太冗余...

  • 阿里短信API接入

    阿里短信API接入 1.下载最新php SDK工具包 2.配置短息参数 3. 访问该文件,即可发送短信

  • iOS,在程序中发送短信和打电话

    1,发送短信发送短信有两种方式:一,在程序外发送短信: 二,在程序内发送短信首先导入头文件,#import

  • 发送短信

  • 发送短信

    @property (nonatomic,weak) MFMessageComposeViewController...

  • 发送短信

    /发送短信/

网友评论

      本文标题:PHP发送短信

      本文链接:https://www.haomeiwen.com/subject/hgsvnhtx.html