2015-10-06 34 views
1

我遇到了一些麻煩,使用php編寫後臺腳本的批量新聞稿。proc_open()無法解析Windows上的本地主機

我注意到通過瀏覽器運行php和通過php可執行文件有很多不同之處。我認爲這是因爲由可執行文件直接執行的php腳本與apache沒有任何交互。 例如,我必須在調用腳本或將其解析爲參數之前將DOCUMENT_ROOT設置爲環境變量,因爲未定義$ _SERVER [「DOCUMENT_ROOT」]。

現在我遇到了這個問題,當我嘗試通過PDO連接到我的數據庫。這裏發生了什麼:

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 

看來本地主機無法解析,但它並沒有明確報告在日誌中。

我該如何解決這個問題?

如果我用我的PDO構造函數中的'localhost'替換爲'127.0.0.1',它就可以工作。很明顯,問題在於解決localhost問題。 我正在使用proc_open()來啓動腳本。我發現如果我通過CMD提示運行腳本,它就可以工作。 它看起來像這樣:開始/ B PHP C:\用戶\對衝\開發\ PHPStorm \ gpstudios \ WWW \文件\程序\ send_newsletters.php

這個問題似乎是與proc_open然後...

編輯

的問題仍然沒有得到解決,但我已經計算出它的根:

proc_open($ CMD,[ '管', 'R'],[ '管',「W '],['pipe','w']],$ pipes,NULL,$ environment_variables);

問題是$ environment_variables,一個包含 - 你猜對它 - 環境變量的關聯數組。 我碰到這個來到php.net:http://php.net/manual/en/function.proc-open.php#117912

對於那些誰發現,使用$ CWD和$ ENV選項將導致proc_open失敗(窗口)。您將需要傳遞所有其他服務器環境變量;

但是,即使我這樣做,它仍然無法解析本地主機。

編輯2

問題已解決。由php.net上的有用用戶提供的代碼使用數組($ _ SERVER),實際上,您只需要$ _SERVER。 所以代碼現在看起來像:

proc_open($cmd, [['pipe', 'r'],['pipe', 'w'],['pipe', 'w']], $pipes, NULL, array_merge($_SERVER, $environment_variables)) 

Huzzah!感謝大家的幫助。

+1

請顯示您的代碼。我懷疑你在'new PDO'中傳遞了一個不正確的主機名。 – Barmar

+1

你在'E_ALL'上有錯誤報告嗎?我看到你有警告但可能不通知。你可以在'Connection.php'中發佈相關的pdo構造函數,以便我們看到如何構建DSN字符串?它實際上在我看來像字符串'localhost'沒有被正確地傳遞給DSN,因爲我想我會希望錯誤消息有更多的信息。 –

+0

@Barmar Connection.php的內容是不相關的。我只是在做'新的PDO(「mysql:host = $ host; dbname = $ db; charset = utf8」,$ username,$ password);' 您明白我正在通過php可執行文件運行命令,而不是通過瀏覽器?您會注意到很多差異,無法解析本地主機顯然是其中之一。 – hedgehog90

回答

1

Solutuion在這裏找到:http://php.net/manual/en/function.proc-open.php#117912

對於那些誰發現使用$ cwd和$ env選項會導致 proc_open失敗(窗口)。您將需要通過所有其他服務器 環境變量;

雖然有幫助的用戶稍微有點不對,但他們把$ _SERVER放入數組中。這是對我有用:

proc_open($cmd, [['pipe', 'r'],['pipe', 'w'],['pipe', 'w']], $pipes, NULL, array_merge($_SERVER, $environment_variables))