PHP DEMO
文字短信发送
<?php
header("Content-type: text/html; charset=utf-8");
/*
文字短信发送demo
视频短信、弹屏闪信、语音短信、国际短信模板发送与文字短信类似
*/
$action='sendtemplate';
$url = 'http://www.lokapi.cn/smsUTF8.aspx';
$username = '******';
$password =strtoupper(md5('*****'));
$token = '*******';
$templateid = '******';
$param = '17712345678|张三|1230';
$timestamp = getMillisecond();
$sign = strtoupper(md5('action='.$action.'&username='.$username.'&password='.$password.'&token='.$token.'×tamp='.$timestamp));
$postData = array
(
'action'=>$action,
'username'=>$username,
'password'=>$password,
'token'=>$token,
'timestamp'=>$timestamp,
'sign'=>$sign,
'rece'=>'json',
'templateid'=>$templateid,
'param'=>$param
);
$result= postSMS($url,$postData);
echo $result;
function postSMS($url,$postData)
{
$row = parse_url($url);
$host = $row['host'];
$port = isset($row['port']) ? $row['port']:80;
$file = $row['path'];
$post = "";
while (list($k,$v) = each($postData))
{
$post .= rawurlencode($k)."=".rawurlencode($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);
}
}
function getMillisecond() {
list($t1, $t2) = explode(' ', microtime());
return (float)sprintf('%.0f',(floatval($t1)+floatval($t2))*1000);
}
?>
彩信发送
<?php
header("Content-type: text/html; charset=utf-8");
/* 彩信发送demo */
$action = 'sendimagetext';
$url = 'http://www.lokapi.cn/smsUTF8.aspx';
$username = '******';
$password = strtoupper(md5('******'));
$token = '******';
$timestamp = getMillisecond();
$sign = strtoupper(md5('action=' . $action . '&username=' . $username . '&password=' . $password . '&token=' . $token . '×tamp=' . $timestamp));
$title = '祝福短信';
$mobile = '17712345678';
/*构造发送主体 */
$content = '祝你生日快乐';
$txt = base64_encode(get_utf8_to_gb($content));
$path = "D:/我的文档/Pictures/6b76a7cb.jpg";
$extension = "jpg"; //图片后缀
$img = imgtobase64($path);
$message = 'txt|' . $txt . ',' . $extension . '|' . $img . ';'; //如果只发送一张图片最后这个;一定不要丢掉
$message = str_replace('%', '%25', $message);
$message = str_replace('&', '%26', $message);
$message = str_replace('+', '%2B', $message);
/*构造发送参数 */
$postData = array(
'action' => $action,
'username' => $username,
'password' => $password,
'token' => $token,
'timestamp' => $timestamp,
'sign' => $sign,
'rece' => 'json',
'title' => $title,
'mobile' => $mobile,
'message' => $message
);
$result = postSMS($url, $postData);
echo $result;
function imgtobase64($img = '', $imgHtmlCode = true)
{
$imageInfo = getimagesize($img);
$base64 = "" . chunk_split(base64_encode(file_get_contents($img)));
return chunk_split(base64_encode(file_get_contents($img)));;
}
function get_utf8_to_gb($value)
{
$value_1 = $value;
$value_2 = @iconv("utf-8", "gb2312//IGNORE", $value_1); //使用@抵制错误,如果转换字符串中,某一个字符在目标字符集里没有对应字符,那么,这个字符之后的部分就被忽略掉了;即结果字符串内容不完整,此时要使用//IGNORE
$value_3 = @iconv("gb2312", "utf-8//IGNORE", $value_2);
if (strlen($value_1) == strlen($value_3)) {
return $value_2;
} else {
return $value_1;
}
}
function postSMS($url, $postData)
{
$row = parse_url($url);
$host = $row['host'];
$port = isset($row['port']) ? $row['port'] : 80;
$file = $row['path'];
$post = "";
while (list($k, $v) = each($postData)) {
$post .= rawurlencode($k) . "=" . rawurlencode($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);
}
}
function getMillisecond()
{
list($t1, $t2) = explode(' ', microtime());
return (float) sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);
}
余额查询
<?php
header("Content-type: text/html; charset=utf-8");
/* 余额查询demo */
$action = 'overage';
$url = 'http://www.lokapi.cn/smsUTF8.aspx';
$username = '*****';
$password = strtoupper(md5('******'));
$token = '******';
$timestamp = getMillisecond();
$sign = strtoupper(md5('action=' . $action . '&username=' . $username . '&password=' . $password . '&token=' . $token . '×tamp=' . $timestamp));
$postData = array(
'action' => $action,
'username' => $username,
'password' => $password,
'token' => $token,
'timestamp' => $timestamp,
'sign' => $sign,
'rece' => 'json'
);
$result = postSMS($url, $postData);
echo $result;
function postSMS($url, $postData)
{
$row = parse_url($url);
$host = $row['host'];
$port = isset($row['port']) ? $row['port'] : 80;
$file = $row['path'];
$post = "";
while (list($k, $v) = each($postData)) {
$post .= rawurlencode($k) . "=" . rawurlencode($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);
}
}
function getMillisecond()
{
list($t1, $t2) = explode(' ', microtime());
return (float) sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);
}
添加模板
<?php
header("Content-type: text/html; charset=utf-8");
/* 添加模板demo */
$url = 'http://www.lokapi.cn/Template.aspx';
$type = 'sms';
$username = '******';
$password = strtoupper(md5('****'));
$timestamp = getMillisecond();
$sign = strtoupper(md5('username=' . $username . '&password=' . $password . '×tamp=' . $timestamp));
$postData = array(
'username' => $username,
'password' => $password,
'timestamp' => $timestamp,
'sign' => $sign,
'rece' => 'json',
'message' => '【乐讯通】您的验证码是:{s6}',
'type' => $type,
'free' => '0'
);
$result = postSMS($url, $postData);
echo $result;
function postSMS($url, $postData)
{
$row = parse_url($url);
$host = $row['host'];
$port = isset($row['port']) ? $row['port'] : 80;
$file = $row['path'];
$post = "";
while (list($k, $v) = each($postData)) {
$post .= rawurlencode($k) . "=" . rawurlencode($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);
}
}
function getMillisecond()
{
list($t1, $t2) = explode(' ', microtime());
return (float) sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);
}