Calling PHP from Rails with input and output - php

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.

Related

How do I work with an Amf ByteArray string in php?

I have what I think is a ByteArray. When using urlencode on the string, I get this data...
%0A%82%03%01%11SaleDate%0DToYear%0DYardId%15BuyNowOnly%0BState%0BModel%0FStockId%11FromYear%11FullText%13Inventory%11Category%09Make%0FOrderBy%1DOrderDirection%0FPageNum%11PageSize%01%01%06%05-1%02%06%01%01%01%01%06%0F1024187%06%09Sale%01%01%06%13RunNumber%06%07Asc%04%01%04d
I am looking at the php function unpack, and trying unpack('c4char/Xstr/...') where X is a, A, h, H to try to pull the "SaleDate" string out, but I can't get it to work.
Probably the completely wrong approach. Do I need to write a custom unpacker of some sort?
I'm not even sure what I'm dealing with here, it's the output from a flash file, so I think it's an Amf "ByteArray" but I'm at a loss as to how to parse it.
Looking for even just a direction to start hunting for how to manipulate/parse/rebuild something like this. Ultimately I'd love to be able to run it through a function, get an associative array, change a few values, repack it, and send it on it's merry way.
I think it's AMF3 and this is the data in the messages part.
I guess if there's one "main" question, how do I manipulate that string of data simply and safely?
Thanks for any leads.
There are a lot of AMF deserializers out there that are open source, for simplicity sake, would probably make a lot of sense to not roll your own here, even though AMF3 is fairly simple.
I came across this link on github which looks promising:
https://github.com/silexlabs/amfphp-2.0
You probably don't want to roll the whole framework, but can probably roll this:
/core/amf/Deserializer.php
/core/amf/Serializer.php
As far as I can tell, this appears to be a proprietary encoding method specific to the vendor. They use a "Byte Array" to allow greater flexibility of the payload. There isn't an easy way to work with this, without completely reverse engineering their encoding and decoding algorithms.

How best to pass a python dictionary to PHP using file IO

I have a python script running on a cron job, once a minute, which builds some data into a dictionary. I am writing that data into a simple, space separated, text file and reading that into PHP whenever the webpage is requested.
What is correct way to do pass this information? What I am doing now certainly feels clunky since I am rebuilding essentially the same structure in PHP. How do I just write my python dictionary (perhaps an order dictionary is needed) to disk so that PHP can read it directly into a PHP array?
I also worry a bit about whether this method of transfer is kinda archaic. Should I be using a database instead, e.g. SQLlite? I worry that someone will load the page as the file is being updated which might cause some problems.
I just need something light and simple. Don't want to overcomplicate it. Thanks in advance.
Any language can read JSON, as JSON is simply a way to format data as text, which can easily be parsed by any program. To write the dictionary as a json file, you do this:
import json
myfile = open('data.json','w+')
#create your dict
json.dump(mydict,myfile)
myfile.close()
now to read it in PHP:
$data = json_decode(file_get_contents("data.json"));
I don't know whether PHP handles json data, but if so, that's probably the way to go. See the json module documentation for more...
And in fact, PHP does handle json data: http://php.net/manual/en/book.json.php
This should be a relatively simple but robust way to transfer the data.

What templating languages are suitable for JS and PHP?

I'm looking for a templating "language" that works for both PHP and JS. I had a look at Mustache (has a very limited "if") and a few other like jquery-tmpl-php. So far but none of these seem to fulfil all my criteria:
Works with data provided by JSON (array, map, literal)
Has an "if" statement that can at least check if
a key in a map exists
a list element is first/last/odd/even
a value is equal to a literal
Can iterate over a list (iterating over keys in a map would be a bonus)
The same template and data generates exactly the same result with PHP and JS
Fast enough (I know, it's a bit vague)
Preferably no compiling step
Bonus: a nice way to "pluralize" texts and basically everything that makes i18n easier
Not smarty :)
I appreciate any ideas, suggestions or tips
Thanks,
Marek
I just wrote this whole post up only to re-read your question and see "not Smarty" :P . I'll just say I looked around for a while for a JS and PHP template library and Smarty seemed like the best option for me. You say Mustache is not enough. The only other one that comes to mind with both JS and PHP functionality is Haml.
Original:
I don't have a ton of experience with this yet but I've started using Smarty PHP templates. I create one Smarty template and send JSON to the client for Ajax requests. I then use this JS Smarty engine for client-side processing. If the client does not support Javascript, I can gracefully degrade by just sending the PHP associated array to the Smarty renderer server-side rather than sending it to the JSON parser. Still uses same .smarty file, working fairly well so far and I think I'm going to keep using this methodology for the rest of my project.

Auto extract strings to defines

I just inherited a website written in PHP to internationalize it... The problem is the code is not consistent. It has some functions that have strings inside echo and another pieces and in another functions the php is closed and html is presented the right way.
I was looking for a tool that could easy my job as much as possible. Retrieving as much strings as it could and defining (through defines) in another file.
I though about creating a script with regex functions to achieve this but if there was anything out there... I looked but couldn't find. Maybe I'm using wrong terms as I'm not English native.
Does anyone know a good way to do this?
If you are open to using gettext for I18N, then you can use xgettext: http://www.gnu.org/s/hello/manual/gettext/xgettext-Invocation.html
Here is a tutorial detailing how you can use it in PHP: http://www.phpdig.net/ref/rn26.html

Passing a string to Flash from 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.

Categories