[Original] integration of flex and PHP scripts (2)-amfphp

Source: Internet
Author: User

BenArticleFor the latest version, see: http://www.deepcast.net/wiki/ow.asp? Integration of flash and PHP

In the previous article flash integration PHP script (1) -- XML, we developed a small Adobe
Flex2 ApplicationProgramConnected to a PHP background program. If you have read the previous article, you may ask: Can they directly pass variables without using XML encoding as an intermediary? The answer is yes. This article will demonstrate examples in this regard.

Note that this article is written in flex2.

I. system requirements: 1. Flex builder 2 (including SDK)

    • Download the flex builder2 trial version
    • Purchase
2, amfphp 1.25:

    • Download amfphp
3. php (installed on a local Web Server)


2. Prerequisites

PHP knowledge about intermediate


Iii. MySQL and PHP

If you want to create a medium/large enterprise application, you can use Adobe Flex Data Service
2. It simplifies data interaction through efficient data transmission, information-based publishing and subscription, and other methods. Amfphp is just a small subset of it. If you work for a large enterprise, you will want to see its performance on the Data Interaction layer.

The key to completing this task is
Amfphp. This project was first created by Wolfgang
Launched by Hamann, the Team has grown to about five or six developers. Thanks to their hard work, the entire flexCommunityNow we have a front-end using PHP backend Based on flex.

This example shows how to display records from a database. It is only displayed and cannot be inserted or updated. The MySQL database name sample and table name users used in this article are consistent with the database structure used in the previous article. You only need to fill in some data for display first. Amfphp_flex.rar this is an SQL file for the database structure and input data.

The following is a PHP script.CodeThe file name isSample. phpPut it in the Services Directory of amfphp.

<? PHP
// Create new service for PHP remoting as Class
Class sample
{
Function sample ()
{
// Define the methodtable for this class in the constructor
$ This-> methodtable = array (
"Getusers" => array (
"Description" => "return a list of users ",
"Access" => "remote"
)
);
}

Function getusers (){
$ Mysql = mysql_connect (localhost, "username", "password ");

Mysql_select_db ("sample ");

// Return a list of all the users
$ Query = "select * from users ";
$ Result = mysql_query ($ query );
While ($ ROW = mysql_fetch_object ($ result )){
$ Arrayofusers [] = $ row;
}
Return ($ arrayofusers );
}
}
?>

If you are familiar with amfphp, you should be familiar with the above.

The class name must be consistent with the file name. If the above file name is smaple. php, the class name is sample. Because class files always call classes with the same name during loading to initialize themselves. The above example defines a valid method for amfphp, that is, getusers, which returns a list of users. In the database, retrieve all users and return them as an array of objects.

4. Application front-end

Now, create the foreground file sample. mxml, which has only 50 lines:

<! -- The first line is the top-level XML document definition -->
<? XML version = "1.0"
Encoding = "UTF-8"?>

<! -- The second line is the applicastion definition, andCreationcompleteTime callInitapplication () function-->

 
   
   










, which means that the DataGrid obtains data from the variable (the PHP Object array here.
up, -->
[Bindable]
Public var dataprovider: array;
flash.net. responder package for remote use. In fact, when you declare that you need a new
responder, flex will automatically include the package. This is only a special demonstration.
<! -- Then, create a variable,Gateway, It isThe remotingconnection data type. You already knowRemotingconnection. You should add a file named remotingconnection. As to the bottom of flex with a lot of code on it.
 
-->
 
Import flash.net. responder;

Public var Gateway: remotingconnection;

<! -- Called when the flash application is loaded Initapplication , Set the gateway variable to the connection path of gateway. php In amfphp. In this way, you can call the getusers method in the sample class. Then execute the command based on whether an error occurs. Onresulth or  Onfault One of the two functions. -->
Public Function initapplication ()
{
Gateway = new remotingconnection ("http: // localhost/flex/PHP/gateway. php ");
Gateway. Call ("sample. getusers", new Responder (onresult, onfault ));
}

<! -- The dataprovider variable is set as the result variable, and amfphp is used to send back the result to ActionScript. This is the PHP Object array that is returned when you run MySQL query ($ arrayofusers. Amfphp has automatically translated the PHP Object array into an ActionScript array. -->
Public Function onresult (Result: array): void
{
Dataprovider = result;
}

<! -- In case of an error, you can track the value of a variable just like the error message prompt provided to the user. It is particularly useful in debugging mode. -->
Public Function onfault (fault: string): void
{
Trace (fault );
}
]>
</MX: SCRIPT>
</MX: Application>

The above instance is a little complex, so let's clarify the location of the file again. Three files are created and downloaded from the amfphp package.

    • Put the flex project file, sample. mxml, and remotingconnection. As in the same directory.
    • Extract and release PHP files and folders, such as services, actions, adapters, app, and browser, from the amfphp file package.
    • Put all files in amfphp under the Web root directory.
    • Put sample. php In the services folder of the amfphp project.

Then, the connection between the PHP backend and the front-end small application instance created by Flex is over. The preceding code is downloaded in amfphp_flex.rar package.


For the latest version of this article, see: http://www.deepcast.net/wiki/ow.asp? Integration of flash and PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.