本文共 2316 字,大约阅读时间需要 7 分钟。
PHP接口请求类
<?phpheader("Content-type:text/html; charset=UTF-8");/
class ChuanglanSmsApi {
//Interface URL Used to send SMSconst API_SEND_URL='http://intapi.253.com/send/json?';//Interface URL Used to Query SMS balanceconst API_BALANCE_QUERY_URL='http://intapi.253.com/balance/json?';const API_ACCOUNT='';//Get SMS Account from https://zz.253.com/site/login.html const API_PASSWORD='';//Get SMS Password from https://zz.253.com/site/login.html/** * 发送短信 * * @param string $mobile 手机号码 * @param string $msg 短信内容 */public function sendInternational( $mobile, $msg) { //创蓝接口参数 $postArr = array ( 'account' => self::API_ACCOUNT, 'password' => self::API_PASSWORD, 'msg' => $msg, 'mobile' => $mobile ); $result = $this->curlPost( self::API_SEND_URL , $postArr); return $result;}/** * 查询额度 * * 查询地址 */public function queryBalance() { //查询参数 $postArr = array ( 'account' => self::API_ACCOUNT, 'password' => self::API_PASSWORD, ); $result = $this->curlPost(self::API_BALANCE_QUERY_URL, $postArr); return $result;}/** * 通过CURL发送HTTP请求 * @param string $url //请求URL * @param array $postFields //请求参数 * @return mixed */private function curlPost($url,$postFields){ $postFields = json_encode($postFields); $ch = curl_init (); curl_setopt( $ch, CURLOPT_URL, $url ); curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json; charset=utf-8' ) ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt( $ch, CURLOPT_POST, 1 ); curl_setopt( $ch, CURLOPT_POSTFIELDS, $postFields); curl_setopt( $ch, CURLOPT_TIMEOUT,1); curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0); $ret = curl_exec ( $ch ); if (false == $ret) { $result = curl_error( $ch); } else { $rsp = curl_getinfo( $ch, CURLINFO_HTTP_CODE); if (200 !== $rsp) { $result = "请求状态 ". $rsp . " " . curl_error($ch); } else { $result = $ret; } } curl_close ( $ch ); return $result;}
}
转载于:https://blog.51cto.com/13750674/2141664