2012-10-13 45 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