DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

PHP: Connecting Flash To A Database (remoting)

12.27.2006
| 7745 views |
  • submit to reddit
        There are many ways to do this.

Depending on the backend language you're using, whether PHP, ColdFusion or other... you'll need to create some components (CFC's for ColdFusion), (Classes for PHP)... these are referred to as "services"

Then you'll need to connect Flash to a remoting gateway (I use AMFPHP for PHP: www.amfphp.org).

Either way, you'll need NetServices.as from Flash MX Remoting, call a gateway and retrieving a list of functions from your classes/components/services.

Enough with the theory, here's how to do it with PHP:
/* /flashservices/services/Catalog.php */
class Catalog {
        var $products_array = array();

// Constructor: Contains the list of methods available to the gateway
function Catalog() {
	$this->methodTable = array (
		"getProducts" => array (
			"description" => "Get list of products",
			"access" => "remote",
			"arguments" => "" // arguments could be optional, not tested
		)
	); // end methodTable
}

function getProducts() {	
	// your code goes here

	return $this->products_array;
}
}

!!!!!!!!! The code below is now deprecated !!!!!!!!
---- see my other snippet to do this in Flash 8 ----

Flash ActionScript (PHP Gateway):
#include "NetServices.as"
NetServices.setDefaultGatewayUrl("http://yourserver.com/flashservices/gateway.php");
gw = NetServices.createGatewayConnection();
CatalogREMOTE = gw.getService("Catalog", this);
CatalogREMOTE.getProducts();

getProducts_Result = function(result) {
	_root.products_results = result;
}

You parse the _root.products_results array however you want :D
    

Comments

Snippets Manager replied on Tue, 2006/12/19 - 10:12am

If you're having problems you might want to include: #include "NetDebug.as" in your ActionScript code to help debugging ... also I strongly suggest to download and install Flash MX Remoting Components (components only... gives you a nice GUI for debugging etc.) Also, your services (Catalog.php) should go in the /flashservices/services/ directory.