public function &sendRequest($url, $checkResultTag = false, &$filecontent = null, $filename = '') { $url = str_replace('#', '%23', $url); if (isset($this->serial) && !is_null($this->serial)) { $url = 'https://' . $this->apihost . '/api/?key=' . $this->apikey . '&target=' . $this->serial . '&' . $url; } else { $url = 'https://' . $this->apihost . '/api/?key=' . $this->apikey . '&' . $url; } if ($this->showApiCalls) { print "API call: \"" . $url . "\"\r\n"; } $c = new mycurl($url); if (!is_null($filecontent)) { $c->setInfile($filecontent, $filename); } if (!$c->createCurl()) { derr('Could not retrieve URL: ' . $url . ' because of the following error: ' . $c->last_error); } $xmlobj = new XmlArray(); if ($c->getHttpStatus() != 200) { derr('HTTP API ret: ' . $c->__tostring()); } $xmlarr = $xmlobj->load_string($c->__tostring()); if (!is_array($xmlarr)) { derr('API didnt return a XML string: ' . $c->__tostring()); } if (!isset($xmlarr['attributes']['status'])) { derr('XML response has no "status" field: ' . array_to_xml($xmlarr)); } if ($xmlarr['attributes']['status'] != 'success') { derr('API reported a failure: ' . $c->__tostring()); } if (!is_null($filecontent)) { return $xmlarr['children']; } if (!$checkResultTag) { return $xmlarr['children']; } //print_r( $xmlarr['children'] ); $cursor =& searchForName('name', 'result', $xmlarr['children']); if (is_null($cursor)) { derr('XML API response has no <result> field:' . $c->__tostring()); } if (is_null($cursor['children'])) { derr('XML API <result> has no content'); } return $cursor['children']; }
/** * @param string $parameters * @param bool $checkResultTag * @param string|null $filecontent * @param string $filename * @param Array $moreOptions * @return DomDocument */ public function sendRequest(&$parameters, $checkResultTag = false, &$filecontent = null, $filename = '', $moreOptions = array()) { $sendThroughPost = false; if (is_array($parameters)) { $sendThroughPost = true; } $host = $this->apihost; if ($this->port != 443) { $host .= ':' . $this->port; } if (isset($this->serial) && !is_null($this->serial)) { $finalUrl = 'https://' . $host . '/api/'; if (!$sendThroughPost) { $finalUrl .= '?key=' . $this->apikey . '&target=' . $this->serial; } } else { $finalUrl = 'https://' . $host . '/api/'; if (!$sendThroughPost) { $finalUrl .= '?key=' . $this->apikey; } } if (!$sendThroughPost) { $url = str_replace('#', '%23', $parameters); $finalUrl .= '&' . $parameters; } if (isset($moreOptions['timeout'])) { $timeout = $moreOptions['timeout']; } else { $timeout = 7; } $c = new mycurl($finalUrl, false, $timeout); if (array_key_exists('lowSpeedTime', $moreOptions)) { $c->_lowspeedtime = $moreOptions['lowSpeedTime']; } if (!is_null($filecontent)) { $c->setInfile($filecontent, $filename); } if ($sendThroughPost) { if (isset($this->serial) && !is_null($this->serial)) { $parameters['target'] = $this->serial; } $parameters['key'] = $this->apikey; $properParams = http_build_query($parameters); $c->setPost($properParams); } if ($this->showApiCalls) { if ($sendThroughPost) { $paramURl = '?'; foreach ($parameters as $paramIndex => &$param) { $paramURl .= '&' . $paramIndex . '=' . str_replace('#', '%23', $param); } print "API call through POST: \"" . $finalUrl . '?' . $paramURl . "\"\r\n"; } else { print "API call: \"" . $finalUrl . "\"\r\n"; } } if (!$c->createCurl()) { derr('Could not retrieve URL: ' . $finalUrl . ' because of the following error: ' . $c->last_error); } if ($c->getHttpStatus() != 200) { derr('HTTP API ret: ' . $c->__tostring()); } $xmlDoc = new DOMDocument(); if (!$xmlDoc->loadXML($c->__tostring(), LIBXML_PARSEHUGE)) { derr('Invalid xml input :' . $c->__tostring()); } $firstElement = DH::firstChildElement($xmlDoc); if ($firstElement === false) { derr('cannot find any child Element in xml'); } $statusAttr = DH::findAttribute('status', $firstElement); if ($statusAttr === false) { derr('XML response has no "status" field: ' . DH::dom_to_xml($firstElement)); } if ($statusAttr != 'success') { var_dump($statusAttr); derr('API reported a failure: "' . $statusAttr . "\"with the following addition infos: " . $firstElement->nodeValue); } if (!is_null($filecontent)) { return $xmlDoc; } if (!$checkResultTag) { return $xmlDoc; } //$cursor = &searchForName('name', 'result', $xmlarr['children']); $cursor = DH::findFirstElement('result', $firstElement); if ($cursor === false) { derr('XML API response has no <result> field', $xmlDoc); } DH::makeElementAsRoot($cursor, $xmlDoc); return $xmlDoc; }