PHP发送短信代码分享


Posted in PHP onAugust 11, 2015

方法一(比较好,推荐)

//PHP发送短信 Monxin专用(PHP代码函数)
//本代码基于Monxin 运行
//代码来源:Monxin ./config/functions.php
 
function sms($config,$language,$pdo,$sender,$phone_number,$content){
   
  //demo var_dump(sms(self::$config,self::$language,$pdo,"system","18074507509,15507455992","测试内容,时间".date("H:i:s",time())));
  $sender=safe_str($sender);
  $content=safe_str($content);
  $arr=explode(',',$config['sms']['disable_phrase']);
  $disable=false;
  foreach($arr as $v){
    if(strpos($content,$v)!==false){$phrase=$v;$disable=true;continue;}
  }
  if($disable){return $language['exist_disable_phrase']." ".$phrase;}
   
  $phone_number=explode(',',$phone_number);
  $phone_number=array_unique($phone_number);
  $addressee='';
  $count=0;
  foreach($phone_number as $v){
    if(preg_match($config['other']['reg_phone'],$v)){$addressee.=$v.',';}
  }
  $addressee=trim($addressee,',');
  $addressee=explode(",",$addressee);
  //var_dump($addressee);
  $section=ceil(count($addressee)/$config['sms']['max']);
   
  for($i=0;$i<$section;$i++){
    $phone[$i]='';
    for($j=$i*$config['sms']['max'];$j<($i+1)*$config['sms']['max'];$j++){
      //echo $j.',';
      if(isset($addressee[$j])){$phone[$i].=$addressee[$j].$config['sms']['delimiter'];}
    }
    $phone[$i]=trim($phone[$i],$config['sms']['delimiter']);
    $temp=explode($config['sms']['delimiter'],$phone[$i]);
    $count=count($temp);
    $length=ceil(strlen(preg_replace('/[\x80-\xff]{3}/','x',$content))/($config['sms']['length']/2));
    $count=$length*$count;
    if(!isset($timing)){$timing=0;}
    if($phone[$i]!=''){
      $time=time();
      $sql="insert into ".$pdo->index_pre."phone_msg (`sender`,`addressee`,`content`,`state`,`time`,`count`,`timing`) values ('$sender','".$phone[$i]."','".$content."','1','$time','$count','0')";  
      if($pdo->exec($sql)){
        return send_sms($config,$pdo,$pdo->lastInsertId());
      }else{
        return false;
      }
    }
  }
 
}

例2:在PHP5中通过file_get_contents函数发送短信(HTTP GET 方式)

PHP代码

<?php   
$url = "http://sms.api.bz/fetion.php?username=13812345678&password=123456&sendto=13512345678&message=短信内容";   
$result = file_get_contents($url);   
echo $result; //返回信息默认为UTF-8编码的汉字,如果你的页面编码为gb2312,请使用下行语句输出返回信息。   
//echo iconv("UTF-8", "GBK", $result);   
?>

例3:在PHP中通过curl发送短信(HTTP POST 方式)

PHP代码

<?php   
$data["username"] = 13812345678;   
$data["password"] = "password123";   
$data["sendto"] = 13512345678;   
$data["message"] = "这是一条测试短信!";   
   
$curl = new Curl_Class();   
$result = @$curl->post("http://sms.api.bz/fetion.php", $data);   
echo $result; //返回信息默认为UTF-8编码的汉字,如果你的页面编码为gb2312,请使用下行语句输出返回信息。   
//echo iconv("UTF-8", "GBK", $result);   
   
//curl类   
class Curl_Class   
{   
function Curl_Class()   
{   
return true;   
}   
   
function execute($method, $url, $fields = '', $userAgent = '', $httpHeaders = '', $username = '', $password = '')   
{   
$ch = Curl_Class::create();   
if (false === $ch)   
{   
return false;   
}   
   
if (is_string($url) && strlen($url))   
{   
$ret = curl_setopt($ch, CURLOPT_URL, $url);   
}   
else   
{   
return false;   
}   
//是否显示头部信息   
curl_setopt($ch, CURLOPT_HEADER, false);   
//   
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   
   
if ($username != '')   
{   
curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);   
}   
   
$method = strtolower($method);   
if ('post' == $method)   
{   
curl_setopt($ch, CURLOPT_POST, true);   
if (is_array($fields))   
{   
$sets = array();   
foreach ($fields AS $key => $val)   
{   
$sets[] = $key . '=' . urlencode($val);   
}   
$fields = implode('&',$sets);   
}   
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);   
}   
else if ('put' == $method)   
{   
curl_setopt($ch, CURLOPT_PUT, true);   
}   
   
//curl_setopt($ch, CURLOPT_PROGRESS, true);   
//curl_setopt($ch, CURLOPT_VERBOSE, true);   
//curl_setopt($ch, CURLOPT_MUTE, false);   
curl_setopt($ch, CURLOPT_TIMEOUT, 10);//设置curl超时秒数   
   
if (strlen($userAgent))   
{   
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);   
}   
   
if (is_array($httpHeaders))   
{   
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders);   
}   
   
$ret = curl_exec($ch);   
   
