Why does file_get_contents work with google.com but not with my site?

$page1 = file_get_contents('http://www.google.com'); $page2 = file_get_contents('http://localhost:8000/prueba'); 

When I repeat the results, Google works, but not with my site. And when I put the address in explorer, it works. And this happens with the whole site that I am doing in django .: (

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

Fatal error: maximum run time exceeded by 60 seconds in C: \ xampp \ htdocs \ squirrelmail \ plugins \ captcha \ backends \ b2evo \ b2evo.php on line 138

+8
source share
2 answers

For those who have this problem using the PHP Embedded Web Server (with Laravel in my case), this is caused by blocking your request by the file_get_contents () / curl functions.

Dev server docs say that

PHP applications will stop if the request is blocked.

Since the embedded PHP server is single-threaded, requesting a different URL on your server will stop the first request and time out.

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

Edit: At the moment, I suggest you use Homestead as a development environment for PHP projects. This saves a lot of work with setting up, creating virtual hosts and configuring the database for more projects.

+20
source

As zub0r pointed out pointed out , the embedded PHP server is single-threaded. If you do not want to install and configure a web server such as nginx, and do not want to use Homestead or Valet, there is another simple solution:

Run another instance of the embedded PHP server with a different port and use it in your application's internal requests.

 php -S localhost:8000 \\ in another console php -S localhost:8001 

I use this in my Laravel application when I request some local dummy API via Guzzle and it works fine.

0
source

All Articles