2012-10-13 43 views
0

我的Flash影片读取并发送数据到免费服务器中的PHP文件。如果Flash以这种方式写入文本文件(由PHP文件管理)来读取变量值,那么看起来确实如此:& variable = value &,我没有问题。 但我的PHP文件,预处理(通过一些数学函数)由Flash发送的数据,然后更新文本文件中的值,这是我的意图,但我无法完成。 假设我想更新计数器(它计数多少次对数据进行更新): 在文本文件中我有&counter=0&(初始值),如果我把PHP文件:FLASH AS2和PHP变量

<?php 
$fp = fopen("jose_stats.txt", "r");// I guess with it, I've read all the variables and values 
// one of them is the variable &counter. 
fclose($fp); 

$toSave = "&counter=&counter+1&\n"; 

$fp = fopen("jose_stats.txt", "w"); 
if(fwrite($fp, "$toSave")) { 
    echo "&verify=success&"; //prints to screen &verify=success which flash will read 
          //and store as myVars.verify 
    } else { // simple if statement 
     echo "&verify=fail&"; //prints to screen &verify=fail which flash will read and 
          //store as myVars.verify 
    } 
    fclose($fp); 

?> 

但随后,我检查我的文本文件,它有&counter=&counter+1&线:(而不是预期的&counter =1&。 请给我和建议,谢谢。

回答

1

为什么不使用JSON?

只是存储在JSON格式的数据:

$count = 1; 

$toWrite = array('count' => $count);//put other data into this array if you want 

//encode it 
$toWrite = json_encode($toWrite); 

//and now write the data 

在Flash中对其进行解码,导入JSON类:

使用JSON.as类,AS2 JSON的例子:

try { 
    var o:Object = JSON.parse(jsonStr); 
    var s:String = JSON.stringify(obj); 
} catch(ex) { 
    trace(ex.name + ":" + ex.message + ":" + ex.at + ":" + ex.text); 
} 

只需要导入class,并运行JSON.parse(yourPhpResponse);

此外,为什么你在文本文件中看到&counter=&的原因是因为你正在存储它:$toSave = "&counter=&counter+1&\n";

+0

好吧,我会考虑JSON。但我仍然在问自己是否可以使用php代码从文本文件读取变量值。我想知道它是否存在。 – JoeCoolman

+0

这是可能的,但它会是一个正确的痛苦。这是JSON的设计目的。将数据转换为大多数语言可以轻松理解的格式。 – 2012-10-13 22:35:23

+0

我遵循你的建议,一切运行正常,只剩下一个问题:'json_encode($ toWrite)'语句使得json对象内容的水平线非常长,当数组不存在任何问题时很长?换句话说,txt或json文件的水平方向是否有任何限制?顺便说一下,我想在这里留下一个很好的代码示例链接,以便从Flash AS2中读取json内容:http://thinkflash.blogspot.com.ar/2009/05/study.html – JoeCoolman