PHP: file_get_contents() gives me Name or service not known
I hate to say this, but the problem solved itself; I just upgraded the Debian packages (apt-get upgrade
) and the problem went away. Frustrating to not know the cause, though.
Thank you all for your time!
file_get_contents fails with getaddrinfo failed: no address associated with hostname
It seems that allow_url_fopen
is disabled on you php.ini
, that's why file_get_contents
isn't working.
Try using curl instead:
$url = "http://www.example.com/";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$page = curl_exec($ch);
curl_close($ch);
echo $page;
Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name does not resolve
Did you run a short test.php inside the container too?
It is not a Symfony or PHP problem. In short container's network cannot resolve the IP address of the domain muckrock.com. Look for DNS settings for your container network. Tell more about your infrastructure. It is Kubernetes or something another?
For raw docker container running you can use something like this:
docker run -it --dns=8.8.8.8 php:7-fpm-alpine /bin/sh
Or for docker-compose something like this (or for version 3 as more actual)
version: 2
services:
application:
dns:
- 8.8.8.8
php_network_getaddresses: getaddrinfo failed: Name or service not known
If you only want to submit GET data to the URL, you should use something straightforward like file_get_contents();
$myGetData = "?var1=val1&var2=val2";
file_get_contents($url.$myGetData);
Related Topics
The Accuracy of PHP Float Calculate
How to Install Composer PHP Packages Without Composer
How to Redirect Domain According to Country Ip Address
Curl Error: Recv Failure: Connection Reset by Peer - PHP Curl
How to Retrieve Request Payload
Difference Between Session_Unset() and Session_Destroy() in PHP
Handling Data in a PHP Json Object
Show a Pdf Files in Users Browser Via PHP/Perl
Call to Undefined Function Curl_Init().
PHP Curl Not Working - Wamp on Windows 7 64 Bit
How to Override the Path of PHP to Use the Mamp Path
Sha1 VS Md5 VS Sha256: Which to Use For a PHP Login
How to Read Last Lines (I.E. "Tail") from a File Using PHP
How to Get Last Key in an Array
PHP Domdocument Errors/Warnings on Html5-Tags