PHP

国际短信

<?php
header("Content-type:text/html; charset=UTF-8");

/* *
* 类名:YunxinSmsApi
* 功能:云信短信发送接口请求类
* 详细:构造云信短信接口请求,获取远程HTTP数据
* 版本:1.3
* 日期:2016-07-16
* 说明:
* 以下代码只是为了方便客户测试而提供的样例代码,客户可以根据自己网站的需要,按照技术文档自行编写,并非一定要使用该代码。
* 该代码仅供学习和研究云信接口使用,只是提供一个参考。
*/

class YunxinSmsApi {

//云信发送短信接口URL, 如无必要,该参数可不用修改

const API_SEND_URL='http://sms.smsyun.cc:9012/servlet/UserServiceAPI';

const API_ACCOUNT='xxxxx';//云信账号 替换成你自己的账号

const API_PASSWORD='xxxxx';//云信密码 替换成你自己的密码

/**
* 发送短信 *
* @param string $mobile 手机号码
* @param string $msg 短信内容
* @param string $needstatus 是否需要状态报告
*/

public function sendSMS( $mobile, $msg, $needstatus = 0) {

//云信接口参数
$postArr = array (
'method' => 'sendSMS',
'username' => self::API_ACCOUNT,
'password' => self::API_PASSWORD,
'mobile' => $mobile,
'content' => $msg,
'isLongSms' => $needstatus
);

$result = $this->curlPost( self::API_SEND_URL , $postArr);
return $result;

}

/**
* 处理返回值
*/

public function execResult($result){

$result=preg_split("/[,rn]/",$result);

return $result;

}

/**
* 通过CURL发送HTTP请求
* @param string $url //请求URL
* @param array $postFields //请求参数
* @return mixed
*/

private function curlPost($url,$postFields){

$postFields = http_build_query($postFields);

$ch = curl_init ();

curl_setopt ( $ch, CURLOPT_POST, 1 );

curl_setopt ( $ch, CURLOPT_HEADER, 0 );

curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );

curl_setopt ( $ch, CURLOPT_URL, $url );

curl_setopt ( $ch, CURLOPT_POSTFIELDS, $postFields );

$result = curl_exec ( $ch );

curl_close ( $ch );

return $result;

}

//魔术获取

public function __get($name){

return $this->$name;

}

//魔术设置

public function __set($name,$value){

$this->$name=$value;

}

}

?>