Sms.class.php
3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
require_once dirname(__FILE__).'/sms/config/config.php';
/**
*
* @author Administrator
*
*/
class Util_Sms
{
/**
*
* @var string
*/
const MTARGET = SMS_SERVER_ADDR;
/**
* 发送文本信息
*
* @param string $phoneNumber
* @param string $content
* @return boolean
*/
public static function sendTextMessage($phoneNumber, $content)
{
if(empty($phoneNumber) || empty($content))
{
return false;
}
$postData = array(
//提交账户
'sname' => SMS_USER_NAME,
//提交账户密码
'spwd' => SMS_USER_PASSWORD,
//企业代码
'scorpid'=>'',
//产品编号
'sprdid'=> SMS_PRODUCT_ID2,
//接收号码,每次只能提交1个手机号码
'sdst'=> $phoneNumber,
//信息内容,通常为70汉字以内,超过70字,请先测试
'smsg'=> $content,
);
$result = self::post(http_build_query($postData), self::MTARGET);
if(!empty($result))
{
$simpleXML = @simplexml_load_string($result);
if(is_object($simpleXML))
{
/*
* 0:成功
* -1:系统异常
* 6001:无效的帐号或密码
* 6012:当前的IP禁止查询
* 6021:无效的企业介入号
* 6024:帐号处于冻结状态
*/
$state = $simpleXML->State;
if($state == 0)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
else
{
return false;
}
}
/**
* 发送数据
*
* @param string $data
* @param string $target
* @return string
*/
private static function post($data, $target)
{
$url_info = parse_url($target);
$httpheader = "POST " . $url_info['path'] . " HTTP/1.0\r\n";
$httpheader .= "Host:" . $url_info['host'] . "\r\n";
$httpheader .= "Content-Type:application/x-www-form-urlencoded\r\n";
$httpheader .= "Content-Length:" . strlen($data) . "\r\n";
$httpheader .= "Connection:close\r\n\r\n";
//$httpheader .= "Connection:Keep-Alive\r\n\r\n";
$httpheader .= $data;
$fd = fsockopen($url_info['host'], 80);
fwrite($fd, $httpheader);
$gets = "";
while(!@feof($fd))
{
$gets .= @fread($fd, 128);
}
fclose($fd);
preg_match("/Content-Length:.?(\d+)/", $gets, $matches);
$length = $matches[1];
$gets = substr($gets, - $length);
return $gets;
}
}
?>