0

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
Ood
  • 1,445
  • 4
  • 23
  • 43
  • @KIKOSoftware No this code is in a separate file, no includes in there either. – Ood Jan 29 '23 at 23:13
  • @KIKOSoftware That's just the port I am using. The port is not the issue, I just tried that out. – Ood Jan 29 '23 at 23:21
  • @Phil cURL with PHP. From the command line it works with cURL. – Ood Jan 29 '23 at 23:21
  • Does it work if you use another URL like `https://example.com`? It definitely sounds like a recursive request problem as mentioned by @KIKOSoftware in their first comment – Phil Jan 29 '23 at 23:23
  • @Phil Yes it works with `https://example.com`. However, it can't be a recursion problem because it happens for any file I try to request on the localhost server. For example I have tried requesting a TXT file--same issue. – Ood Jan 29 '23 at 23:25
  • How are these services configured? Are you running Apache / NGINX or the built-in PHP server? If the former, do you have any URL rewriting or reverse proxies configured? If the latter, are you using a router file? – Phil Jan 29 '23 at 23:31
  • @Phil This happens on the built-in PHP test server on macOS with no custom configurations. So it's not an Apache sever or development environment that uses rewriting or anything like that. – Ood Jan 29 '23 at 23:35
  • 1
    And how are you running the _client-side_ PHP code? Probably best if you [edit] your question to show exactly how you're running both of these services and how you're running the script in your question (eg via CLI, opening a browser page, etc) – Phil Jan 29 '23 at 23:37
  • @Phil Added everything I could think of. – Ood Jan 29 '23 at 23:42
  • 1
    _"running a PHP test server and I want to request a PHP file from another one"_ this was very confusing in your question as it sounds like you're running multiple services when in fact you're only running one. In any case, I've found the relevant duplicate for you – Phil Jan 30 '23 at 00:10

0 Answers0