从Artisan Command for Cron Job运行时,Curl返回零响应



尝试运行一个API,这将使我更新温度和湿度值,但是curl函数无法正常工作,因为它给出了无响应并引发错误。从终端运行测试

代码:

        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 PusherPusher(
                '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);
                        Apphelperlog::instance()->tempHumLog($s);
                    }
                }
            }
        }
    }

foreach循环引发了$响应等于null的错误。卷曲功能从这里不起作用,而是定期工作。帮助。我需要每小时运行此任务以获得平均温度和湿度。

,因为您说您的代码在某个地方工作,这意味着您的服务器有问题,而不是代码本身(因为这是卷曲的问题(。

在您的php.ini文件中,有此行:extension=php_curl.dll启用卷曲。在您的服务器上删除它,您有Crons可以使卷发起作用。这应该解决您的问题。

测试Localy您为我们提供的API返回500,但是如果您说它对您有用

最新更新