public function getLocks() { $requestUrl = $this->baseURL . "/api/smartapps/installations/" . $this->smartthings->getEndpoint() . "/lock"; $curl = new \anlutro\cURL\cURL(); $result = $curl->newRequest('GET', $requestUrl)->setHeader('content-type', 'application/json')->setHeader('Accept', 'json')->setHeader('Authorization', 'Bearer ' . $this->smartthings->getAccessToken())->setOptions([CURLOPT_VERBOSE => true])->send(); $switches = json_decode($result->body); return $switches; }
/** * @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; } }
/** * 发起请求 * @param $uri * @param string $method * @param array $data * @return mixed * @throws NotSupportedException */ private function _request($uri, $method = 'get', array $data = []) { $curl = new cURL(); $url = $this->_createUrl($uri); $request = $curl->newRequest($method, $url, $data); $response = $this->_createHttpHeader($request)->send(); // todo: 记录接口日志 return $response; }