PHP在Catch Block中执行CURL

时间:2018-04-21 20:15:15

标签: php laravel curl

当我的任何一个脚本中的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();
      }
    }

忽略顶部的函数调用 感谢

  • 扎克

1 个答案:

答案 0 :(得分:0)

更改:

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.....
  }