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.
Related
i wrote something that i tend to call a "parser" (no idea if that's the correct term)
all it currently does is grab a local lua file that has a lot of similarity with a json and remove / replace all contents that keep it from being an actual json.
process is something like:
search through all lua files with glob() / file_get_contents()
remove/replace (preg_replace/str_replace) everything from the file that keeps it from being a json
use json_decode() on the final thing
grab all info that I need and add to database
display data
I used php for all these steps
These lua files can be retrieved from a guy that maintains it on github so I thought it would be a smart idea to just parse them right from the raw url.
Ideally I would need github to send me some kind of clue that the guy who maintains it sent an update and then I start parsing in order to automate that process.
Can this be done? Is there a better way to do this? Any pointers are welcome
I have an interactive JavaScript app that runs through arrays of data. I don't want users to be able to easily view the array content, so I have stored the arrays in PHP and pass them to the JavaScript app with AJAX and JSON. I had thought about using a database, but since these files only have to read (not written) I thought it would be unnecessary.
Am I missing an obvious and easier way to do this? Perhaps some sort of server-side cache?
Cheers,
You really shouldn't be using PHP to emit JSON arrays when you could just create static JSON files (like data.json) with the data already in JSON format.
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.
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.
A bit on my requirements, at the moment i'm passing on a search string from a Cocoa app to a PHP script which queries the database, and then generates an XML output for Cocoa again.
The only bit I am having issues with now is generating the XML output from the db query.
At the moment, i'm trying to loop through the results and output them, however I am having issues, I can create the XML file but it doesn't get the results from the loop.
I can post my code if needed, but I'm sure somebody will have a more effective method.
Do not use PHP at all for the XML generation. MySQL can do that. If the XML in a specific format you can use XSLT, but at that point it may be easier to use PHP instead.