Passing a string to Flash from PHP - php

how can I pass a string from PHP to Flash?
I need to pass this to flash,
$var = 'uid_'.$uid.'_'.'likes_'.$likes;
Any ideas on how I can accomplish this?
Thanx in advance!

So, your best bet in this case is going to be one of two things:
1) If this is a simple easy solution with a little bit of data that isn't going to see a whole ton of traffic, just have your PHP output an XML file and use Flash's URLLoader() to load in that .xml data - then parse it.
Alternately,
2) If this is going to see some heavy traffic, or if you want to do it the "right" way, look into either ZendAMF or AMFPHP. Lee Brimelow has tutorials for working with this stuff at gotoandlearn.com - basically, you can remote into a PHP Web Service which will return data (not just strings - you can even do typed objects!) as binary data directly into your Flash file.
Either way you're not going to have too much trouble with it - it's a pretty straightforward operation. Let me know if you have any questions.

The simplest way to achieve is to use the well-named flashvars to pass it.
Adobe as a KB about flash HTML parameters http://kb2.adobe.com/cps/127/tn_12701.html
Javascript based flash integration libraries (like swfObject) allow to pass any of these parameters as well.

Related

Rendering a page in PHP: How?

This may be a inappropriate question for SO, but I thought lets see :)
I'm writing a website in php. Every pageload may have 10-20 DB requests.
Using the result of the DB queries I need to generate a page.
The page would contain a topic (should be image or text) followed by comments. There could be mutiple topics like this.
Currently, I'm creating a string using the DB result and sending it to the browser.
When browser receives the string (as an ajax response), it parses using split functions and creates the HTML dynamically.
I'm basically a C++ programmer; relatively new to web development. So, I do not have fair understanding of the JS objects. How long of a string can JS variable hold? Is it ok to use split and generate HTML at the client.
I'm not generating the complete HTML at the server side to avoid any overhead because of string concatenation. I believe sending less no. of characters to the client (like I'm doing) is better as compared to sending complete HTML code.
Is something (or everything) wrong in my understanding :)
Any help is appreciated.
EDIT:
Well, I'll be highly grateful if I could get opinions in yes/no. What would you recommend. Sending HTML to the client or a string that will be used at the client to generate HTML?
Unless you have a specific reason for doing so, I think you should look into generating the HTML with PHP and sending it directly to the browser. PHP was built specifically for this purpose.
I think you be best off to look at jQuery and more specific to the AJAX method of that library. Also, take a look at JSON and you should be all good to go.
Have you considered using a templating engine like Smarty?
It's pretty simple to use, take a look at the crash course, you might like it! http://www.smarty.net/crash_course

Passing of Web Scraping Data

I'm currently writing an application that will extract data from a few different websites to be passed back to my app, parsed, formatted, and displayed. The problem I keep running into is being able to pass in and display the data in a graphical manner. I was hoping to use HTML5 to do this, and all of my scraping is set up in php. Of course, to draw in HTML5 requires using JavaScript, and getting my php output to JavaScript seems messy. Am I missing a better way to architect this solution?
It seems like a good way to me, as good as any, except it's not very backwards compatible, it might be better to do the graphing server side
If you want to do graphics directly in php you may want to look at using GD or ImageMagick.

Calling PHP from Rails with input and output

I am wanting to call a RAILS script within my php code base to avoid having to duplicate complicated business logic. I see that in PHP I can use passthru() but I'm not sure that is the best bet when returning more than a string back to the PHP function. I need to return back a hash/object of key/value pairs.
I'll be using script/runner on the RAILS side so that I don't have to change anything in the RAILS code. I was hoping there would be a simpler way to do this but I'm finding very little documentation on the webs that relates to this. Thanks in advanced.
Peace,
James
You can make your Rails script output a CSV data and then use PHP's fgetcsv to parse it into array. Of course, you'll have to modify your Rails code to output CSV, but it doesn't sound like a big change - just a couple of lines to convert your data into CSV. FasterCSV gem could help with that.

