proc_open() fails to resolve localhost on Windows - php

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.

Solutuion found here: 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;
The helpful user got it slightly wrong though, they put $_SERVER in an array. This is what worked for me:
proc_open($cmd, [['pipe', 'r'],['pipe', 'w'],['pipe', 'w']], $pipes, NULL, array_merge($_SERVER, $environment_variables))

IMHO, your host not found, please check host or see this
PHP php_network_getaddresses: getaddrinfo failed: No such host is known
mysql_connect: php_network_getaddresses: getaddrinfo failed: No such host is known using file values

Related

phpunit error php_network_getaddresses: getaddrinfo failed: Name or service not known when running composer locally

When i try to run phpUnit locally i get this error:
Runtime: PHP 8.0.25
Configuration: /var/www/html/php-project/phpunit.xml
Warning - The configuration file did not pass validation!
The following problems have been detected:
Line 22:
- Element 'testsuites': This element is not expected.
Test results may not be as expected.
E 1 / 1 (100%)
Time: 00:00.375, Memory: 6.00 MB
There was 1 error:
1) ShoppingCartRepositoryTest::testGetShoppingCart
PDOException: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known
/var/www/html/php-project/src/Config/ConnectDb.php:17
/var/www/html/php-project/src/Config/ConnectDb.php:24
/var/www/html/php-project/src/Repository/ShoppingCartRepository.php:19
/var/www/html/php-project/test/Repository/shoppingCartRepositoryTest.php:14
Caused by
PDOException: PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Name or service not known
Since im new to PHPUNIT i dont know if im missing some sort of configuration, i'll be more than grateful if someone can help.
It's plain php (no frameworks)

Occasional "php_network_getaddresses: getaddrinfo failed" connecting to DigitalOcean Managed Database

The other day I purchased a DigitalOcean Managed Database to use as a secondary MySQL server to reduce load on our primary CPanel CentOS server.
This has worked great but every couple of hours our error logs from our PHP scripts fill with the following errors:
[24-Jan-2022 04:16:21 UTC] PHP Warning: mysqli::__construct(): php_network_getaddresses: getaddrinfo failed: Name or service not known
[24-Jan-2022 04:16:21 UTC] PHP Warning: mysqli::__construct(): (HY000/2002): php_network_getaddresses: getaddrinfo failed: Name or service not known
[24-Jan-2022 04:16:21 UTC] PHP Warning: mysqli_set_charset(): Couldn't fetch mysqli
[24-Jan-2022 04:16:21 UTC] PHP Warning: mysqli::prepare(): Couldn't fetch mysqli
[24-Jan-2022 04:16:21 UTC] PHP Fatal error: Uncaught Error: Call to a member function bind_param() on null
These errors are originating on this line:
$db = new mysqli("ygo-external-database-do-user-10645539-0.b.db.ondigitalocean.com:25060","username","password", "database");
I don't think it's a firewall issue since it is working most of the time. The website is also using Cloudflare but I don't think that is the issue either as I paused Cloudflare once when it occurred and the issue remained for ~20 minutes or so. Restarting the Litespeed web server tends to fix this immediately but I am non the wiser as to why this is happening.

Symfony 5 SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed:

I am trying to run
php bin/console make:migration
I received these errors
In AbstractMySQLDriver.php line 93:
An exception occurred in driver: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution
In PDOConnection.php line 31:
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution
In PDOConnection.php line 27:
SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution
In PDOConnection.php line 27:
PDO::__construct(): php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution
I have checked all of my .env files. I have configured the
DATABASE_URL=mysql://mylocalDb:#fL3#&_{Dx3N#127.0.0.1:3306/LocalDataBase
I have changed the doctrine config to use server_version: '5.7'. I don't know for sure if that correct. I just removed the # from in front of the line because of the note that said you must configure your server version. But not sure which server it is referring to.
So, my question is how do I test the database connection in Symfony. I have searched the documents and was unable to find a clear answer to this question. When I run
php bin/console doctrine:database:create
This produces an error
In CreateDatabaseDoctrineCommand.php line 82:
Connection does not contain a 'path' or 'dbname' parameter and cannot be created.
I have cleared my cache twice after making config changes and still receive the same error messages.
What other ways can I test the db connection.
https://symfony.com/doc/current/doctrine.html#configuring-the-database
Your DB password contains '#' which breaks Symfony's parser, changing the password to not have the symbol should resolve your issue.
Like ikyuchukov said, the "#" character breaks the parsing. But instead of changing your password, url-encoding this character will do a better job, in my opinion. You can replace it with it's url-encoded value "%40".

file_get_contents(http://localhost:51089): no such host is known

I am following step-by-step guide of google app engine "Using the User Service", but after I ran the app server locally
C:\Users\xxx>dev_appserver.py --php_exectuable_path="C:\Users\xxx\Desktop\php54\php-cgi.exe" C:\Users\xxx\Desktop\FirstPhpPrj\
below errors pop up:
Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed:
No such host is known. in <b>C:\Users\xxx\Desktop\google_appengine\php\sdk\
google\appengine\runtime\RemoteApiProxy.php</b> on line 92
Warning:
file_get_contents(http://localhost:51089)
: failed to open stream:
php_network_getaddresses: getaddrinfo failed: No such host is known. in C
:\Users\xxx\Desktop\google_appengine\php\sdk\google\appengine\runtime\RemoteApiP
roxy.php on line 92
Fatal error: Uncaught exception 'google\net\ProtocolBufferDecodeError'
with message 'Not initialized: login_url' in C:\Users\xxx\Desktop\google_appengin
e\php\sdk\google\appengine\runtime\proto\ProtocolMessage.php:121
Stack trace:
#0 C:\Users\xxx\Desktop\google_appengine\php\sdk\google\appengine\runtime\proto\
ProtocolMessage.php(88): google\net\ProtocolMessage->mergeFromString('')
#1 C:\Users\xxx\Desktop\google_appengine\php\sdk\google\appengine\runtime\Remote
ApiProxy.php(109): google\net\ProtocolMessage->parseFromString('')
#2 C:\Users\xxx\Desktop\google_appengine\php\sdk\google\appengine\runtime\ApiProxy.php(42): google\appengine\runtime\RemoteApiProxy->makeSyncCall('user', 'CreateLoginURL', Object(google\appengine\CreateLoginURLRequest), Object(google\app
engine\CreateLoginURLResponse), NULL)
#3 C:\Users\xxx\Desktop\google_appengine\php\sdk\google\appengine\api
\users\UserService.php(69): google\appengine\runtime\ApiProxy::makeSyncCall('user', 'CreateLoginURL', Object(google\appengine\CreateLoginURLRequest), Object(google in C
:\Users\xxx\Desktop\google_appengine\php\sdk\google\appengine\runtime\proto\ProtocolMessage.php on line 121
I browsed the log, there are two warnings and one fatal error. But not after I looked thru the coding of php sdk, i realized that this fatal error, caused by empty string input in "mergeFromString", is in fact caused by the warnings of"
file_get_contents(http://localhost:51089)
no such host is known", because in RemoteApiProxy.php, function CreateLoginURLResponse:
$serialized_remote_respone = file_get_contents(
'http://localhost:' . $this->apiPort, false, $context);
once serialized_remote_response gets an empty result, it will affect the command in next line:
$remote_response->parseFromString($serialized_remote_respone);
In this case, my own codes are actually irrelevant as it is exactly the same as the google sample codes:here.
I just want to figure out why on earth, after google app engine starts its API server at
http://localhost:51089
yet it is not recognized! I tried pin this host and windows also threw error!

php failed to connect to mongodb socket

just set up a fresh development ubuntu 13.04 server with nginx, php-fpm, and mongodb. The following line of code kills all of my scripts that were running great on a production ubuntu 12.10 server
$m = new MongoClient("mongodb://user:pass#/tmp/mongodb-27017.sock/database");
error
Warning: MongoClient::__construct(): php_network_getaddresses:
getaddrinfo failed: Name or service not known in
/var/www/app/php/db.php on line 4
Fatal error: Uncaught exception 'MongoConnectionException'
with message 'Failed to connect to: /tmp/mongodb-27017.sock:0:
php_network_getaddresses: getaddrinfo failed: Name or service not
known' in /var/www/app/php/db.php:4 Stack trace:
0 /var/www/app/php/db.php(4): MongoClient->__construct('mongodb://app...')
1 /var/www/app/admin/loggedin.php(3): require('/var/www/app...')
2 /var/www/app/admin/index.php(1): require('/var/www/app...')
3 {main} thrown in /var/www/app/php/db.php on line 4
I've been toying with this for a day and half and can't figure it out. It works when I connect to localhost but I'd really prefer sockets. any ideas?
The PHP driver does not support passing in the credentials as part of the URI when connecting over Unix Domain Sockets.
You need to pass in the username and password in the $options array (2nd argument).
Ubuntu 14.04, Php 5.59, MongoDB shell version: 2.4.9
I got mine to work by passing the following:
I had to change my connection from $m = new Mongo(); // connect to $m = new Mongo("mongodb://127.0.0.1"); // connect
For some reason it appears that the /etc/hosts file stopped being called localhost for 127.0.0.1 which was throwing off the default mongodb connector so I had to be explicit. Sigh.

Categories