url:http://www.dengwz.com/wp-includes/js/tinymce/plugins/wptextpattern/ini_news_parser.class.php 亿美短信接口HTTP协议实现 – PHP研发工程师_小邓PHP博客
小邓博客,一位正在奋斗的PHP开发工程师的学习经历分享!为高效PHP开发而努力奋斗

亿美短信接口HTTP协议实现

PHP dengwz7788 982℃ 0评论

最近在做帮一个朋友做一个发短信的接口,也是我一直想做的一个接口,,亿美公司提供了几种方式,我还是一直很偏向HTTP协议的

因为这种方式不限制语言,而且轻量级。然后刚好这几天也在学习代码重构技术。

根据重构的原则编写了这个发短信的类

这里有说几个坑:

  1.  调用发短信接口后,短信发送成功,但手机收不到短信? 发送短信的格式有要求,必须有是公司简称或者产品简称  类似:【支付宝】【京东】这些的 信息,不然短信没办法发出去。
  2. 签名移到最前端  联通移动什么都不会限制的
  3. 短信内容必须是UTF-8编码的,中文还需要经过urlencode()编码。代码中有实现。

[php]

/***
* 北京亿美短信发送通用接口
* @version 1.0
* @author Summer<dengwz7788@gmail.com>
* @保留版权信息,不限制使用范围
* **/

class SendM{

private $cdkey = "0SDK-EMY-6688-KHZOS"; // "改成您的密钥"
private $password = "123456"; //"改成您的密码"
private $SendMessageApi = "http://sdk4report.eucp.b2m.cn:8080/sdkproxy/sendsms.action";
private $PlanSendMessageApi = "http://sdk4report.eucp.b2m.cn:8080/sdkproxy/sendtimesms.action";
private $QueryMessageNumApi = "http://sdk4report.eucp.b2m.cn:8080/sdkproxy/querybalance.action";
private $getreportApi = "http://sdk4report.eucp.b2m.cn:8080/sdkproxy/getreport.action";
private $registApi = "http://sdk4report.eucp.b2m.cn:8080/sdkproxy/regist.action";
private $registdetailinfoApi= "http://sdk4report.eucp.b2m.cn:8080/sdkproxy/registdetailinfo.action";
private $querybalanceApi = "http://sdk4report.eucp.b2m.cn:8080/sdkproxy/querybalance.action";
private $getmoApi = "http://sdk4report.eucp.b2m.cn:8080/sdkproxy/getmo.action";

public function __construct() {

$this->param["cdkey"] = $this->cdkey;
$this->param[‘password’] = $this->password;

}

/**获取剩余短信余量**/
public function querybalance()
{
$Arr= $this->Curl($this->querybalanceApi, $this->param);
$this->json($Arr);
}

/**获取上行短信接口**/
public function getmo()
{
$Arr= $this->Curl($this->getmoApi, $this->param);
$this->json($Arr);
}

/**获取短信数据报表**/
public function getreport()
{
$Arr= $this->Curl($this->getreportApi, $this->param);
$this->json($Arr);
}

/**公司信息注册**/
public function registdetailinfo()
{
$this->param["ename"] = "小邓PHP-高效PHP开发"; //企业名称(最多60字节),必须输入
$this->param["linkman"] = "小邓"; //联系人姓名(最多20字节),必须输入
$this->param["phonenum"] = ""; //联系电话(最多20字节),必须输入
$this->param["mobile"] = ""; //联系手机(最多15字节),必须输入
$this->param["email"] = ""; //电子邮件(最多60字节),必须输入
$this->param["fax"] = ""; //联系传真(最多20字节),必须输入
$this->param["address"] = ""; //公司地址(最多60字节),必须输入
$this->param["postcode"] = ""; //邮政编码(最多6字节),必须输入

$Arr= $this->Curl($this->registdetailinfoApi, $this->param);
$this->json($Arr);
}

/**查询发送短信数**/
public function querymessagenum()
{
$Arr= $this->Curl($this->QueryMessageNumApi, $this->param);
$this->json($Arr);
}

/**公司注册**/
private function regist()
{
$Arr= $this->Curl($this->registApi, $this->param);
$this->json($Arr);
}

/**发送短信接口,单例发送**/
public function Sendsms($Content,$phone,$addserial="")
{
if(is_array($phone)){
$phone = implode(",", $phone);
}
$this->param["phone"] = $phone;
$this->param["message"] = $Content;
$this->param[‘seqid’] = $this->seqid;

$this->param["addserial"] = $addserial;
$Arr= $this->Curl($this->SendMessageApi, $this->param);
$this->json($Arr);
}

private function Curl($url,$param)
{
$ch2 = curl_init();
//获取个人信息
$url2 = $url."?".$this->_param($param);

curl_setopt($ch2, CURLOPT_URL, $url2);
curl_setopt($ch2, CURLOPT_HEADER, 0); //不返回header部分
curl_setopt($ch2, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
curl_setopt($ch2, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch2, CURLOPT_FOLLOWLOCATION,1);
$information = curl_exec($ch2);
curl_close($ch2);

return $this->XML_toArr($information);
}

private function XML_toArr($XML)
{
$p = xml_parser_create();
xml_parse_into_struct($p, trim($XML), $vals, $index);
xml_parser_free($p);

foreach($vals as $key=>$value)
{
$result[strtolower($value[‘tag’])] = $value[‘value’];
}

return $result;
}

private function _param($param)
{
foreach($param as $key=>$value)
{
$tmp[] = $key."=".urlencode($this->encodeToUTF8($value));
}

return implode("&", $tmp);
}

private function json($data)
{
echo json_encode($data);
exit;
}

private static function encodeToUTF8($value)
{

$encode = mb_detect_encoding($value, array("ASCII","UTF-8","GB2312","GBK","BIG5"));

if($encode == "UTF-8"){
return $value;
}

return mb_convert_encoding($value, "UTF-8", $encode);

}

}
[/php]

Thinkphp导入类的使用模版

[php]

import("@.ORG.SendM");

$SendM = NEW SendM();

$SendM->Sendsms("【PHP开发工程师-小邓高新能PHP开发】给你发了短信了", "1397335xxxx");
[/php]

转载请注明:PHP研发工程师_小邓PHP博客 » 亿美短信接口HTTP协议实现

喜欢 (7)or分享 (0)
发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址