public function getOTP(Request $request, $mail) { if (filter_var($mail, FILTER_VALIDATE_EMAIL)) { $key = $request->headers->get('pub-key'); $app = \App\Aplication::where('public_key', $key)->get(); //return var_dump($app[0]->id); if (isset($app[0])) { $client = \App\Client::where('email', $mail)->get(); if (isset($client[0])) { $list = ['libertad', 'patria', 'soberano', 'independencia', 'avanzadora', 'chavez', 'cimarron', 'cacao']; $code = $list[rand(0, count($list) - 1)] . rand(100, 999); /* Solicitar envio de SMS */ $curl = new cURL(); $params = array('telf' => $client[0]->phone, 'texto' => 'Hemos eviado una clave de acceso temporal para la aplicación: ' . $app[0]->name . ' Su clave temporal OTP es: ' . $code, 'app_name' => 'ssas'); $url = $curl->buildUrl('http://desarrollo.ssas.gob.ve/rest/SMS', $params); $curl->get($url); /* Solicitar envio de Correo */ $email = $client[0]->email; \Mail::send('otp_mail', ['aplication' => $app[0]->name, 'otp' => $code], function ($message) use($email) { $message->from('*****@*****.**', 'Aplicaciones SIAS'); $message->to($email); $message->subject('Clave de Acceso Temporal'); }); /* Encriptamos el otp con clave privada */ $encrypt = crypt($code, $app[0]->private_key); /* Almacenamos OTP */ $otp = new Otp(); $otp->code = $encrypt; $otp->status = 'D'; $otp->clients_id = $client[0]->id; $otp->aplication_id = $app[0]->id; $otp->save(); return array('crypt' => $encrypt); } else { return response()->json(['error' => 'no autorizado'], 401); } } else { return response()->json(['error' => 'no autorizado'], 401); } } else { return response()->json(['error' => 'no autorizado'], 401); } }
/** * @param $responseClass * @return BasicResponse * @throws TooManyRequestsException * @throws \Exception */ public function make($responseClass = BasicResponse::class) { $connection = $this->getConnection(); $curl = new cURL(); $url = $curl->buildUrl($connection->getApiEntryPoint() . $this->getMethod() . '.' . $this->getFormat(), $this->getParameters()); if ($connection->isCached($url)) { return $connection->getCached($url); } try { /** @var Request $request */ $request = $curl->newRequest('get', $url)->setOption(CURLOPT_CONNECTTIMEOUT, static::CONNECTION_TIMEOUT)->setOption(CURLOPT_TIMEOUT, static::REQUEST_TIMEOUT)->setOption(CURLOPT_SSL_VERIFYPEER, static::SSL_VERIFICATION); $response = new $responseClass($request->send(), $this); $this->resetRetriesCount(); } catch (TooManyRequestsException $e) { if (!$this->canRetry()) { throw $e; } $this->prepareRetry(); return $this->make($responseClass); } return $connection->setCached($url, $response); }
private function _request($url, $method = 'POST', $parameters = []) { if ($url[0] != '/') { throw new ApiConnectorException('Your service url must begin with slash.'); } $response = null; $curl = new cURL(); $token = $this->generateToken($parameters); $parameters['__token'] = $token; foreach ($this->files as $file) { $parameters[$file->getPostFilename()] = $file; } $return = null; $url = $this->ApiServer->getEndpointUrl() . $url; //$url = 'http://teste.local/index.php'; /*if(in_array($method, ['POST', 'PUT'])) { $req = curl_init($url); curl_setopt($req, CURLOPT_POST, 1); curl_setopt($req, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($req, CURLOPT_URL, $url); curl_setopt($req, CURLOPT_RETURNTRANSFER, 1); if(count($this->files) > 0) { //curl_setopt($req, CURLOPT_UPLOAD, 1); //curl_setopt($req, CURLOPT_UPLOAD, 1); //$request->setOption(CURLOPT_SAFE_UPLOAD, true); curl_setopt($req, CURLOPT_HTTPHEADER, ['Content-type: multipart/form-data']); } //dd($url, $parameters); curl_setopt($req, CURLOPT_POSTFIELDS, http_build_query($parameters)); $retorno = curl_exec($req); dd('aki', $retorno, 'arquivo existe??', file_exists($this->files[0]->getFilename()), $this->files[0]->getFilename()); $retorno = $this->translate($retorno); return $retorno; }*/ $request = $curl->newRequest($method, $url, $parameters); /*if(count($this->files) > 0) { $request->setOption(CURLOPT_UPLOAD, true); $request->setOption(CURLOPT_SAFE_UPLOAD, true); }*/ $request->setHeader('X-Requested-With', 'XMLHttpRequest'); if (class_exists('Illuminate\\Support\\Facades\\Config')) { $request->setHeader('language', \Illuminate\Support\Facades\Config::get('app.locale')); } $request->setOption(CURLOPT_FOLLOWLOCATION, 1); if ($method == 'GET' || $method == 'DELETE') { $parameters['__token'] = $token; $iurl = $curl->buildUrl($url, $parameters); $request->setUrl($iurl); } if (in_array($method, ['DELETE', 'PUT'])) { $parameters['_method'] = $method; $method = 'POST'; } try { if (count($this->files) > 0) { $novo = []; $this->http_build_query_for_curl($parameters, $novo); foreach ($this->files as $file) { $novo[$file->getPostFilename()] = $file; } $curl->setDefaultOptions([CURLOPT_HEADER => ['Content-type: multipart/form-data']]); switch ($method) { case 'POST': $return = $curl->rawPost($url, $novo); break; case 'PUT': $url = $curl->buildUrl($url, $novo); //Seria um problema do verbo put? Ter que passar parametros via get por não suportar uma payload assim como o POST.... $return = $curl->rawPut($url, $novo); break; } } else { $return = $request->send(); } //$return = $request->send(); } catch (\Exception $err) { throw new \Exception('Falha ao conectar. Url completa: ' . $url . ' | token: ' . $parameters['__token'] . ' exception: ' . $err->getMessage()); } try { $ret = $this->translate($return->body); $ret->response = $return; return $ret; } catch (\Exception $e) { return $return->body; } }