Note that there are some explanatory texts on larger screens.

plurals
  1. POConnection refused for PHP curl() when starting PHP webserver via exec()
    text
    copied!<p>I'm attempting to start PHP's built-in webserver at the beginning of some unit tests. The goal is for the same script to start the webserver and then connect to it to verify our curl wrapper libraries behave appropriately. </p> <p>Script:</p> <pre><code>&lt;?php echo "Starting server...\n"; exec("php -S localhost:8000 -t /path/to/files &gt; /dev/null &amp; echo $!", $output); $ch = curl_init("http://localhost/"); curl_setopt($ch, CURLOPT_PORT, 8000); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_exec($ch); if(curl_errno($ch)) { echo 'Curl error: ' . curl_error($ch); } // Close handle curl_close($ch); echo "PID: ".$output[0]."\n"; exec('kill '.$output[0]); ?&gt; </code></pre> <p>When I run the above script I get this error:</p> <pre><code>Curl error: Failed connect to localhost:8000; Connection refused </code></pre> <p>Notes:</p> <ul> <li>The webserver does start, if I remove the <code>kill</code> at the end of the script I can see the webserver running in the processlist</li> <li>If I start up PHP's webserver on the command line my PHP script can connect to it</li> <li>If I start up PHP's webserver from <em>another</em> PHP script the above script can connect to it</li> <li><code>curl</code> from the command line can connect to the PHP webserver started</li> </ul> <p>I can only guess that there's something being blocked or happening when PHP's webserver is started by a process that then attempts to connect to it?</p> <p>PHP 5.4.15</p> <p>CentOS release 5.6 (Final)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload