Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing a PHP Script Within Qt Application

Tags:

c++

php

qt

qprocess

I'm developing a Qt application where I need to execute a PHP script. Despite the PHP file being located in the same directory as the project files, I receive an error when trying to run the script using QProcess. Here's the snippet causing the issue:

this->getMined.start("php", QStringList() << "get_cexio_BTC.php");
this->getMined.waitForFinished();
QByteArray output = getMined.readAll();

When I run the application, I get the following error:

Could not open input file: get_cexio_BTC.php

Is there something I'm overlooking in the QProcess setup that could be causing this file path issue?

like image 908
Kamil Pajak Avatar asked Feb 22 '26 05:02

Kamil Pajak


2 Answers

So if php is not found in the path, or didn't get loaded into the environment variables that QProcess is using, you could get an error like that.

Try printing out the environment variables:

qDebug() << "Process Environment" << myProcess.getProcessEnvironment().toStringList();

If under the PATH variable it doesn't mention the location of php.exe, it will fail.

You may need to specify php.exe instead of just php. If it is being found correctly, then you should be able to see the usage statement of the php command when it isn't ran with any input file.

http://www.php.net/manual/en/features.commandline.options.php

If that isn't the issue, then make sure that you are in the working directory that you think you are.

system("dir");

or

qDebug() << "current path" << QDir::currentPath();

You can change the current working directory in code, or specify it for a specific process or you can go and change it from Qt's Project Settings > Run > Working Directory.

And if you didn't already, be sure to check/print out the error string of your QProcess instance when it isn't doing what you want it to do.

qDebug() << "error string?" << myProcess.errorString();

And make sure you know to use forward slashes and quotes where appropriate. If you have an unquoted command line argument/filename, it may see spaces in your path as another option to process.

And lastly if you are on Windows 8 and you are accessing something on a network drive, you have to run a net use command to be able to access those paths from the commandline, even if they are mapped and working in Windows explorer.

Hope that helps.

like image 130
phyatt Avatar answered Feb 23 '26 19:02

phyatt


You can use QNetworkAccessManager to access a page on localhost to trigger the included PHP script.

for example if you have the following php page in your htdocs directory: test.php

// test.php
echo("hello world");

you can access your page : http://localhost/test.php

QUrl url("http://localhost/test.php");
QNetworkAccessManager nam;
QNetworkReply* reply = nam.get(QNetworkRequest(url));
like image 28
Nejat Avatar answered Feb 23 '26 20:02

Nejat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!