为什么(条件)不能与'{“”}''

时间:2019-02-05 14:44:37

标签: php if-statement

我正在设置一个PHP代码,该代码从Bash获取一些信息并将其放入PHP。我的If陈述不排序我从bash编写或得到的内容。

这是针对Linux上的服务器Apache2。我试图将要在条件中使用的字符串放在变量中。

GameToken = `grep "Matching server game" "${FactorioServerLogPath}" 2> /dev/null | awk '{print $7}' | tail -1 | tail -c +2 | head -c -2`;

$GameState = `curl -s -X GET -H "Content-type: application/json" -H "Accept: application/json" "https://multiplayer.factorio.com/get-game-details/$GameToken"`;

if ( $GameState != '{"message":"no game for given game_id"}' ) {
  echo "<h2> The Server is up and running </h2>";
} else {
  echo "<h2> The Server is currently turned off </h2>";
}

当变量$ GameState不像{“ message”:“给定game_id没有游戏”时,支出应该看起来像是服务器已关闭。

1 个答案:

答案 0 :(得分:3)

也许您应该先对其进行解码,然后尝试检查是否满足条件:

例如:

def verify(code: String): Action[AnyContent] = Action.async { 
  implicit request =>
    weatherService.getWeather().map {
      case Success(weatherUpdate) => Ok(weather_template(weatherUpdate))
      case Failure(ex) => BadRequest(weather_error())
    }
}

或者做起来更好:

$arr = json_decode($GameState, true);

if(array_key_exists('message', $arr) && $arr['message'] == 'no game for given game_id'){
     //do what you want
}