代码拉取完成,页面将自动刷新
<?php
/**
* Author: dungang
* Date: 2017/4/12
* Time: 14:52
*/
namespace dungang\luosimao;
use yii\helpers\Json;
use yii\validators\Validator;
class CaptchaValidator extends Validator
{
public $messages = [
'-500'=>'验证码服务器挂了',
'-10'=>'API KEY 为空',
'-11'=>'response为空',
'-20'=>'response错误',
'-40'=>'API_KEY使用错误',
];
public function validateAttribute($model, $attribute)
{
if (empty(\Yii::$app->params['luosimao']) ||
empty(\Yii::$app->params['luosimao']['apiKey'])) {
$this->addError($model,$attribute,\Yii::t('app','Lost luosimao captcha config'));
} else {
$apiKey = \Yii::$app->params['luosimao']['apiKey'];
if($response = \Yii::$app->request->post('luotest_response')) {
$rst = $this->request('https://captcha.luosimao.com/api/site_verify',true,[
'api_key'=>$apiKey,
'response'=>$response
]);
if ($rst) {
$rst = Json::decode($rst);
if ($rst['res'] == 'success') {
return null;
} else {
$this->addError($model,$attribute,isset($this->message[$rst['error']])
?$this->message[$rst['error']]
:$rst['msg']);
}
}
}
$this->addError($model,$attribute,\Yii::t('app','Server Error'));
}
}
/**
* @param $url
* @param $isPost
* @param array $data
* @return mixed
* @throws \Exception
*/
public function request($url,$isPost,$data=[])
{
$ch = curl_init();
//set url
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
//定义post
if ($isPost) {
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
}
$response = curl_exec($ch);
if (curl_errno($ch)) {
throw new \Exception(curl_error($ch), 0);
} else {
$httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (200 !== $httpStatusCode) {
throw new \Exception($response, $httpStatusCode);
}
}
curl_close($ch);
return $response;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。