Executing a php script from C - php

I am trying to write a c program that executes a PHP script.
I tried using php_execute_script function that I came across in my google search.
However, it seems I have to call TSRMLS_FETCH() which throws an exception.
Does anyone have an idea why? or how should I do this in the proper way?
Thanks.

you can use exec function:
exec ("php myscript.php");
or you can use popen:
FILE *p;
p = popen("php myscript.php","r");
pclose(p);

What you want is embedding the PHP interpreter.
You may try PHPEmbed from the facebook github

Related

PHP website not running exec function when passing image as an argument

I'm having trouble running the "exec" function in my PHP website. I am able to run it several times with an executable that just takes in a variable argument and returns some test message. However, when I use an executable that does some image processing, where I want to pass an image from the website that a user uploads as an argument, it does not seem to be executing the executable at all. I even have some cout commands in the executable to ensure its running, but these are not being displayed on the website. So I think that for some reason the php cannot run the executable? I am able to run it fine from my desktop...
Here's an example of the code that isn't working on my PHP website:
$imgtest1="/uploaded_files/me.jpg";
$imgtest2="/uploaded_files/clusteroutput.jpg";
$nosuppix = 400;
$noweight = 100;
$executabletest = exec("ImgProc $imgtest1 $nosuppix $noweight $imgtest2");
echo $executabletest;
Is there a way to debug or get an error output from the exec function? Is there something I'm missing when passing an image to the executable? The executable uses several DLL files which are in the same folder as the executable. Do they need to be packaged together for some reason? I apologize, but I really don't know what's left to test...
***Edit: I'm now able to get it to run if I write out all the code in the escapeshellcmd itself... how come I'm not able to just pass the variables?
$cmdinput = escapeshellcmd('SuperpixelsFinal "D:/WebPages/TALIA ART/TALIA ART/uploaded_files/me.jpg" 400 100 "D:/WebPages/TALIA ART/TALIA ART/uploaded_files/clusteroutput.jpg"');
For anyone else having this issue, it is because I was using escapeshellcmd when I should have used escapeshellarg for each individual string and then run the exec with double quotes using only variables like so:
$executabletest = exec("SuperpixelsFinal $imgtest1 $nosuppix $noweight $imgtest2");

PHP excute Python SFTP script

in PHP I need to do some SFTP, but I am having issues because I am not allowed to install the SSH extension, and phpseclib is not working how I need it to.
As such, I am going to execute a Python script to do the SFTP for me. What I imaging is doing something like the following
exec("SFTPUpload.py remoteFile serverLocation");
So I execute SFTPUpload.py passing it the location of the file on my server which needs transferring, and the location on the server it needs transferring too.
In terms of the Python script (I am not too familiar with Python), I imagine it would be something like the following
username='sftpUser'
password='sftpPassword'
port=22
#SFTP
client.load_system_host_keys()
print " hostname =%s \n username=%s \n password=%s \n" (hostname,username,password)
t = paramiko.Transport((hostname, port))
t.connect(username=username,password=password)
sftp = paramiko.SFTPClient.from_transport(t)
sftp.put(source,destination)
sftp.close()
t.close()
However, the way I am calling it from PHP, I need the Python to be in a class or something so I can pass it the variables.
How would I achieve something like this?
Thanks
I believe you can do it with the exec() function as you described by simply parsing the command line parameters in Python.
Like:
import sys
print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)
Where you can access the elements in sys.argv like a list.
(You could also take a look at the getopt module which offers even more (C-like) parameter passing options, but I believe the above solution will do.)
If the exec() function does not work, I believe you could consider to use the system function in PHP:
Something like this:
$mystring = system('SFTPUpload.py remoteFile serverLocation', $retval);

How to take user input from HTML form to C program

