PHP 做群发短信(短信接口连接问题)

来源:互联网 发布:2015淘宝新店扶持 编辑:程序博客网 时间:2024/06/05 18:55

首先写个方法:

function sendSMS($http,$username,$password,$sendmobile,$send_content){
   $httpurl = $http;
   $data = array
    (
    'USERNAME'=>$username,     //用户账号
    'PASSWORD'=>$password, //用户密码
    'MOBILE'=>$sendmobile,    //号码
    'CONTENT'=> mb_convert_encoding($send_content,'GB2312','UTF-8'), //内容
    );
   $re= $this->postSMS($httpurl,$data);   //POST方式提交
   if(trim($re)>0)
   {
    return "success";
   }
   else
   {
    return "error";
   }
}
 function postSMS($httpurl,$data=''){
 $row = parse_url($httpurl);
 $host = $row['host'];
 $port = isset($row['port']) ? $row['port']:80;
 $file = $row['path'];
 $post = '';
 while (list($k,$v) = each($data))
 {
  $post .= ($k)."=".($v)."&"; 
 }
 $post = substr($post,0 ,-1);
 $len = strlen($post);
 
 $fp = @fsockopen( $host ,$port, $errno, $errstr, 10);
 if (!$fp) {
  return "$errstr ($errno)\n";
  }else {
  $receive = '';
  $out = "POST $file HTTP/1.1\r\n";
  $out .= "Host: $host\r\n";
  $out .= "Content-type: application/x-www-form-urlencoded\r\n";
  $out .= "Connection: Close\r\n";
  $out .= "Content-Length: $len\r\n\r\n";
  $out .= $post; 
  fwrite($fp, $out);
  while (!feof($fp)) {
   $receive .= fgets($fp,128);
  }
  fclose($fp);
  $receive = explode("\r\n\r\n",$receive);
  unset($receive[0]);
  return implode("",$receive);
 }
}

 

 

 

 

PHP处理页面

 

$sendmobile = isset($_POST['mobile']) ? trim($_POST['mobile']) : '';
 $hm_arr = explode(',', $sendmobile);
 $sendtime = isset($time) ? strtotime($time) : $systime;
 $sendcontent = isset($_POST['contentname']) ? trim($_POST['contentname']) : '';
 $sendstatus = 1;
 $sendsign = isset($_POST['sendsign']) ? trim($_POST['sendsign']) : '';
 $send_content = $sendcontent.'  --'.$sendsign;
 $httpurl = $sms_config['httpurl'];
 $username = $sms_config['username'];
 $password = $sms_config['password'];
 $balance_info = $balance->GetBlanceInfo($uid);
 $balance_sy = $balance_info['balance_info']-$balance_info['rate'];
 $rate = $balance_info['rate'];
 if($balance_sy > $rate){
 $hp = $balance->sendSMS($httpurl,$username,$password,$sendmobile,preg_replace('/\s+/','%20',$send_content));
 if($hp == 'success'){
    $sql_c = "update {$tablepre}sms_info set balance_info = '$balance_sy',amount = amount+'1' where userid = '$uid'";
    $rs = $db->query($sql_c);
    $sql_s = "update {$tablepre}company set corpname = '$sendsign' where userid = '$uid'";
    $rs_s = $db->query($sql_s);
    $sql = "insert into {$tablepre}sms_log (sendmobile,userid,sendtime,sendcontent,sendtype,sendstatus,sendmoney) values ('$sendmobile','$uid','$sendtime','$sendcontent','$sendtype','$sendstatus','$rate')";
    $rs = $db->query($sql);
    header("location:index.php?c=sigle_send_mess&s=1");
    }else{
    header("location:index.php?c=sigle_send_mess&s=2");
 }
   }else{
     header("location:index.php?c=sigle_send_mess&s=3");
    }