0

所以我试图在一个单独的 url 上读取一个 JSON 文件,并根据我想让程序做某事的 JSON 文件中的内容。但是我目前尝试这样做的方式不起作用。

<?php
$homepage = file_get_contents('http://direct.cyberkitsune.net/canibuycubeworld/status.json');
//echo $homepage;
if($homepage contains 'f'){
echo 'true';
}else{
echo 'Something went terribly wrong!";
}
?>

这是我得到的错误

Parse error: syntax error, unexpected T_STRING in /home/a1285224/public_html/cubeworld.php on line 4

当我在 php 手册下搜索“file_get_contents”时,它说它应该将整个文件读入一个字符串,所以我有点困惑为什么我会收到一个字符串错误。

谢谢~

4

1 回答 1

0

代替“包含”,请参见strpos函数。此外,您必须用单引号关闭单引号,用双引号关闭双引号。

IE

$homepage = file_get_contents("http://direct.cyberkitsune.net/canibuycubeworld/status.json");

// echo $homepage;

if (strpos($homepage, "f") !== false)
    {
      echo "true";
    }
else
    {
      echo "Somethingwentterriblywrong!";
    }

根据您要执行的操作,另请参阅 PHP 的JSON 库

要获取 JSON 数据,可以使用 json_decode。

转储所有变量:

$json = json_decode($homepage, true);
var_dump($json);

要获取所有其他数据:

printf("Time: %s<br>Site: %s<br>Reg: %s<br>Shop: %s", $json['time'], $json['site'], $json['reg'], $json['shop']);
于 2013-11-28T21:57:30.527 回答