Flash -> JSON -> PHP


/ Published in: ActionScript 3
Save to your folder(s)

Uses JSON Lite SWC/Class from http://thanksmister.com/?p=40


Copy this code and paste it in your HTML
  1. // Create JSON Object to send to PHP
  2. import flash.net.URLRequest;
  3. import flash.net.URLLoader;
  4. import com.serialization.json.JSON;
  5.  
  6. //var obj:Object = JSON.deserialize(string); // Use this to deserialize data coming back!
  7.  
  8.  
  9. var people:Array = new Array();
  10. var person:Object = new Object();
  11. person.firstname = "Kobe";
  12. person.lastname = "Bryant";
  13. people.push(person);
  14.  
  15. var url:String = "http://localhost/getJSON.php";
  16. var request:URLRequest = new URLRequest(url);
  17. request.method = URLRequestMethod.POST;
  18.  
  19. var requestVars:URLVariables = new URLVariables();
  20. requestVars.myObject = JSON.serialize(people);
  21. request.data = requestVars;
  22.  
  23. var loader:URLLoader = new URLLoader();
  24. loader.load(request);
  25.  
  26. /* PHP TO CATCH THE JSON
  27.  
  28. $object = JSON_decode($_POST['myObject']);
  29.  
  30. // Takes JSON data and writes to data.txt
  31. $fp = fopen('data.txt', 'w');
  32. fwrite($fp, $_POST['abc']);
  33. fwrite($fp, $object[0]->firstname . " ");
  34. fwrite($fp, $object[0]->lastname . chr(13) . chr(10));
  35. fwrite($fp, $_POST['myObject']);
  36. fclose($fp);
  37.  
  38.  
  39. */

URL: http://www.destroyyourcomputer.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.