获取从前台发送的验证码
$luotest_response = $request->get('verifyCode');
第二步 发送 验证
$transfer = new Transfer("https://captcha.luosimao.com/api/site_verify","POST");
文件内容:
<?php namespace App\PlugIn;
use App\PlugIn\Secret\Des;
use Config,Session;
class Transfer
{
/**
* @var string 服务器返回的数据流
*/
protected $stream = '';
/**
* @var null 解析后的结果
*/
protected $body = null;
/**
* @var string 接口路径
*/
protected $gateway = '';
/**
* 发送方式
*
* @var string
*/
protected $method = '';
/**
* @var array 要发送的数据
*/
protected $data = [];
/**
* 初始设置接口地址和发送方法
*
* @param $url
* @param $method
*/
public function __construct($url, $method)
{
$this->setGateWay($url);
$this->method = $method;
}
/**
* 设置网络访问地址
*
* @param $url
*/
private function setGateWay($url)
{
$this->gateway = $url;
}
/**
* 添加要发送的数据
*
* @param array $data
*/
public function addData(array $data)
{
$this->data = array_merge($this->data, $data);
}
/**
* 与服务器通信
*/
protected function transfer()
{
if (!function_exists('curl_init')) {
throw new Exception('NeedCurlExtension:Need to open the curl extension');
}
$ci = \curl_init();
$options = [ CURLOPT_TIMEOUT => 120,
CURLOPT_HEADER => 0,
CURLOPT_RETURNTRANSFER => 1, // 1:获取的信息以文件流的形式返回
CURLOPT_POSTFIELDS => $this->data, // post数据
CURLOPT_URL => $this->gateway,
CURLOPT_CUSTOMREQUEST => $this->method,
CURLOPT_SSL_VERIFYPEER => false ];
curl_setopt_array($ci, $options);
$this->stream = curl_exec($ci); //\jt\utils\Debug::log('TransferGetStream', $this->stream);
curl_close($ci); } /** * 分离解析数据 * * @return array */ private function parse() { //分离: 协议、头部信息、主体内容 //list(, $body) = \explode("\r\n\r\n", $this->stream); //var_dump($this->stream); //$this->stream = str_replace('null', '""', $this->stream); //echo "<pre>";print_r($this->stream);die; $this->body = \json_decode($this->stream, true); //Error::writeLog($this->body, 'transfer'); } /** * 为发送的数据签名 */ private function sign() { $this->data['key'] = \Config::PARTNER_KEY; if (RUN_MODE === 'test') { $this->data['is_test'] = 1; } ksort($this->data); $param = implode('&', $this->data); $this->data['sign'] = \md5($param . \Config::SIGN_SALT); } /** * 发送请求 * * @return array */ public function send() { //$this->sign(); $this->transfer(); $this->parse(); return $this->body; } /** * 直接获取结果,不解析 */ public function getStream() { $this->transfer(); return $this->stream; } /** * 请求数据加密 */ public function encrypt($data){ $xpp_key = Config::get('base.XPP_KEY'); $param_request = array( "sign" => "0ca415f9e07e4ed64f3ee4ef6bf7f87c", "nonce_str" =>"sm7c46ra3ryxk5dcw4f2k2drl".mt_rand(111111,999999), "timestamp" =>time(), "client" =>"street", "identifier" => Session::has("identifier") ? Session::get("identifier") : getUuid('identifier') ); $param = array_merge($param_request,$data); //将参数数组key值按照自然排序从大到小排序 ksort($param); unset($param['sign']); //将排序后的参数数组按照key=val&key=val的形式组成字符串,将字符串与XPP_KEY连接,用md5加密一次(32位小写),得到sign $sb = ''; foreach($param as $key=>$val){ $sb .= $key . '=' . $val . '&'; } $sb .= $xpp_key; $server_sign = md5($sb); $param["sign"] = $server_sign; $encrypt_str = Des::getInstance()->encrypt((json_encode($param))); return $encrypt_str; }}
$transfer->addData([
'api_key' => Config::get("app.verifykey"),
'response' => $luotest_response
]);
$res = $transfer->send();
if($res["error"]!=0 || $res["res"]!="success"){
return redirect()->back()->withErrors(array("login_error"=>"人机验证失败","username"=>$request->get('code')));
}