【问题标题】:Curl returning null response when run from artisan command for cron job从 cron 作业的工匠命令运行时,Curl 返回空响应
【发布时间】: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


【解决方案1】:

既然你说你的代码在某处工作,这意味着你的服务器有问题,而不是代码本身可能有问题(因为这是一个 CURL 问题)。

在您的 php.ini 文件中有这一行:extension=php_curl.dll 以启用 curl。在您的服务器上取消注释您拥有使 curl 工作的 crons。这应该可以解决您的问题。

在本地测试您提供给我们的 api 返回 500,但如果您说它对您有用,我认为这只是一个示例,问题出在 CURL 本身。

【讨论】:

  • 问题不在代码中。雄辩的映射由于某种未知原因返回垃圾值。但是感谢您的帮助!
  • 是的,这也是我提到的,因为 api 返回 500,所以问题不在代码本身。很高兴听到您的问题已解决!!!
猜你喜欢
  • 2013-07-24
  • 2012-04-10
  • 1970-01-01
  • 1970-01-01
  • 2017-04-20
  • 2016-07-03
  • 2021-09-15
  • 1970-01-01
  • 2015-11-15
相关资源
最近更新 更多