/** * @param string|null $expectedResponse the response type which expected * @param bool $useSession should the request use an automatic session id? * @param bool $secondFail is this the second try of this request? * @return \SimpleXMLElement * @throws \Exception */ public function send($expectedResponse = null, $useSession = true, $try = 1) { $this->request['RequestId'] = '33300000-2200-1000-0000-' . substr(md5(uniqid()), 0, 12); $this->request['Version'] = $this->smartHome->getVersion(); if ($useSession) { $this->request['SessionId'] = $this->smartHome->getSessionId(); } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://' . $this->smartHome->getHost() . '/' . $this->action); if (($clientId = $this->smartHome->getClientId()) !== null || $this->action === 'upd') { if ($clientId === null && $this->action === 'upd') { throw new \Exception('Unable to get updates if no client id is specified!', 107); } curl_setopt($ch, CURLOPT_HTTPHEADER, array('ClientId: ' . $clientId)); } curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_SSLVERSION, 3); curl_setopt($ch, CURLOPT_POSTFIELDS, $this->action === 'upd' ? 'upd' : $this->request->asXML()); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); list($header, $body) = explode("\r\n\r\n", curl_exec($ch), 2); $this->setResponse($body); $this->setResponseHeader($header); curl_close($ch); /** * Fix the problems */ $responseType = (string) $this->getResponse()->attributes('xsi', true)->type; if ($expectedResponse !== null && strtolower($responseType) !== strtolower($expectedResponse)) { if ($try > 1) { throw new \Exception('Request failed second time. Error: ' . $responseType, 99); } if ($responseType === 'GenericSHCErrorResponse' || $responseType === 'AuthenticationErrorResponse') { $this->smartHome->login(++$try); return $this->send($expectedResponse, $useSession, $try); } else { $try--; } if ($responseType === 'VersionMismatchErrorResponse') { $this->smartHome->setVersion((string) $this->getResponse()->attributes()->ExpectedVersion); return $this->send($expectedResponse, $useSession, ++$try); } else { $try--; } } return $this->getResponse(); }
*/ use Bubelbub\SmartHomePHP\SmartHome; use Bubelbub\SmartHomePHP\Entity\LogicalDevice; require_once 'vendor/autoload.php'; $newLine = php_sapi_name() == 'cli' ? PHP_EOL : '<br />'; $config = new SimpleXMLElement('<SmartHomeConfiguration />'); $configFile = __FILE__ . '.config'; if (file_exists($configFile)) { try { $config = new SimpleXMLElement($configFile, 0, true); } catch (Exception $ex) { } } $sh = new SmartHome('Hostname or IP address', 'Username', 'Password'); if (!file_exists($configFile)) { $config->addChild('SessionId', $sh->getSessionId()); $config->addChild('ClientId', $sh->getClientId()); $config->addChild('Version', $sh->getVersion()); $config->addChild('ConfigurationVersion', $sh->getConfigVersion()); $config->saveXML($configFile); } $sh->setSessionId((string) $config->SessionId); $sh->setClientId((string) $config->ClientId); $sh->setVersion((string) $config->Version); $sh->setConfigVersion((string) $config->ConfigurationVersion); // get your current session id echo 'Your session id is ' . $sh->getSessionId() . $newLine; // get your current session id echo 'Your current client id is ' . $sh->getClientId() . $newLine; // get your current version echo 'Your current version is ' . $sh->getVersion() . $newLine;