TTY ,SWIG and PHP - php

Need some help here. I have a c++ library for communicating with an embedded module (ArchLinux) via tty. This library was compiled/converted into php using swig.
The issue now is that a sample program written in php and run from the command line executes as expected but when this same code is used as part of a web page's functionality it fails to execute.
My assumption based on my limited linux knowledge is that tty requires a console in order to run which is why it fails to run as part of a webpage??
Does anyone have any ideas as to how I can get this to work? I have read something about using posix_ttyname but I cant seem to find any code samples that demonstrate its use.
I have attached the offending c++ files along with a test main.php which works for review.
Thanks everyone
http://www.mediafire.com/?ctblcvsy86mdg8p

$argv variable is available only when script is called from CLI. If you don't want to change the script so it could be called from web, you can try calling it from another script as
exec('main.php param');
Just like you do from command line.

Related

how to get a php script to run a .cpp file

i am trying to find a way to have a PHP script execute a .cpp file, and then return the results via PHP.
I will map out my methodology to avoid confusion in my question:
Certain action on webpage (Button click) -> PHP runs .cpp file -> the results of the .cpp file are returned to PHP -> returned data is used to repopulate the page .
Is this possible to do? (please do not vote down, i have looked online and could not find a solid lead to help me establish this connection)
I hope you mean to run a built executable...
The exec function will run the application similar to running it through a command line. This means that you can get the standard output of the application like you would through the command line.
How to run abc.exe using php
It's kind of kludgy, if you can, just duplicate the executables functionality in PHP.

How to import custom bash functions to be used in PHP exec/system/etc?

I'm writing a command-line application that will substitute a bunch of bash functions and manual work made by a team of developers. Currently, half of what we usually do is inside a ~/.functions file that is sourced in the ~/.bash_profile of each developer.
My command-line application is written in PHP, and for a while I would need to run some of those functions from inside my application. However, the following code would not work, the output says it cannot find the given function:
exec('bash my_legacy_functions.sh');
exec('my_custom_legacy_function param1 param2');
I may be wrong, but I could understand that every exec() call runs a command in a separate process, meaning the functions would not be available for subsequents exec() calls. Is this right, and if yes, would it be possible to override this behaviour without having to bundle everything into one call?
In the end it turns out the default shell was not bash, and on top of that source is not a common command in bash. I found by this other question's answer that the solution is something like:
function run($cmd) {
exec("bash -c 'source my_legacy_functions.sh; $cmd'");
}

Google Analytics from PHP CLI

Has anyone ever done Google Analytics tracking from a PHP command line program?
I have a PHP command line program that is run through cron. It will grab data every 5 minutes and I need to track that.
Looked at the GA library but it looks like either it's using JavaScript or it needs an <img> tag.
Please enlight.
Thank you,
Tee
http://code.google.com/p/serversidegoogleanalytics/
Never used it myself but looks like exactly what you're looking for.
Edit:
Just pondering your question again and realised you were asking specifically for PHP CLI as a command line program. Just wanted to make sure you're aware that you can just use it to call a file and that'll be the only command line part. The rest of the program can be full OOP code (As my suggestion is structured) which includes files and extends classes.
So using CLI as Command line isn't really limiting as your tone suggests.
If you need to track that, just write a log file ..
file_put_contents('/tmp/myscript.log', "running cron\n", FILE_APPEND);
Google Analytics is used to track visits of a website and not cronjob activities. :s

How to run a command line script through a normal Zend Framework application

I am working on a ZF application that needs to run a command line script and then parse the results into something meaningful and return to the user.
I know there are various PHP functions, like exec and system, but I was wondering if there is anything built into Zend Framework that does command line scripting easily.
Even if there isn't a ZF specific function, what is the best function/method to use for running a command line script and then retrieving the results in PHP upon completion of the script.
I would write a Service which uses exec to get what you need. You can add some basic error handling there.
You can start an process under linux with this:
ZendX_Console_Process_Unix
But never tryed it..
We recently needed to do this (we use ZF too), hope this helps: http://www.kintek.com.au/web-design-blog/how-to-run-a-php-script-from-command-line/

Calling fcsh from PHP script

My question is whether or not Flex's fcsh can be called from within a PHP script. Here is the background:
I have been created a simple process that creates a simple quiz/tutorial by converting a text file into a .mxml file and compiling to a .swf file using the mxmlc compiler. This works well from the command line, but I wanted to make the process easier by creating a web-interface to do this. My initial attempts with PHP's exec() function have not worked. The Python scripts I use to create the .mxml file work fine (using exec()), but I have not been able to get the mxmlc compiler to work.
After searching on the Web and on this site, I believe that using fcsh (instead of mxmlc) may be the way to go. Using fcsh would certainly compile the .mxml file faster (after the first run), and I think that fcsh can be launched as a service that might be able to be called from PHP.
On the other hand, maybe I am approaching this the wrong way. Would it be better to write a Flex application that calls fcsh and avoid using PHP?
Edit: Using fcshctl as hasseg suggested in his answer below worked very well. Thanks Ali.
The problem with calling fcsh from within scripts is that it works as an interactive shell instead of taking command-line arguments, compiling, and returning an exit status. There are different ways to get around this, which I've listed in this blog post of mine, where I mainly talk about fcshctl (which is my own solution for this,) but at the bottom of the post I've also listed other similar solutions to get fcsh integrated into nonstandard build workflows.
There are a few other ways in php to execute an external script. They are exec(), passthru(), system(), and backticks i.e. the key to the left of the 1 key. Each one has a different purpose and return mechanism.
You may have to put the command that executes your executable into a script and call that script via one of these functions.
Is there a particular reason why you can't use mxmlc directly? It seems like it would be easier to call than fcsh. Just specify all your compiler options in a XML file run it like mxmlc -load-config path/to/config.xml. You can find an example of the XML configuration format in FLEX_HOME/frameworks/flex-config.xml.

Categories