private function sendRequest($method, $args) { SpotTiming::start(__CLASS__ . '::' . __FUNCTION__); $reqarr = array('version' => '1.1', 'method' => $method, 'params' => $args); $content = json_encode($reqarr); /* * Actually perform the HTTP POST */ $svcProvHttp = new Services_Providers_Http(null); $svcProvHttp->setUsername($this->_username); $svcProvHttp->setPassword($this->_password); $svcProvHttp->setMethod('POST'); $svcProvHttp->setContentType('application/json'); $svcProvHttp->setRawPostData($content); $output = $svcProvHttp->perform($this->_url, null); if ($output['successful'] === false) { $errorStr = "ERROR: Could not decode json-data for NZBGet method '" . $method . "', " . $output['errorstr']; error_log($errorStr); throw new Exception($errorStr); } # if $response = json_decode($output['data'], true); if (is_array($response) && isset($response['error']) && isset($response['error']['code'])) { error_log("NZBGet RPC: Method '" . $method . "', " . $response['error']['message'] . " (" . $response['error']['code'] . ")"); throw new Exception("NZBGet RPC: Method '" . $method . "', " . $response['error']['message'] . " (" . $response['error']['code'] . ")"); } elseif (is_array($response) && isset($response['result'])) { $response = $response['result']; } SpotTiming::stop(__CLASS__ . '::' . __FUNCTION__, array($method, $args)); return $response; }
public function processNzb($fullspot, $nzblist) { $nzb = $this->prepareNzb($fullspot, $nzblist); $title = urlencode($this->cleanForFileSystem($fullspot['title'])); $category = urlencode($this->convertCatToSabnzbdCat($fullspot)); # yes, using a local variable instead of the member variable is intentional $url = $this->_url . '&nzbname=' . $title . '&cat=' . $category; /* * Actually perform the HTTP POST */ $svcProvHttp = new Services_Providers_Http(null); $svcProvHttp->setUsername($this->_username); $svcProvHttp->setPassword($this->_password); $svcProvHttp->setMethod('POST'); $svcProvHttp->setUploadFiles(array(array('name' => 'nzbfile', 'filename' => $nzb['filename'], 'mime' => $nzb['mimetype'], 'data' => $nzb['nzb']))); $output = $svcProvHttp->perform($url, null); $errorStr = 'sabnzbd push failed: ' . $output['errorstr']; if ($output['successful'] === false || strtolower(trim($output['data'])) != 'ok') { error_log($errorStr); throw new Exception($errorStr); } # if }
/** * * Execute a POST to the given url and return the body. * @param String $url * @param array $postdata * @return bool|mixed */ protected function postAndDownloadNzb($url, array $postdata) { // Initialize download retrieval class $svcHttp = new Services_Providers_Http($this->_cacheDao); $svcHttp->setPostContent($postdata); $svcHttp->setMethod('POST'); $result = $svcHttp->perform($url); // Check if any error occured if (!$result['successful']) { SpotDebug::msg(SpotDebug::DEBUG, __CLASS__ . '->postAndDownloadNzb(), not succesful=' . $result['errorstr']); return false; } # if // Load the body into simplexml. // If the xml is well formed this will result in true thus returning the xml. // Suppress errors if the string is not well formed, where testing here. if (@simplexml_load_string($result['data'])) { return $result['data']; } else { return false; } # else }
public function translateMultiple($dstLanguage, $list, $field) { /* * Try to obtain an translator token */ $translaterToken = $this->getAuthToken(); if ($translaterToken === false) { return false; } # if /* * Actually start translating our message */ $doc = new DOMDocument('1.0', 'utf-8'); $doc->formatOutput = true; $tar = $doc->createElement('TranslateArrayRequest'); $tar->appendChild($doc->createElement('AppId', '')); $tar->appendChild($doc->createElement('From', '')); $texts = $doc->createElement('Texts'); foreach ($list as $v) { $str = $doc->createElement('string'); $str->appendChild($doc->createTextNode($v[$field])); $str->setAttribute('xmlns', 'http://schemas.microsoft.com/2003/10/Serialization/Arrays'); $texts->appendChild($str); } # foreach $tar->appendChild($texts); $tar->appendChild($doc->createElement('To', $dstLanguage)); $doc->appendChild($tar); $svcPrvHttp = new Services_Providers_Http(null); $svcPrvHttp->setBearerAuth($translaterToken); $svcPrvHttp->setMethod('POST'); $svcPrvHttp->setRawPostData($doc->saveXML()); $svcPrvHttp->setContentType('text/xml'); $httpResult = $svcPrvHttp->perform(Services_Translation_Microsoft::translateUrl, false); /* * and use the result */ if ($httpResult['successful']) { /* * Loop through all results, and actually translate the * information */ $translated = simplexml_load_string($httpResult['data']); $listCounter = 0; foreach ($translated->TranslateArrayResponse as $tar) { $list[$listCounter][$field . '_translated'] = (string) $tar->TranslatedText; $listCounter++; } # foreach return $list; } else { return false; } # else }