【问题标题】:PHP Execute CURL inside Catch BlockPHP 在 Catch 块内执行 CURL
【发布时间】:2018-04-21 20:15:14
【问题描述】:

每当我的任何一个脚本中的 Artisan 命令失败时,我都会尝试将 CURL 发送到我的 Slack 频道(它每小时运行一次,所以我想确保我们知道是什么在晚上的任何时候导致了这些错误) ,但它不想发送 CURL。我已经确认其他事情有效,但它似乎现在不想发送它。

这是发送的代码,不多

} catch(\Throwable $e) {
        $message = "Found an error with the message " . $e->getMessage() . " , The function ResetBars did not finish.";
        Curl::to('https://hooks.slack.com/services/MYHOOK/RAWR/NOSEE')
        ->withContentType('application/json')->withData('{"text":"'.$message.'"}')->post();
      }

不知道为什么这不起作用,我使用的是 PHP Curl 库,但它的作用与常规 cURL 相同

我可以包含其余代码,但这并不重要,因为这是唯一不工作的部分

编辑:完整代码

public function handle() {
      //This function updates a bars hours after they're closed to the next day's hours
      //First we remove unwanted barDays
      try {
        $this->cleanBar();
        $this->cleanBarDays();
        $this->fixBarStats();
        $this->fixPromoStats();
        $this->cleanUserBars();
        $count = count(barDays::all()) + count(promoStats::where('timeLeft', NULL)->get()) + count(User::where('bar_id', '!=', NULL)->get()) + count(barStats::where('timeLeft', NULL)->get());
        $Bars = Bars::all();
        $this->output->progressStart(count($Bars));
        foreach($Bars as $Bar) {
          $now = Carbon::now()->setTimezone($Bar->timezone);
          $hours = $Bar->getHours();
          if($hours[0] === 'Closed') {
            $Bar->numPeople = 0;
            $Bar->hours = 'Closed';
            Bars::withoutSyncingToSearch(function() use($Bar) {
              $Bar->save();
            });
          } else {
            //Not closed, we check if the exploded hours followed the format
            $openHours = $hours[0];
            $closedHours = $hours[1];
            //Then we try to parse them, otherwise we catch the error to follow the culprit
            $open = Carbon::parse($openHours, $Bar->timezone);
            $closed = Carbon::parse($closedHours, $Bar->timezone);
            //If the bar is closed, we update the hours on the bar
            if(!$now->between($open, $closed)) {
              $Bar->updateHours();
              $Bar->numPeople = 0;
              //UPDATE BARSTATS HERE WITH BAR ID
              $barStats = barStats::where('bar_id', $Bar->id)->where('timeLeft', NULL)->get();
              foreach($barStats as $barStat) {
                $barStat->timeLeft = Carbon::now();
                $barStat->save();
              }
            } elseif($now->between($open, $closed)) {
              $Bar->updateHours();
            }
            Bars::withoutSyncingToSearch(function() use($Bar) {
              $Bar->save();
            });
          }
          $this->output->progressAdvance();
        }
        $this->output->progressFinish();
        Curl::to('https://hooks.slack.com/services/nuh uh/')
        ->withContentType('application/json')->withData('{"text":"Reset '.count($Bars).' Bars Hour\'s, '.$count.' number of miscellaneous models and reset number of people if closed."}')->post();
        return "Updated";
      } catch(Exception $e) {
        $message = "Found an error with the message " . $e->getMessage() . " , The function ResetBars did not finish.";
        Curl::to('https://hooks.slack.com/services/nope/')
        ->withContentType('application/json')->withData('{"text":"'.$message.'"}')->post();
      }
    }

忽略顶部的函数调用 谢谢

  • 扎克

【问题讨论】:

    标签: php laravel curl


    【解决方案1】:

    变化:

    catch(\Throwable $e) {
        $message = "Found an error with the message " . $e->getMessage() . " , The function ResetBars did not finish.";
        Curl::to('https://hooks.slack.com/services/MYHOOK/RAWR/NOSEE')
        ->withContentType('application/json')->withData('{"text":"'.$message.'"}')->post();
      }
    

    收件人:

    catch(Exception $e) {
        $message = "Found an error with the message " . $e->getMessage() . " , The function ResetBars did not finish.";
        Curl::to.....
      }
    

    【讨论】:

    • } catch(Exception $e) { $message = "Found an error with the message " . $e->getMessage() . " , The function ResetBars did not finish."; Curl::to('https://hooks.slack.com/services/myhook') ->withContentType('application/json')->withData('{"text":"'.$message.'"}')->post(); }
    • 打印里面的东西,验证一下,请。
    • 我也试过了,我试过$output->error('test');,它通过控制台没有问题
    • 哈哈,你的 try{}catch() 没有抛出异常。你的代码很好。 :)。
    • 是的,先生,这不是很有意义,但我会在主要问题中发布它
    猜你喜欢
    • 1970-01-01
    • 2012-02-19
    • 1970-01-01
    • 1970-01-01
    • 2017-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多