Can I execute js files via php?

The situation is next:
I have php file, which parses a web-page. on that web-page is a phone number and it's digits are mixed with each other. The only way to put each digit on the correct place is to use some JS functions (on the client side). So, when I execute that php file in linux console, it gives me all that I need, except js function's result (no wonder - JavaScript is not a server-side language). So all I see from JS - only a code, that I have written.
The question: can I execute js files via php and how?
Results of a quick google search (terms = javascript engine php)
J4P5 -- not developed since 2005 [BAD](according to its News)
PECL package spidermonkey
a 2008 post by jeresig points to PHPJS but can't see when it was last updated.
I'm sure you'll find many more links on that google search.
Alternatively:
you say that the digits are scrambled and you need to "unscramble" them using js. Can you code that unscrambling logic into a PHP function and just use it? Will sure save you a lot of trouble, but if learning to use js in php is what you're after, then its a whole different story...
Zend tutorial: "Using javascript in PHP with PECL and spidermonkey"?
http://en.wikipedia.org/wiki/Comparison_of_Server-side_JavaScript_solutions
Alternatively, PHP has simple functions for executing other programs and retrieving their output (I used this along with GPG once to create a PHP file manager which could live-encrypt files as you were uploading them and live-decrypt as you were downloading them)
Using those functions along with http://code.google.com/p/v8/ you should be able to interpret any javascript.
Not unless you know someone who's implemented a Javascript engine in PHP.
In other words, probably not.
Without some sort of browser emulation or passing the unparsed js off to a server side implementation of javascript (maybe node.js?), you won't be able to execute it.
However, does the page use the same js function to unscramble the phone number every time? You should be able to read the incorrect digits and shuffle them with PHP.
If you're prepared to do a bit of work building your own JS runtime to work with it, Tim Whitlock has written a javascript tokenizer and parser in pure PHP
node.js is server-side... but full JS :) no PHP in it so I don't it answer your needs...
Anyway, here is an example : a chat in JS both client & server-side : http://chat.nodejs.org/
Plus, not every host allows you to use the v8 engine...
If you have Javascript data objects, and you need to convert them to/from PHP arrays, that's quite easy using PHP's json_encode() and json_decode() functions.
But actually running Javascript code? No. You can't. You might be able to find a JS interpreter written in PHP (a few other answers have pointed a links that may or may not help you here), or more likely execute the JS using a stand-alone JS interpreter on your server which you call out to from PHP. However if the JS code includes references to the browser's DOM (which is highly likely), that's a whole other set of issues which will almost certainly make it impossible.
Given the way you describe the question, I'd say the easiest solution for you would just be to re-implement the JS code as PHP code; it's unlikely that all the work arounds being suggested would be appropriate for what sounds like a fairly simple bit of utility code.

PHP/Flash advice

Looking to start a project that would require me to use Flash or Flex (I have not worked with either of these yet, yikes!!!). Flash would be the front end user interface that needs to display items pulled from a MySQL Database (I was thinking ajax via jQuery but open to suggestions). My question is, What would be the best approach for something like this?
High level
Flash calls to display image through ajax/php from Db
I don't know how to code the Flash part, any tutorials that kinda sound like what I'm doing? suggestions? thoughts? other ideas?
Side Note: The database table will contain text that describes the image being passed to flash, so I would like to display both the text and image.
Thanks for any advice/help,
--Phill
I'd recommend you try AMFPHP or ZendAMF. Both of these use AMF3 (which requires you to use Actionscript 3/FlashCS3+), and it is one of the quickest ways to get data into Flash.
You could also use php to generate an xml file and just request that url in Flash to load up the XML. You can also use ajax like you said, but it's probably going to be the slowest method open to you, unless you're working with very small data sets.
Regardless of the method you choose, I'd suggest you use Actionscript 3. It's much faster and if you use XML, it'll be a life saver.
Here's a tutorial about setting up flash to work with PHP & MySQL:
link

Categories