/** * Calls API * @param string $controller Group name * @param string $action Method name * @param mixed $data Input data * @param bool $assoc Result format. true - array, false - object * @return mixed Result * @throws \Exception Case error during requesting */ public function send($controller, $action, $data, $assoc = true) { global $APPLICATION; $http = new \Bitrix\Main\Web\HttpClient(array("version" => "1.1", "socketTimeout" => 30, "streamTimeout" => 30, "redirect" => true, "redirectMax" => 5)); $http->setHeader("Content-Type", "application/json; charset=utf-8"); $http->setHeader("Authorization", "Basic " . base64_encode($this->apiLogin . ":" . $this->apiKey)); if (strtolower(SITE_CHARSET) != 'utf-8') { $data = $APPLICATION->ConvertCharsetArray($data, SITE_CHARSET, 'utf-8'); } $jsonData = json_encode($data); $result = $http->post($this->constructApiUrl($controller, $action), $jsonData); $errors = $http->getError(); if (!$result && !empty($errors)) { $strError = ""; foreach ($errors as $errorCode => $errMes) { $strError .= $errorCode . ": " . $errMes; } throw new \Exception($strError); } else { $status = $http->getStatus(); if ($status != 200) { throw new \Exception(sprintf('HTTP error code: %d', $status)); } $resData = $http->getResult(); $decodedResult = json_decode($resData, $assoc); if (strtolower(SITE_CHARSET) != 'utf-8') { $decodedResult = $APPLICATION->ConvertCharsetArray($decodedResult, 'utf-8', SITE_CHARSET); } } return $decodedResult; }
public static function addLogo($token, $domain, $file, &$error) { $http = new \Bitrix\Main\Web\HttpClient(); $boundary = 'CMY' . md5(rand() . time()); $data = ''; $data .= '--' . $boundary . "\r\n"; $data .= 'Content-Disposition: form-data; name="token"' . "\r\n\r\n"; $data .= $token . "\r\n"; $data .= '--' . $boundary . "\r\n"; $data .= 'Content-Disposition: form-data; name="domain"' . "\r\n\r\n"; $data .= $domain . "\r\n"; $data .= '--' . $boundary . "\r\n"; $data .= 'Content-Disposition: form-data; name="file"; filename="file"' . "\r\n"; $data .= 'Content-Type: application/octet-stream' . "\r\n\r\n"; $data .= file_get_contents($file) . "\r\n"; $data .= '--' . $boundary . "--\r\n"; $http->setHeader('Content-type', 'multipart/form-data; boundary=' . $boundary); $http->setHeader('Content-length', CUtil::binStrlen($data)); $response = $http->post('https://pddimp.yandex.ru/api/add_logo.xml', $data); $result = new CDataXML(); $result->loadString($response); if ($logoUrlNode = $result->selectNodes('/action/domains/domain/logo/url')) { return $logoUrlNode->textContent(); } self::setError2($result, $error); return false; }
public function sendStatus($orderId, $status, $substatus = false) { global $APPLICATION; if (strlen($this->yandexApiUrl) <= 0 || strlen($this->campaignId) <= 0 || intval($orderId) <= 0 || strlen($status) <= 0 || strlen($this->oAuthToken) <= 0 || strlen($this->oAuthClientId) <= 0 || strlen($this->oAuthLogin) <= 0) { return false; } $format = $this->communicationFormat == self::JSON ? 'json' : 'xml'; $url = $this->yandexApiUrl . "campaigns/" . $this->campaignId . "/orders/" . $orderId . "/status." . $format; $http = new \Bitrix\Main\Web\HttpClient(array("version" => "1.1", "socketTimeout" => 30, "streamTimeout" => 30, "redirect" => true, "redirectMax" => 5)); $arQuery = array("order" => array("status" => $status)); if ($substatus) { $arQuery["order"]["substatus"] = $substatus; } if (strtolower(SITE_CHARSET) != 'utf-8') { $arQuery = $APPLICATION->ConvertCharsetArray($arQuery, SITE_CHARSET, 'utf-8'); } $postData = ''; if ($this->communicationFormat == self::JSON) { $postData = json_encode($arQuery); } $http->setHeader("Content-Type", "application/" . $format); $http->setHeader("Authorization", 'OAuth oauth_token="' . $this->oAuthToken . '", oauth_client_id="' . $this->oAuthClientId . '", oauth_login="******"', false); $result = $http->query("PUT", $url, $postData); $errors = $http->getError(); if (!$result && !empty($errors)) { $bResult = false; $message = "HTTP ERROR: "; foreach ($errors as $errorCode => $errMes) { $message .= $errorCode . ": " . $errMes; } } else { $headerStatus = $http->getStatus(); if ($headerStatus == 200) { $message = GetMessage("SALE_YMH_STATUS") . ": " . $status; $bResult = true; } else { $res = $http->getResult(); $message = "HTTP error code: " . $headerStatus . "(" . $res . ")"; if ($headerStatus == 403) { $this->notifyAdmin("SEND_STATUS_ERROR_403"); } if ($headerStatus == 500) { $intervalSeconds = 3600; $timeToStart = ConvertTimeStamp(strtotime(date('Y-m-d H:i:s', time() + $intervalSeconds)), 'FULL'); \CAgent::AddAgent('\\CSaleYMHandler::sendStatusAgent("' . $orderId . '","' . $status . '", "' . $substatus . '", "' . $this->siteId . '");', 'sale', "N", $intervalSeconds, $timeToStart, "Y", $timeToStart); } $bResult = false; } } $this->log($bResult ? self::LOG_LEVEL_INFO : self::LOG_LEVEL_ERROR, "YMARKET_STATUS_CHANGE", $orderId, $message); return $bResult; }
public function sendMessage($user_id, $uid, $message) { $this->SetOauthKeys($user_id); if ($this->access_token === false) { return false; } $message = CharsetConverter::ConvertCharset($message, LANG_CHARSET, "utf-8"); $arParams = array_merge($this->GetDefParams(), array("oauth_token" => $this->token, "user_id" => $uid, "text" => $message)); $arParams["oauth_signature"] = urlencode($this->BuildSignature($this->GetSignatureString($arParams, self::MESSAGE_URL))); $arHeaders = array("Authorization" => 'OAuth oauth_consumer_key="' . $arParams["oauth_consumer_key"] . '", oauth_nonce="' . $arParams["oauth_nonce"] . '", oauth_signature="' . $arParams["oauth_signature"] . '", oauth_signature_method="HMAC-SHA1", oauth_timestamp="' . $arParams["oauth_timestamp"] . '", oauth_token="' . $this->token . '", oauth_version="1.0"'); $arPost = array("user_id" => $uid, "text" => $message); $ob = new \Bitrix\Main\Web\HttpClient(); foreach ($arHeaders as $header => $value) { $ob->setHeader($header, $value); } return $ob->post(self::MESSAGE_URL, $arPost); }
public static function setLogo($token, $domain, $file, &$error) { $http = new \Bitrix\Main\Web\HttpClient(); $boundary = 'CMY2' . md5(rand() . time()); $data = ''; $data .= '--' . $boundary . "\r\n"; $data .= 'Content-Disposition: form-data; name="token"' . "\r\n\r\n"; $data .= $token . "\r\n"; $data .= '--' . $boundary . "\r\n"; $data .= 'Content-Disposition: form-data; name="domain"' . "\r\n\r\n"; $data .= $domain . "\r\n"; $data .= '--' . $boundary . "\r\n"; $data .= 'Content-Disposition: form-data; name="file"; filename="logo"' . "\r\n"; $data .= 'Content-Type: application/octet-stream' . "\r\n\r\n"; $data .= file_get_contents($file) . "\r\n"; $data .= '--' . $boundary . "--\r\n"; $http->setHeader('Content-type', 'multipart/form-data; boundary=' . $boundary); $http->setHeader('Content-length', CUtil::binStrlen($data)); $response = $http->post('https://pddimp.yandex.ru/api2/admin/domain/logo/set', $data); $result = json_decode($response, true); if (isset($result['success']) && $result['success'] == 'ok') { return true; } self::setError($result, $error); return false; }
public function GetCurrentUser() { if ($this->access_token === false) { return false; } $h = new \Bitrix\Main\Web\HttpClient(); $h->setHeader("Authorization", "Bearer " . $this->access_token); $result = $h->get(static::ACCOUNT_URL); $result = \Bitrix\Main\Web\Json::decode($result); if (is_array($result)) { $result["access_token"] = $this->access_token; } return $result; }
public function GetCurrentUser() { if ($this->access_token === false) { return false; } $httpClient = new \Bitrix\Main\Web\HttpClient(); $httpClient->setHeader("Authorization", "Bearer " . $this->access_token); $result = $httpClient->get($this->resource . static::VERSION . static::CONTACTS_URL); $result = \Bitrix\Main\Web\Json::decode($result); if (is_array($result)) { $result["access_token"] = $this->access_token; $result["refresh_token"] = $this->refresh_token; $result["expires_in"] = $this->accessTokenExpires; } return $result; }