Why does file_get_contents work with google.com bu

2019-01-12 04:23发布

问题:

$page1 = file_get_contents('http://www.google.com');

$page2 = file_get_contents('http://localhost:8000/prueba');

When I echo the results, with Google it works but not with my site. And when I put the address on the explorer works. And this happen with all the site that i make in django. :(

Warning: file_get_contents(http://localhost:8000/prueba) [function.file-get-contents]: failed to open stream: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond. in C:\xampp\htdocs\squirrelmail\plugins\captcha\backends\b2evo\b2evo.php on line 138

Fatal error: Maximum execution time of 60 seconds exceeded in C:\xampp\htdocs\squirrelmail\plugins\captcha\backends\b2evo\b2evo.php on line 138

回答1:

For anyone having this problem using PHP Built-in web server (with Laravel in my case), it is caused by your request being blocked by file_get_contents() / curl functions.

Docs of dev server say that

PHP applications will stall if a request is blocked.

Since the PHP built-in server is single threaded, requesting another url on your server will halt first request and it gets timed out.

As a solution, you can use nginx (LEMP stack) or other web servers.

Edit: As of now, I really suggest you to use Homestead as development environment for PHP projects. It saves you lots of work with configuration, creation of virtual hosts and DB configuration for more projects.



标签: php django get