I've run into some trouble with a batch newsletter sending background script written in php.
I've noticed lots of differences between running a php via the browser, and via the php executable. I assume this is because php scripts executed directly by the executable don't have any interaction with apache. For instance, I have to set DOCUMENT_ROOT as an environment variable before calling the script or parse it as an argument, because $_SERVER["DOCUMENT_ROOT"] is not defined.
Now I've run into this problem when I try and connect to my database via PDO. Here's what happens:
C:/Users/hedge/Dev/PHPStorm/gpstudios/www/files/processes/send_newsletters.php hfBH6rCA 2>&1
log.js:137 Warning: PDO::__construct(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\class\Connection.php on line 42
Call Stack:
0.0003 126144 1. {main}() C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\processes\send_newsletters.php:0
0.0016 155392 2. getConnection() C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\processes\send_newsletters.php:14
0.0023 196416 3. Connection->__construct() C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\inc\config.php:20
0.0023 196760 4. PDO->__construct() C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\class\Connection.php:42
Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host is known. ' in C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\class\Connection.php on line 42
PDOException: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\class\Connection.php on line 42
Call Stack:
0.0003 126144 1. {main}() C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\processes\send_newsletters.php:0
0.0016 155392 2. getConnection() C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\processes\send_newsletters.php:14
0.0023 196416 3. Connection->__construct() C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\inc\config.php:20
0.0023 196760 4. PDO->__construct() C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\class\Connection.php:42
PHP Warning: PDO::__construct(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\class\Connection.php on line 42
PHP Stack trace:
PHP 1. {main}() C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\processes\send_newsletters.php:0
PHP 2. getConnection() C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\processes\send_newsletters.php:14
PHP 3. Connection->__construct() C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\inc\config.php:20
PHP 4. PDO->__construct() C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\class\Connection.php:42
PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host is known. ' in C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\class\Connection.php:42
Stack trace:
#0 C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\class\Connection.php(42): PDO->__construct('mysql:host=loca...', '<redacted>', '<redacted>')
#1 C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\inc\config.php(20): Connection->__construct('localhost', '<redacted>', '<redacted>', '<redacted>')
#2 C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\processes\send_newsletters.php(14): getConnection()
#3 {main}
thrown in C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\class\Connection.php on line 42
It seems localhost can't be resolved, but it doesn't explicitly report that in the logs.
How do I fix this?
If I replace 'localhost' in my PDO constructor with '127.0.0.1', it works. Clearly the issue is with resolving localhost. I am using proc_open() to start the script. I found that if I run the script through CMD prompt, it works. it looks like this: start /B php C:\Users\hedge\Dev\PHPStorm\gpstudios\www\files\processes\send_newsletters.php
The issue would appear to be with proc_open then...
EDIT
The problem is still unresolved but I've worked out the root of it:
proc_open($cmd, [['pipe', 'r'],['pipe', 'w'],['pipe', 'w']], $pipes, NULL, $environment_variables);
The problem is $environment_variables, an associative array which contains - you guessed it - environment variables. I came across this on php.net: http://php.net/manual/en/function.proc-open.php#117912
For those who are finding that using the $cwd and $env options cause proc_open to fail (windows). You will need to pass all other server environment variables;
However, even when I do just that, it still fails to resolve localhost.
EDIT 2
Problem resolved. The code provided by the helpful user on php.net used array($_SERVER), when in fact, you just need $_SERVER. So the code now looks like:
proc_open($cmd, [['pipe', 'r'],['pipe', 'w'],['pipe', 'w']], $pipes, NULL, array_merge($_SERVER, $environment_variables))
Huzzah! Thanks for the help everybody.