-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.php
34 lines (27 loc) · 904 Bytes
/
worker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
$worker = new GearmanWorker();
$worker->addServer('gearman');
$worker->addFunction('getTemp', 'getTemp');
print "Waiting for a job...\n";
while($worker->work()) {
if ($worker->returnCode() !== GEARMAN_SUCCESS) {
print 'Exiting with return code: ' . $gmworker->returnCode();
break;
}
}
function getTemp($job) {
$ch = curl_init();
$payload = json_decode($job->workload());
print "Getting current temperature for {$payload->city}...\n";
$city = urlencode($payload->city);
$api_key = $_ENV['API_KEY'] ?? false;
if (!$api_key) {
return 'No API key';
}
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => "http://api.openweathermap.org/data/2.5/weather?q=$city,us&appid=$api_key"
));
$result = json_decode(curl_exec($ch));
return round((($result->main->temp - 273.15) * 1.8) + 32);
}