I have this small program which takes input from stdin
sample.c
#include<stdio.h>
main()
{
int l=0;
scanf("%d",&l);
printf("\n%d",l);
}
ofcourse! compiled it: cc sample.c and got a.out
and i am trying to run it via php like
$runcmd = "./a.out > output.txt";
exec($runcmd,$outp);
print_r($outp);
my problem is i dont have any idea how to give input to this program so that scanf can read that?
please help me here!
googling gave some tips like proc_open, popen .... but i couldn't make it.
Thanks in Advance.
take a look at popen
http://se1.php.net/popen
it works a bit like fopen, and when using fwrite, insted of writing to a file you can write to a prosses stdin insted.
$runcmd = "./a.out > output.txt";
$process_stdin = popen($runcmd, 'w');
fwrite($process_stdin, "text to send to process");
pclose($process_stdin);
If you design your C program as a server, you should use sockets or named pipe. That way, you will be able to interact with it without launching it.
You can use popen if you want to use it multiple time almong your script.
If you just need to use it one time, you can just pass parameter as arguments.
Why not passing it as a command line argument to your C program. Then instead of using scanf you get your input in argv[].

Running a second PHP script while keeping the client on same page

I'm creating a app that requires me to run a second php script while the first script is still running.
I'm new to php programing so I'm sure there's a simple function I can use that I'm just not aware of.
Looking forward to any help...
Shane
Since you are new to PHP I'm guessing you're looking for the include/require (and include_once/require_once) language constructs which will execute another PHP script as if it is part of the current script.
Otherwise if you want it to run as a separate process look into exec, shell_exec, or backticks. If you need the other PHP script to run as a background process make sure to redirect stdout somewhere (a file or maybe /dev/null if you don't need it) so that your currently executing script doesn't have to wait for it to finish to continue executing.
This will actually require us to use some Javascript for an ajax call to execute our PHP and return it's data.
I prefer Jquery, which will look similar to this:
function callPHP(){
$.post('./filetocall.php', {variableid: 'id'}, function (response) {
$("#div_for_return_data").val(response);
});
}
filetocall.php can look like anything. It's output will populate the #div_for_return_data
eg:
<?php echo $_GET['variableid']; ?>
Then just call the Jquery function from anywhere.

php exec() error

I'm having a little problem with the following:
When I execute this line:
echo exec(createDir($somevariable));
I get this error:
Warning: exec() [function.exec]: Cannot execute a blank command in /home/mydir/myfile.inc.php on line 32
Any ideas.
Thanks.
exec() expects a string argument, which it would pass on to your operating system to be executed. In other words, this is a portal to the server's command line.
I'm not sure what function createDir() is, but unless it's returning a valid command line string, it's probably failing because of that.
In Linux, you might want to do something like
exec('/usr/bin/mkdir '.$path);
...on the other hand, you should abstain from using exec() at all costs. What you can do here, instead, is take a look at mkdir()
With exec you can execute system calls like if you were using the command line. It hasn't to do anything with executing PHP functions.
To create a directory you could do the following:
exec( 'mkdir [NAME OF DIRECTORY]' );
I'd guess that your createDir() function doesn't return anything. Might also be worth checking that $somevariable is also set to something sensible
You're misunderstanding the purpose of exec(). If all you want to do is create a directory then you should use mkdir().
I think I've derived from other posts and comments what it is you actually want to do:
I think createDir() is a PHP function you've written yourself. It does more than just make a directory - it populates it, and that might take some time.
For some reason you believe that the next command gets run before createDir() has finished working, and you thought that by invoking createDir() using exec() you could avoid this.
Tell me in a comment if this is way out, and I'll delete this answer.
It's seems unlikely that createDir() really does keep working after it's returned (if it does, then we call that 'asynchronous'). It would require the programmer to go out of their way to make it asynchronous. So check that assumption.
Even so, exec() is not for invoking PHP functions. It is for invoking shell commands (the kind of thing you type in at a command prompt). As many of us have observed, it is to be avoided unless you're very careful - the risk being that you allow a user to execute arbitrary shell commands.
If you really do have to wait for an asynchronous function to complete, there are a couple of ways this can be done.
The first way requires that the asynchronous function has been written in an amenable manner. Some APIs let you start an asynchronous job, which will give you a 'handle', then do some other stuff, then get the return status from the handle. Something like:
handle = doThreadedJob(myParam);
# do other stuff
results = getResults(handle);
getResults would wait until the job finished.
The second way isn't as good, and can be used when the API is less helpful. Unfortunately, it's a matter of finding some clue that the job is finished, and polling until it is.
while( checkJobIsDone() == false ) {
sleep(some time interval);
}
I'm guessing createDir() doesn't have a return value.
Try exec("mkdir $somevariable");

Categories