2013-07-10 96 views
0

所以我想讀取一個JSON文件在一個單獨的網址和基於什麼是JSON文件,我想讓程序做些什麼。但目前我試圖做到這一點的方式不起作用。使用包含密鑰與file_get_contents

<?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 

當我搜索了「的file_get_contents」下的PHP手冊它說,它應該將整個文件讀入一個字符串,所以我有點困惑爲什麼我得到一個字符串錯誤。

謝謝〜

回答

0

在 '包含' 代替,看到strpos功能。此外,您必須用單引號關閉單引號,用雙引號括起雙引號。

$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 library

要獲取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']);