获取Json并输出到未解码的文本文件

时间:2010-06-16 18:55:01

标签: php javascript json caching scripting

我想获取json脚本并将其写入未解码的txt文件,确切地说它是最初的。我确实有一个我正在修改的脚本但不确定要使用的命令。这个脚本解码,这是我想要的。

//Get Age  
 list($bstat,$bage,$bdata) = explode("\t",check_file('./advise/roadsnow.txt',60*2+15));  
//Test Age  
if ( $bage > $CacheMaxAge ) {  
  //echo "The if statement evaluated to true so get new file and reset $bage";  


    $bage="0";  
    $file = file_get_contents('http://somesite.jsontxt');  
    $out = (json_decode($file));  
    $report = wordwrap($out->mainText, 100, "\n");   
    //$valid = $out->validTo;  
    //write the data to a text file called roadsnow.txt  
    $myFile = "./advise/roadsnow.txt";  
    $fh = fopen($myFile, 'w') or die("can't open file");  
    $stringData = $report;  
    fwrite($fh, $stringData);  
}  
else {  
  //echo the test evaluated to false; file is not stale so read local cache  
  //print "we are at the read local cache";  
  $stringData = file_get_contents("./advise/roadsnow.txt");  
}  
// if/else is done carry on with processing  
//Format file  
$data = $stringData  

1 个答案:

答案 0 :(得分:0)

试试这个:

// Get JSON Data
$json_data = file_get_contents('http://somesite.jsontxt');

// Write JSON to File
file_put_contents('json_data.txt', $json_data);