【问题标题】:Get Json and output to text file undecoded获取 Json 并输出到未解码的文本文件
【发布时间】:2011-03-04 15:10:46
【问题描述】:

我想获取 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  

【问题讨论】:

  • $out = (json_decode($file));我会将其更改为 $out = ($file);并删除 $report = wordwrap($out->mainText, 100, "\n"); ??

标签: php javascript json caching scripting


【解决方案1】:

试试这个:

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

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

【讨论】:

  • 我试过了,但无法将其输出到文本文件,不用担心,因为我找到了另一种解决方法,非常感谢
猜你喜欢
  • 2017-01-22
  • 1970-01-01
  • 2015-11-15
  • 1970-01-01
  • 2015-06-29
  • 1970-01-01
  • 2011-08-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多