Running a SQL Server PHP Script in background WAMP - php

I've got a script that connects to SQL server to pull some data over to a MySQL database, and I'm wanting to set up scheduled tasks to run this on a regular basis. Ideally it should run in the background, but I'm not too fussed if it opens/closes something.
I've made a bat file with the script
C:\wamp\bin\php\php5.3.0\php.exe "C:\wamp\www\data\import.php"
However it doesn't seem to like the fact it's connecting to SQL Server and it erroring with
PHP Fatal error: Call to undefined function sqlsrv_connect() in c:\wamp\www\includes\connection.php on line 4
Which it doesn't throw when running it in a browser. Any ideas?

It looks like your command line PHP (CLI) does not have the SQL Server extension activated.
Try to:
Check your php.ini file (the CLI-specific one, not the Apache one)
Run php -m to see what modules are activated in command line.

Related

How to use PHP to execute AutoHotKey script on Windows Server 2016?

I have a Windows Server 2016 VPS with Plesk and PHP 7.1x.
I am trying to execute a simple AutoHotKey script from PHP using the following command:
<?php shell_exec('start /B "C:\Program Files\AutoHotkey\AutoHotkey.exe" C:\inetpub\vhosts\mydomain.com\App_Data\myahkscript.ahk'); ?>
This is the only line on the page. I have tried different ahk scripts, the current one simply creates a MsgBox.
When I execute my php page, on VPS Task Manager I see three processes created with the expected USR: cmd.exe, conhost.exe and php-cgi.exe. However, my PHP page just sits waiting on the server and nothing actually happens on the server.
I have also tried the same line except replacing shell_exec with exec. This seems to make no difference. I have tried without start /b with both commands. In that case the PHP page completes but no new processes are started.
I cannot find any errors in any logs: Mod_Security, Plesk Firewall, IIS.
Any ideas?
EDIT:
I tried my command from the VPS command prompt and immediately slapped in the face with the obvious issue of the space in 'Program Files'. I quoted the string as shown above and the command works. This eliminated the hang when running from PHP. However, the command still does nothing when executed from the web page.
EDIT:
Based on suggestions from the referenced post 'debugging exec()':
var_dump: string(0)""
$output: Array()
$return_val: 1
One point was that I would probably not be able to invoke GUI applications. That puts a damper on the idea.

Call to undefined function mysqli_connect() [Cron job only]

When running a php script by navigating to the file directly on the server (via the web), everything runs fine.
However, when setting up the same file to run via a cron job, I get the error message:
Call to undefined function mysqli_connect()
The cron job is set up to run this file, with permission 744:
php -q /home/username/public_html/seafood/php/lobsteremail.php
Any idea why a file might run fine manually but not as part of a cron job?
Is your cron job set to run on boot?
If so, it cant find mysqli_connect() because mysql is not running yet.
Before calling the php script, you need to wait.
Sleep 10 # wait for mysql
php -q /home/username/public_html/seafood/php/lobsteremail.php

Tell PHP to enable or use GD when running script via cron

I am attempting to run a php script via cron and I'm hitting a brick wall.
When I run the php script via the command line as root, everything works correctly.
When I run the php script via the command line as the user, everything works correctly.
The error that I am getting is:
PHP Fatal error: Call to undefined function imagecreatefromstring()
To test I created a php file that has...
<?php
var_dump(gd_info());
?>
When that file is executed via cron it again says that I have a fatal error. "Call to undefined function gd_info()"
So at this point, I've narrowed it down to GD not loading in the cron environment, but at this point, I don't know what to do to fix the issue.
My guess is you have multiple copies of PHP. You and root use one built with GD, cron uses another due to PATH environment variable inconsistencies.
As a working user (you or root), run
which php
That will give you a full path like /usr/bin/php. Use that path in your cron entry, eg
0 0 * * * /usr/bin/php /path/to/your/script.php

cURL not working in Windows Task Scheduler

I've a PHP script that uses cURL to perform certain tasks. At the moment, I have the script running every 10 minutes. This is what I'm running via Windows Task Scheduler.
C:\wamp\bin\php\php5.4.3\php.exe -f C:\wamp\www\autoscripts\index.php
However, for some reason, whenever the argument quoted above is run through the command line, I get the error "Fatal error: Call to undefined function curl_init()". The script works perfectly when I access it via the browser. Is there any reason why PHP isn't able to access the cURL extension via the command line?
Most likely running from command line does not use any ini file that loads the extensions. Open phpinfo() from the browser, copy path to loaded ini file and change your task to:
C:\wamp\bin\php\php5.4.3\php.exe -c "C:\path\to\php.ini" -f C:\wamp\www\autoscripts\index.php
Figured it out. Basically, on WampServer, there are TWO php.ini files that you need to be aware of.
C:\wamp\bin\php\php5.4.3\php.ini
C:\wamp\bin\apache\apache2.2.22\bin\php.ini
Forgot that the command line uses a different ini file than the web server. :(

Run PHP command line, needs socket_create

I have a PHP script that needs to run from command line. One of the function it calls is socket_create.
In php.ini, I have included the following in order to get that function working (see the comment on http://www.php.net/manual/en/sockets.installation.php):
extension=php_sockets.dll
How do I run the script from command line such that it doesn't complain that socket_create is an unknown function? Does PHP CLI actually respect what's in php.ini? I thought it was supposed to, but I do get errors when running it via CLI and no errors when running it via the browser.
UPDATE: by the way, I'm testing this out: https://github.com/nicokaiser/php-websocket. I assume that the server needs to be executed via command line.
A different php.ini file may apply when running php on the CLI rather than as a web server module. You appear to be using Windows, so I'm not sure where this file may be, but it could provide a clue; look for several php.ini files in your disk and see if one of them applies to CLI invocations.

Categories