public function Send(array $request) { $method = isset($request['METHOD']) ? strtoupper($request['METHOD']) : ''; if ($method !== \Bitrix\Main\Web\HttpClient::HTTP_GET && $method !== \Bitrix\Main\Web\HttpClient::HTTP_POST) { throw new Bitrix\Main\ArgumentException("Could not find 'METHOD'.", 'request'); } $path = isset($request['PATH']) && is_string($request['PATH']) ? $request['PATH'] : ''; if ($path === '') { throw new Bitrix\Main\ArgumentException("Could not find 'PATH'.", 'request'); } $postData = $method === \Bitrix\Main\Web\HttpClient::HTTP_POST && isset($request['BODY']) ? $request['BODY'] : null; if (!$this->client) { $this->client = new \Bitrix\Main\Web\HttpClient(); } $this->client->setRedirect(false); if ($method === \Bitrix\Main\Web\HttpClient::HTTP_POST && is_array($postData)) { //Force UTF encoding $this->client->setCharset('UTF-8'); if ((!isset($request['UTF']) || !$request['UTF']) && !defined('BX_UTF')) { $postData = \Bitrix\Main\Text\Encoding::convertEncodingArray($postData, SITE_CHARSET, 'UTF-8'); } } $headers = isset($request['HEADERS']) ? $request['HEADERS'] : null; if (is_array($headers)) { foreach ($headers as $k => $v) { $this->client->setHeader($k, $v, true); } } if (!empty($this->cookies)) { $this->client->setCookies($this->cookies); } if ($this->enableProxy) { $this->client->setProxy($this->proxyServer, $this->proxyPort, $this->proxyUserName, $this->proxyUserPassword); } if ($this->userName !== '') { $this->client->setAuthorization($this->userName, $this->userPassword); } $this->client->setHeader('User-Agent', $this->userAgent, true); $absolutePath = $this->GetUrl() . $path; if (!$this->client->query($method, $absolutePath, $postData)) { $this->responseData = null; $this->errors = $this->client->getError(); } else { /**@var \Bitrix\Main\Web\HttpHeaders*/ $responseHeaders = $this->client->getHeaders(); //STATUS.VERSION & STATUS.PHRASE are delcared for backward compatibility only. $this->responseData = array('STATUS' => array('VERSION' => '', 'CODE' => $this->client->getStatus(), 'PHRASE' => ''), 'CONTENT' => array('TYPE' => $this->client->getContentType(), 'ENCODING' => $this->client->getCharset()), 'HEADERS' => $responseHeaders, 'BODY' => $this->client->getResult()); if ($responseHeaders->get('Set-Cookie', false) !== null) { $this->cookies = array_merge($this->cookies, $this->client->getCookies()->toArray()); CCrmExternalSale::Update($this->externalSaleId, array('COOKIE' => serialize($this->cookies))); } $this->errors = array(); } return $this->responseData; }
private function getFileSizeInternal($downloadUrl) { $accessToken = $this->getAccessToken(); $http = new HttpClient(array('socketTimeout' => 10, 'streamTimeout' => 30, 'version' => HttpClient::HTTP_1_1)); $http->setHeader('Authorization', "Bearer {$accessToken}"); if ($http->query('HEAD', $downloadUrl) === false) { $errorString = implode('; ', array_keys($http->getError())); $this->errorCollection->add(array(new Error($errorString, self::ERROR_HTTP_GET_METADATA))); return null; } if (!$this->checkHttpResponse($http)) { return null; } return $http->getHeaders()->get('Content-Length'); }
/** * Returns HttpClient object with query result * * @param string $scope Url to call * @param string $method HTTP method (GET/POST/PUT supported) * @param array|null $data Post data * @param bool $skipRefreshAuth Skip authorization refresh * * @returns \Bitrix\Main\Web\HttpClient * @throws SystemException */ protected function query($scope, $method = "GET", $data = null, $skipRefreshAuth = false) { if ($this->engineSettings['AUTH']) { $http = new Web\HttpClient(); $http->setHeader('Authorization', 'OAuth ' . $this->engineSettings['AUTH']['access_token']); $http->setRedirect(false); switch ($method) { case 'GET': $http->get($scope); break; case 'POST': $http->post($scope, $data); break; case 'PUT': $http->query($method, $scope, $data); break; case 'DELETE': break; } if ($http->getStatus() == 401 && !$skipRefreshAuth) { if ($this->checkAuthExpired(false)) { $this->query($scope, $method, $data, true); } } return $http; } else { throw new SystemException("No Yandex auth data"); } }
protected function query($scope, $method = "GET", $data = null, $bSkipRefreshAuth = false, $contentType = 'application/json') { if ($this->engineSettings['AUTH']) { $http = new HttpClient(); $http->setHeader("Authorization", 'Bearer ' . $this->engineSettings['AUTH']['access_token']); /* $http->setAdditionalHeaders( array( 'Authorization' => , 'GData-Version' => '2' ) ); */ switch ($method) { case 'GET': $result = $http->get($scope); break; case 'POST': case 'PUT': $http->setHeader("Content-Type", $contentType); $result = $http->query($method, $scope, $data); break; case 'DELETE': break; } if ($http->getStatus() == 401 && !$bSkipRefreshAuth) { if ($this->checkAuthExpired(true)) { return $this->query($scope, $method, $data, true, $contentType); } } return $http; } }
/** * @param Uri $uri Absolute URL to get metadata for. * @return array|false */ protected static function getRemoteUrlMetadata(Uri $uri) { $httpClient = new HttpClient(); $httpClient->setTimeout(5); $httpClient->setStreamTimeout(5); if (!$httpClient->query('GET', $uri->getUri())) { return false; } if ($httpClient->getStatus() !== 200) { return false; } $htmlContentType = strtolower($httpClient->getHeaders()->getContentType()); if ($htmlContentType !== 'text/html') { return static::getFileMetadata($httpClient->getEffectiveUrl(), $httpClient->getHeaders()); } $html = $httpClient->getResult(); $htmlDocument = new HtmlDocument($html, $uri); $htmlDocument->setEncoding($httpClient->getCharset()); ParserChain::extractMetadata($htmlDocument); $metadata = $htmlDocument->getMetadata(); if (is_array($metadata) && static::validateRemoteMetadata($metadata)) { if (isset($metadata['IMAGE']) && static::getOptionSaveImages()) { $metadata['IMAGE_ID'] = static::saveImage($metadata['IMAGE']); unset($metadata['IMAGE']); } return $metadata; } return false; }
/** * @param FileData $fileData * @return bool */ public function deleteFile(FileData $fileData) { if (!$this->checkRequiredInputParams($fileData->toArray(), array('id'))) { return null; } $accessToken = $this->getAccessToken(); $http = new HttpClient(array('redirect' => false, 'socketTimeout' => 10, 'streamTimeout' => 30, 'version' => HttpClient::HTTP_1_1)); $accessToken = urlencode($accessToken); if ($http->query('DELETE', "https://apis.live.net/v5.0/{$fileData->getId()}?access_token={$accessToken}") === false) { $errorString = implode('; ', array_keys($http->getError())); $this->errorCollection->add(array(new Error($errorString, self::ERROR_HTTP_DELETE_FILE))); return false; } if (!$this->checkHttpResponse($http)) { return false; } return true; }