7 Replies Latest reply on Nov 3, 2009 12:58 PM by BhargavaSrinarasi

    How to pass arguments to PHP from Flex?

    BhargavaSrinarasi

      Hi,

       

      I'm new to Flex and I want to pass some arguments to a PHP script that is running on a server.

      I've to send the user name, password and a file to it. I tested the PHP script with a simple HTML form and it works fine.

      How to do the same thing using Flex?

       

      Sending user name and password is fine. But the file? I'm stuck on that part.

       

      Any help is appreciated.

       

      Thanks,

        • 1. Re: How to pass arguments to PHP from Flex?
          Louder6Pounder Level 1

          You can use the URLrequest and URLVariables i believe.  Here is an example:

           

           

          var serverURL:String = serverURL;

           

           

           

          //Create new instance of URLrequest

          var request:URLRequest = new URLRequest(serverURL);

          request.method = URLRequestMethod.POST;

           

           

          //Add variables

          var variables:URLVariables = new URLVariables();  

           

           

          if(!overwrite){

               variables.overwrite = "true";

          }else{

               variables.overwrite = "false";

             }

               

          variables.fileType =  "jpeg";

          variables.imageByteArray = encodedByteArray;

          request.data = variables;

                     

          //use loader on URLrequest

          var loader:URLLoader = new URLLoader();

          loader.load(request); 

          loader.addEventListener(Event.COMPLETE, saveSuccessful);

          • 2. Re: How to pass arguments to PHP from Flex?
            BhargavaSrinarasi Level 1

            Hi, Thanks for the reply.

             

            But how to assign the file to 'variables'? I mean what should I assign? A file reference? or the path? I'm pasting my PHP code here to help you understand it better. I'm pretty confused. Hope you can help me.

             

                  <?php
                  $username = $_POST["userName"];
                  $password = $_POST["Password"];
                  $image = $_POST["image"];
                  $filename = $_FILES["image"]['name'];
                  $tmpfilename = $_FILES["image"]['tmp_name'];
                  $fileparts = explode(".", $filename);
                  $ext = $fileparts[count($fileparts) - 1];
                  $newfilename = substr($tmpfilename, 0, strlen($tmpfilename) - strlen($ext))  . $ext;

                  copy($tmpfilename, $newfilename);

                  // The twitter API address
                  $url = 'http://twitter.com/account/update_profile_background_image.xml';
                  $curl_handle = curl_init();
                  curl_setopt($curl_handle, CURLOPT_URL, "$url");
                  curl_setopt($curl_handle, CURLOPT_POST, 1);
                  curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array('Expect:'));
                  curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
                  curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
                  curl_setopt($curl_handle, CURLOPT_POSTFIELDS, array("image" => "@$newfilename"));
                  curl_setopt($curl_handle, CURLOPT_USERPWD, "$username:$password");
                  $buffer = curl_exec($curl_handle);
                  curl_close($curl_handle);
                         echo $buffer;
                  ?>

             

            Thanks again

            • 3. Re: How to pass arguments to PHP from Flex?
              camfieldaj Level 1

              You should use HTTPService.

               

                  <mx:HTTPService 
                      id="myHTTPService"
                      url="http://someserver/somePHPpage.php"
                      resultFormat="e4x"
                      result="myResultHandler(event);"
                      fault="myFaultHandler(event);"
                   <mx:request xmlns="">
                    <valuePassed>{"somevalue"}</valuePassed>
                   </mx:request>
                  />

              in the php page:

              if(isset($_REQUEST["valuePassed"]))
              {
              $localValue = $_REQUEST["valuePassed"];
              //$localValue will contain string literal somevalue.
               
              }
              • 4. Re: How to pass arguments to PHP from Flex?
                Mayank (Adobe) Adobe Employee

                Have you considered using RemoteObject to access PHP functions from your Flex application? Flash Builder 4 Beta 2 has data centric wizards which help you connect your PHP code to your Flex application.

                 

                This link has some tutorials and videos which can help you get started with PHP & Flex.

                http://sujitreddyg.wordpress.com/flash-builder-4/

                 


                Mayank Kumar

                Computer Scientist, Flash Builder

                • 5. Re: How to pass arguments to PHP from Flex?
                  BhargavaSrinarasi Level 1

                  Hi camfieldaj,

                   

                  My question is what will be the 'somevalue' when I want to send a file? What should I use?

                  • 6. Re: How to pass arguments to PHP from Flex?
                    camfieldaj Level 1

                    Gotcha. Ok, to upload a file to a php script I use the FileReference class.

                     

                    public var fileref:FileReference = new FileReference();

                     

                    in the function that's called when you allow them to upload

                     

                    var rptTypes:FileFilter = new FileFilter("ZIP Files", "*.zip");

                    var allowed:Array = new Array(rptTypes);

                     

                    fileref.browse(allowed);

                     

                    You have to declare a urlRequest to point at your php file.

                     

                    fileref.upload(urlRequest);

                     

                    On the php side:

                     

                    if(isset($_FILES['Filedata']['name']))
                    {
                    $real_filename=$_FILES['Filedata']['name'];

                     

                    There's a little more to it, but there are some good examples in the

                    help files and the php method of dealing with uploads is well

                    documented.

                     

                    Hope this helps.

                    • 7. Re: How to pass arguments to PHP from Flex?
                      BhargavaSrinarasi Level 1

                      I got it working with something similar to what you said. Thanks very much!!

                       

                      But it's not working when I run it on standalone swf. Do you know any workaround?

                       

                      Thanks again!