I am running a PHP test server and I want to request a PHP file from another one, essentially simulating a remote request. However, every time I try this the PHP file loads indefinitely and locks up the server so that I can no longer access any other file until I restart it.
This is my code:
$url = 'http://localhost:8000/index.php';
$options = array('http' => array('method' => 'GET'));
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
echo $result;
Requesting a relative path, like index.php
works fine, however, that would defy the point of loading the file the way a browser would (with executed PHP).
I have tried implementing the same functionality with PHP/cURL and the same thing happens. Maybe the issue is caused by the configuration of the PHP test server? If anyone has had this problem before or knows what's going on and could let me know it would be greatly appreciated.
Edit: This code is not in index.php. I have also tried to send the request to a TXT file instead, with the same result.
This is my code when trying to request the TXT file:
$url = 'http://localhost:8000/test.txt';
$options = array('http' => array('method' => 'GET'));
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
echo $result;
This produces the same issue and the test.txt
file just contains hello
.
I am using the built-in PHP test server on macOS without any special configurations, like this:
php -S localhost:8000
The PHP file that is requesting the other file is called from the browser by visiting its URL:
http://localhost:8000/test.php