if (curl_errno($ch))   
{   
curl_close($ch);   
return array(curl_error($ch), curl_errno($ch));   
}   
else   
{   
curl_close($ch);   
if (!is_string($ret) || !strlen($ret))   
{   
return false;   
}   
return $ret;   
}   
}   
   
function post($url, $fields, $userAgent = '', $httpHeaders = '', $username = '', $password = '')   
{   
$ret = Curl_Class::execute('POST', $url, $fields, $userAgent, $httpHeaders, $username, $password);   
if (false === $ret)   
{   
return false;   
}   
   
if (is_array($ret))   
{   
return false;   
}   
return $ret;   
}   
   
function get($url, $userAgent = '', $httpHeaders = '', $username = '', $password = '')   
{   
$ret = Curl_Class::execute('GET', $url, '', $userAgent, $httpHeaders, $username, $password);   
if (false === $ret)   
{   
return false;   
}   
   
if (is_array($ret))   
{   
return false;   
}   
return $ret;   
}   
   
function create()   
{   
$ch = null;   
if (!function_exists('curl_init'))   
{   
return false;   
}   
$ch = curl_init();   
if (!is_resource($ch))   
{   
return false;   
}   
return $ch;   
}   
   
}
PHP 相关文章推荐
PHP开发需要注意的安全问题
Sep 01 PHP
PHP学习笔记之二 php入门知识
Jan 12 PHP
PHP.ini中配置屏蔽错误信息显示和保存错误日志的例子
May 12 PHP
php 判断网页是否是utf8编码的方法
Jun 06 PHP
php使用session二维数组实例
Nov 06 PHP
thinkphp配置连接数据库技巧
Dec 02 PHP
smarty模板引擎之分配数据类型
Mar 30 PHP
PHP简单操作MongoDB的方法(安装及增删改查)
May 26 PHP
php实现用户注册密码的crypt加密
Jun 08 PHP
PHP 实现从数据库导出到.csv文件方法
Jul 06 PHP
phpstorm 正则匹配删除空行、注释行(替换注释行为空行)
Jan 21 PHP
PHP中ltrim()函数的用法与实例讲解
Mar 28 PHP
php实现可逆加密的方法
Aug 11 #PHP
PHP实现获取某个月份周次信息的方法
Aug 11 #PHP
PHP实现删除字符串中任何字符的函数
Aug 11 #PHP
详解php的socket通信
Aug 11 #PHP
Java中final关键字详解
Aug 10 #PHP
php生成二维码
Aug 10 #PHP
PHP的文件操作与算法实现的面试题示例
Aug 10 #PHP
You might like
常用的php图片处理类(水印、等比缩放、固定高宽)分享
2015/06/19 PHP
php获取网站百度快照日期的方法
2015/07/29 PHP
PHP操作mysql数据库分表的方法
2016/06/09 PHP
Yii核心验证器api详解
2016/11/23 PHP
如何离线执行php任务
2017/02/21 PHP
PHP ADODB生成HTML表格函数rs2html功能【附错误处理函数用法】
2018/05/29 PHP
utf8的编码算法 转载
2006/12/27 Javascript
Jquery对数组的操作技巧整理
2014/03/25 Javascript
dedecms页面如何获取会员状态的实例代码
2016/03/15 Javascript
jQuery实现按钮点击遮罩加载及处理完后恢复的效果
2016/06/07 Javascript
原生js仿jquery实现对Ajax的封装
2016/10/04 Javascript
js+css3制作时钟特效
2016/10/16 Javascript
jQuery ajax读取本地json文件的实例
2017/10/31 jQuery
基于Vue实现拖拽功能
2020/07/29 Javascript
JS中使用new Option()实现时间联动效果
2018/12/10 Javascript
vue-router路由模式详解(小结)
2019/08/26 Javascript
vue项目中在可编辑div光标位置插入内容的实现代码
2020/01/07 Javascript
Python socket.error: [Errno 98] Address already in use的原因和解决方法
2014/08/25 Python
python中实现将多个print输出合成一个数组
2018/04/19 Python
Python面向对象类继承和组合实例分析
2018/05/28 Python
Python可变参数会自动填充前面的默认同名参数实例
2019/11/18 Python
h5封装下拉刷新
2020/08/25 HTML / CSS
英国领先的亚洲旅游专家:Wendy Wu Tours
2018/01/21 全球购物
阿玛尼美妆俄罗斯官网:Giorgio Armani Beauty RU
2020/07/19 全球购物
澳大利亚著名的纺织品品牌:Canningvale
2020/05/05 全球购物
室内设计实习自我鉴定
2013/09/25 职场文书
护理专业毕业生自我鉴定
2013/10/08 职场文书
优秀员工评语
2014/02/10 职场文书
校园安全演讲稿
2014/05/09 职场文书
学校标语大全
2014/06/19 职场文书
创先争优标语
2014/06/27 职场文书
计算机网络专业自荐信
2014/07/04 职场文书
公司向个人借款协议书范本
2014/10/09 职场文书
县委党的群众路线教育实践活动工作情况报告
2014/10/25 职场文书
本科毕业论文致谢怎么写
2015/05/14 职场文书
Pygame Rect区域位置的使用(图文)
2021/11/17 Python