【发布时间】:2019-09-28 01:13:04
【问题描述】:
尝试运行一个 API,它将为我提供更新的温度和湿度值,但 curl 函数不起作用,因为它给出了 NULL 响应并引发错误。从终端运行来测试它
代码:
class updateTempHumHourly extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'update:temphum';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Update temperature and humidity readings hourly';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$options = array(
'cluster' => 'ap2',
'useTLS' => true
);
$pusher = new \Pusher\Pusher(
'osdifnerwion4iownfowinf',
'dofhewofhewofnowenfid',
'7asdkland',
$options
);
$thinkers = t::where('user_id', '!=' , NULL)->where('thinker_status',1)->get();
foreach($thinkers as $t)
{
$temp = [
'action'=>'list_slave',
'mac'=> $t->thinker_MAC,
'type' => 'all',
'appkey' => 'nope'
];
json_encode($temp);
$response = Curl::to('http://103.31.82.46/open/open.php')
->withContentType('application/json')
->withData($temp)
->asJson(true)
->withHeader('Postman-Token: d5988618-676e-430c-808e-7e2f6cec88fc')
->withHeader('cache-control: no-cache')
->post();
foreach($response['slaves'] as $s)
{
if(array_key_exists("temp",$s) && array_key_exists("hum",$s))
{
$slave = sd::where("connected_thinker_MAC",$response['mac'])->where("device_id",$s['slave_id'])->first();
$slave->temperature = $s['temp'];
$slave->humidity = $s['hum'];
$slave->save();
$array = [];
$array['temperature'] = $s['temp'];
$array['humidity'] = $s['hum'];
$array['id'] = $s['slave_id'];
$pusher->trigger($t->user_id."-channel","s-event",$array);
\App\helper\log::instance()->tempHumLog($s);
}
}
}
}
}
foreach 循环抛出 $response 等于 null 的错误。 curl 功能在此处无法正常工作,但可以正常工作。帮助。我需要每小时运行一次这个任务来获得平均温度和湿度。
【问题讨论】:
-
所以你的代码可以在浏览器上运行,但不能从命令行运行?
-
似乎 API 响应 HTTP 500 错误,至少在我这边。
标签: laravel cron laravel-artisan