1. 程式人生 > >極驗獲取資料的程式碼

極驗獲取資料的程式碼

獲取從前臺傳送的驗證碼
$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')));
}