曾经无意间知道了有短信验证码接收平台这个产物,随后试用了几次,感觉不错,遂阅读了飞Q的API文档,写出了这个类。大牛就路过吧……
<?php
/**
* Name :飞Q短信验证码-API-PHP-Class
* Author :Xiyan
* Author_Home:http://www.shenqhy.com/
* Version :0.1.1 (Beta)
* Notice :如果发现问题,欢迎反馈!
* License :LGPL
*/
class fqcode
{
public $token;
public $NextToken;
public $uid;
public $stats;
public $logs;
public $apiUrl = 'http://sms.xudan123.com/do.aspx';
function __construct()
{
date_default_timezone_set('PRC');
$this->logs = '['.date('Y 年 n 月 d 日 H:i:s',time()).'] [Initialization]'."\n";
}
//CURL发送HTTP POST请求
public function HttpPost($api_post_url, $api_post_data, $head_array ='', $post_time = 2, $post_timeout = 2)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_post_url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $api_post_data);
if (!empty($head_array)) {curl_setopt($ch, CURLOPT_HTTPHEADER, $head_array);}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $post_time);
curl_setopt($ch, CURLOPT_TIMEOUT, $post_timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
//字段生成(仅支持单位数组)
public function GenerateFields($array)
{
if (is_array($array))
{
$n = count($array);//获取数组键数
$i = 0;
$Fields = '';
foreach ($array as $key => $value)
{
$i++;
if ($i<$n) {$lsymbol = '&';}else{$lsymbol = '';}
$Fields .= $key.'='.$value.$lsymbol;
}
unset($i,$n,$lsymbol,$key,$value);
return $Fields;
}else{
return false;
}
}
//简单的日志记录
public function Log($logs,$qqq ='')
{
$date = date('Y 年 n 月 d 日 H:i:s',time());
$this->logs .= $qqq."[{$date}] [{$logs}]\n";
}
//错误处理
public function ProcessError($data)
{
switch ($data[0]) {
case 'unknow_error':
$this->Log('(False)unknow_error');
return 'unknow_error';
break;
case 'not_login':
$this->Log('(False)not_login');
return 'not_login';
break;
case 'not_found_project':
$this->Log('(False)not_found_project');
return 'not_found_project';
break;
case 'not_found_moblie':
$this->Log('(False)not_found_moblie');
return 'not_found_moblie';
break;
case 'login_error':
$this->Log('(False)login_error');
return 'login_error';
break;
case 'mobile_exists':
$this->Log('(False)mobile_exists');
return 'mobile_exists';
break;
case 'not_receive':
$this->Log('(False)not_receive');
return 'not_receive';
break;
case 'parameter_error':
$this->Log('(False)parameter_error');
return 'parameter_error';
break;
case 'no_data':
$this->Log('(False)no_data');
return 'no_data';
break;
case 'project_state_error':
$this->Log('(False)project_state_error');
return 'project_state_error';
break;
case 'mobile_state_error':
$this->Log('(False)mobile_state_error');
return 'mobile_state_error';
break;
case 'max_count_disable':
$this->Log('(False)max_count_disable');
return 'max_count_disable';
break;
case 'message':
$this->Log('(False)'.'Message : '.$data[2]);
return 'please try again later';
break;
default:
$this->Log('Success!');
return $data;
break;
}
}
//分割数据
public function splite($sdata)
{
return explode('|', $sdata);
}
//f(user,password)
public function Login($uid,$pwd)
{
$this->Log('Start login.',"\n");
$fields = array(
'action' =>'loginIn',
'uid' =>$uid,
'pwd' =>$pwd
);
$data = $this->HttpPost($this->apiUrl,$this->GenerateFields($fields));
$temp = $this->ProcessError($this->splite($data));
unset($fields);
if ($temp != 'login_error') {
$this->uid = $temp[0];
$this->token = $temp[1];
return $temp;
}else{
return $temp;
}
}
//f(pid,[uid,可选],[token,可选])
public function GetMobileNum($pid)
{
$this->Log('Get mobile num',"\n");
$parametern = func_num_args();//获取传入的参数个数
if ($parametern == 3 && ($uid=func_get_arg(1)) != '' && ($token=func_get_arg(2)) != '') {//在条件中赋值
//如果传递了uid和token则uid为传入的,否则为登陆时的获取的
}elseif ($parametern < 3 && $parametern == 1 && ($uid=$this->uid) !='' && ($token=$this->token) != '') {
}else{
$this->Log('(False) Argument is null or not logged in!');
return;
}
$fields = array(
'action' =>'getMobilenum',
'pid' =>$pid,
'uid' =>$uid,
'token' =>$token
);
$data = $this->HttpPost($this->apiUrl,$this->GenerateFields($fields));
unset($fields);
return $this->ProcessError($this->splite($data));
}
//f(mobileNum,NextPid,[uid,可选],[token,可选])
public function GetVcodeAndHoldMobileNum($mobilenum,$nextPid)
{
$this->Log('Get the verification code and keep mobile number.',"\n");
$parametern = func_num_args();
if ($parametern == 4 && ($uid=func_get_arg(2)) != '' && ($token=func_get_arg(3)) != '') {
//如果传递了uid和token则uid为传入的,否则为登陆时的获取的
}elseif ($parametern < 4 && $parametern == 2 && ($uid=$this->uid) !='' && ($token=$this->token) != '') {
}else{
$this->Log('(False) Argument is null or not logged in!');
return;
}
$fields = array(
'action' =>'getVcodeAndHoldMobilenum',
'mobile' =>$mobilenum,
'next_pid'=>$nextPid,
'uid' =>$uid,
'token' =>$token,
'author_uid'=>'fqcode'
);
$data = $this->HttpPost($this->apiUrl,$this->GenerateFields($fields));
unset($fields,$uid,$token,$parametern,$$nextPid);
return $this->ProcessError($this->splite($data));
}
//f(mobileNum,[uid,可选],[token,可选])
public function GetVcodeAndReleaseMobile($mobilenum)
{
$this->Log('Get the verification code and no longer use.',"\n");
$parametern = func_num_args();
if ($parametern == 3 && ($uid=func_get_arg(1)) != '' && ($token=func_get_arg(2)) != '') {
//如果传递了uid和token则uid为传入的,否则为登陆时的获取的
}elseif ($parametern < 3 && $parametern == 1 && ($uid=$this->uid) !='' && ($token=$this->token) != '') {
}else{
$this->Log('(False) Argument is null or not logged in!');
return;
}
$fields = array(
'action' =>'getVcodeAndReleaseMobile',
'mobile' =>$mobilenum,
'uid' =>$uid,
'token' =>$token,
'author_uid'=>'fqcode'
);
$data =$this->HttpPost($this->apiUrl,$this->GenerateFields($fields));
unset($fields,$uid,$token,$parametern);
return $this->ProcessError($this->splite($data));
}
//f(pid,mobileNum,[uid,可选],[token,可选])
public function AddIgnoreList($pid,$mobiles)
{
$this->Log('Add several phone numbers to blacklist.',"\n");
$parametern = func_num_args();
if ($parametern == 4 && ($uid=func_get_arg(2)) != '' && ($token=func_get_arg(3)) != '') {
}elseif ($parametern < 4 && $parametern == 2 && ($uid=$this->uid) !='' && ($token=$this->token) != '') {
}else{
$this->Log('(False) Argument is null or not logged in!');
}
$fields=array(
'action' =>'addIgnoreList',
'pid' =>$pid,
'uid' =>$uid,
'token' =>$token
);
$data =$this->HttpPost($this->apiUrl,$this->GenerateFields($fields));
unset($fields,$uid,$token,$parametern);
if ($data > 0) {
$this->Log('Success');
return $data;
}else{
return $this->ProcessError($this->splite($data));
}
}
//f(pid,[uid,可选],[token,可选])
public function ClearIgnoreList($pid)
{
$this->Log('Empty certain (All) project blacklist all.',"\n");
if ($pid == '') {
//如果为传入PID则会记录提示
$this->Log('Note: PID is empty, all projects will be emptied blacklist!');
intval($pid,10);
}
$parametern = func_num_args();
if ($parametern == 3 && ($uid=func_get_arg(1)) != '' && ($token=func_get_arg(2)) != '') {
}elseif ($parametern < 3 && $parametern == 1 && ($uid=$this->uid) !='' && ($token=$this->token) != '') {
}else{
$this->Log('(False) Argument is null or not logged in!');
}
$fields = array(
'action' => 'clearIgnoreList',
'pid' => $pid,
'uid' => $uid,
'token' => $token
);
$data =$this->HttpPost($this->apiUrl,$this->GenerateFields($fields));
unset($fields,$uid,$token,$parametern);
if ($data > 0) {
$this->Log('Success');
return $data;
}else{
return $this->ProcessError($this->splite($data));
}
}
//f(pid,[uid,可选],[token,可选])
public function GetRecvingInfo($pid)
{
$this->Log('Get a list of numbers being used in the project.',"\n");
if ($pid == '') {
//如果为传入PID则会记录提示
$this->Log('Note, PID is empty, it will return a list of all.');
intval($pid,10);
}
$parametern = func_num_args();
if ($parametern == 3 && ($uid=func_get_arg(1)) != '' && ($token=func_get_arg(2)) != '') {
}elseif ($parametern < 3 && $parametern == 1 && ($uid=$this->uid) !='' && ($token=$this->token) != '') {
}else{
$this->Log('(False) Argument is null or not logged in!');
}
$fields = array(
'action' => 'getRecvingInfo',
'pid' => $pid,
'uid' => $uid,
'token' => $token
);
$data =$this->HttpPost($this->apiUrl,$this->GenerateFields($fields));
unset($fields,$uid,$token,$parametern);
if (!json_decode($data)) {
$this->ProcessError($this->splite($data));
return $data;
}else{
return json_decode($data);
}
}
//f(mobileNum,[uid,可选],[token,可选])
public function CancelSMSRecv($mobilenum)
{
$this->Log('Cancel a text message receiving, the immediate release of the amount to be locked.',"\n");
if ($mobilenum == '') {
$this->Log('Mobile num is empty.');
return;
}
$parametern = func_num_args();
if ($parametern == 3 && ($uid=func_get_arg(1)) != '' && ($token=func_get_arg(2)) != '') {
}elseif ($parametern < 3 && $parametern == 1 && ($uid=$this->uid) !='' && ($token= $this->token) != '') {
}else{
$this->Log('(False) Argument is null or not logged in!');
}
$fields= array(
'action' => 'cancelSMSRecv',
'mobile' => $mobilenum,
'uid' => $uid,
'token' => $token
);
$data =$this->HttpPost($this->apiUrl,$this->GenerateFields($fields));
unset($fields,$uid,$token,$parametern);
if ($data = 1) {
$this->Log('Success');
return $data;
}else{
return $this->ProcessError($this->splite($data));
}
}
//f([uid,可选],[token,可选])
public function CancelSMSRecvAll()
{
$this->Log('Cancel all text message receiving, the immediate release of the all amount to be locked.',"\n");
$parametern = func_num_args();
if ($parametern == 2 && ($uid=func_get_arg(0)) != '' && ($token=func_get_arg(1)) != '') {
}elseif ($parametern <= 2 && ($uid=$this->uid) !='' && ($token=$this->token) != '') {
}else{
$this->Log('(False) Argument is null or not logged in!');
}
$fields= array(
'action' => 'cancelSMSRecvAll',
'uid' => $uid,
'token' => $token
);
$data =$this->HttpPost($this->apiUrl,$this->GenerateFields($fields));
unset($fields,$uid,$token,$parametern);
if ($data = 1) {
$this->Log('Success');
return $data;
}else{
return $this->ProcessError($this->splite($data));
}
}
//f(u,r_u)
public function CheckUserRefer($u,$r_u)
{
$this->Log('Check a user whether is another user\'s Agent.',"\n");
if (func_num_args()<2) {
$this->Log('Lack of necessary parameters.');
}elseif ($u == '' | $r_u == '') {
$this->Log('The parameter is incorrect.');
}else{
$this->Log('Unknown error.');
}
$fields = array(
'u' => $u,
'r_u' => $r_u
);
$data =$this->HttpPost($this->apiUrl,$this->GenerateFields($fields));
unset($u,$uid,$r_u);
return $this->ProcessError($this->splite($data));
}
}
?>
文档也没写。。。不过和飞Q上的API文档的命名以及参数没多少差别,也就是初始化后登陆,然后uid和token变成可选项(uid在前,token在后)而已
转载请注明:神奇海域 » 飞Q验证码API-PHP-Class