本文整理汇总了PHP中Phue\Client::getHost方法的典型用法代码示例。如果您正苦于以下问题:PHP Client::getHost方法的具体用法?PHP Client::getHost怎么用?PHP Client::getHost使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Phue\Client
的用法示例。
在下文中一共展示了Client::getHost方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: sendRequest
/**
* Send request
*
* @param string $address API address
* @param string $method Request method
* @param \stdClass $body Post body
*
* @return string Request response
* @throws Exception\ConnectionException
* @throws \Exception
*/
public function sendRequest($address, $method = self::METHOD_GET, \stdClass $body = null)
{
// Build request url
$url = "http://{$this->client->getHost()}/api/{$address}";
// Open connection
$this->getAdapter()->open();
// Send and get response
$results = $this->getAdapter()->send($url, $method, $body ? json_encode($body) : null);
$status = $this->getAdapter()->getHttpStatusCode();
$contentType = $this->getAdapter()->getContentType();
// Close connection
$this->getAdapter()->close();
// Throw connection exception if status code isn't 200 or wrong content type
if ($status != 200 || $contentType != 'application/json') {
throw new ConnectionException('Connection failure');
}
// Parse json results
$jsonResults = json_decode($results);
// Get first element in array if it's an array response
if (is_array($jsonResults)) {
$jsonResults = $jsonResults[0];
}
// Get error type
if (isset($jsonResults->error)) {
throw $this->getExceptionByType($jsonResults->error->type, $jsonResults->error->description);
}
// Get success object only if available
if (isset($jsonResults->success)) {
$jsonResults = $jsonResults->success;
}
return $jsonResults;
}
示例2: getJsonResponse
/**
* Send request
*
* @param string $address
* API address
* @param string $method
* Request method
* @param \stdClass $body
* Post body
*
* @return \stdClass Json body
*/
protected function getJsonResponse($address, $method = self::METHOD_GET, \stdClass $body = null)
{
// Build request url
$url = "http://{$this->client->getHost()}{$address}";
// Open connection
$this->getAdapter()->open();
// Send and get response
$results = $this->getAdapter()->send($url, $method, $body ? json_encode($body) : null);
$status = $this->getAdapter()->getHttpStatusCode();
$contentType = $this->getAdapter()->getContentType();
// Throw connection exception if status code isn't 200 or wrong content type
if ($status != 200 || $contentType != 'application/json') {
throw new ConnectionException('Connection failure');
}
// Parse json results
$jsonResults = json_decode($results);
return $jsonResults;
}
示例3: execute
protected function execute(InputInterface $input, OutputInterface $output)
{
$ip = $input->getArgument('ip');
$username = $input->getArgument('username');
if ($ip) {
$ipAddress = $ip;
} else {
$output->writeln('Finding your Bridge...');
$bridges = json_decode(file_get_contents('https://www.meethue.com/api/nupnp'));
/**
* No bridges - stop.
*/
if (!$bridges) {
$output->writeln('No bridges found - sorry!');
}
/**
* One bridge, grab IP.
*/
if (count($bridges) == 1) {
$ipAddress = $bridges[0]->internalipaddress;
}
/**
* Multiple bridges? Ask for IP.
*/
if (count($bridges) > 1) {
$choices = [];
foreach ($bridges as $bridge) {
$choices[] = $bridge->internalipaddress;
}
$helper = $this->getHelper('question');
$question = new ChoiceQuestion('We found multiple bridges. Which one do you want to connect to?', $choices, 0);
$question->setErrorMessage('Choice %s is invalid.');
$choice = $helper->ask($input, $output, $question);
$ipAddress = $choice;
$output->writeln('You have just selected: ' . $choice);
}
}
if ($input->getOption('yell')) {
//
}
/**
* Username.
*/
$user = $username ? $username : get_current_user();
$user = $user . substr(md5(microtime()), rand(0, 26), 10);
/**
* Authenticate with Bridge.
*/
$client = new Client($ipAddress, $user);
$output->writeln("Testing connection to bridge at {$client->getHost()}");
try {
$client->sendCommand(new Ping());
} catch (ConnectionException $e) {
$output->writeln("Issue connecting to bridge - " . $e->getMessage());
exit(1);
}
$output->writeln("Attempting to create user: " . $user . "...");
$output->writeln("Press the Bridge's button! Waiting...");
$maxTries = 30;
for ($i = 1; $i <= $maxTries; ++$i) {
try {
$response = $client->sendCommand(new CreateUser($user));
$output->write("\n");
$output->writeln("Successfully created new user: " . $response->username);
break;
} catch (LinkButtonException $e) {
$output->write(".");
} catch (Exception $e) {
$output->writeln("Failure to create user. Please try again!");
$output->writeln("Reason: " . $e->getMessage());
break;
}
sleep(1);
}
/**
* Save to memory.
* @todo Would prefer a better way of doing this, if you can think of one.
*/
if ($ipAddress && $user) {
file_put_contents("config.txt", json_encode(['ip' => $ipAddress, 'username' => $user]));
}
$output->writeln("Let there be light! Use `hue list` to see available commands.");
}