PHP php_network_getaddresses: getaddrinfo failed: No such host is known - php

I am having DNS issues with a certain target domain. I am using fopen() (but same issue with other functions) to retreive an image, but I get this error:
Warning: fopen(): php_network_getaddresses: getaddrinfo failed: No such host is known
I am able to ping or nslookup the domain from the command prompt, but for some reason php throws this error. When I try fopen('http://www.google.com', r); or other domains, all goes fine. But above mentioned domain simply won't resolve with PHP. I have flushed the DNS, restarted Apache, but still no luck..
I have tried with:
Windows 7, Apache 2.2 PHP 5.3.6
Windows server 2008r2, Apache 2.2 PHP 5.3.6
What can cause this single domain to not resolve?

IMO it's the different way to resolve a name from the OS and PHP.
Try:
echo gethostbyname("host.name.tld");
and
var_export (dns_get_record ( "host.name.tld") );
or
$dns=array("8.8.8.8","8.8.4.4");
var_export (dns_get_record ( "host.name.tld" , DNS_ALL , $dns ));
You should found some DNS/resolver error.

Your "localhost" cannot resolve the name www.google.com, which means your machine doesn't/can't reach a valid dns server.
Try ping google.com on the console of that machine to verify this.

It is more flexible to use curl instead of fopen and file_get_content for opening a webpage.

getaddrinfo failed: No such host is known
The above error is caused due to mistake in Database Host (DB_HOST) in .env file
Open the .env file and Specially check for this env variables
DB_HOST=localhost
DB_DATABASE=database_name
DB_USERNAME=database_username
DB_PASSWORD=database_password

Your target domain might refuse to send you information. This can work as a filter based on browser agent or any other header information. This is a defense against bots, crawlers or any unwanted applications.

I had a lot of issues with this on Windows. I just performed git clone, copied my .env.example file to .env. updated it, and executed my trusty PHP script to update initial database used for a project. And it failed, and whatever I did, it kept on failing. Out of desperation, I reinstalled MySQL, but that didn't fix things.
Finally, I figured it out. PHP script extracted the settings from .env file using file_get_contents(), and exploding it using \n, but the .env file was encoded with CRLF, so my DB_HOST was empty. After I changed the encoding of the .env file, everything was fine.
So, my point is, echo the parameters supplied to mysqli::construct, and it might be the culprit.

A weird thing I found was that the environment variable SYSTEMROOT must be set otherwise getaddrinfo() will fail on Windows 10.

What had caused this error on my side was the following line
include_once dirname(__FILE__) . './Config.php';
I managed to realize it was the culprit when i added the lines:
//error_reporting(E_ALL | E_DEPRECATED | E_STRICT);
//ini_set('display_errors', 1);
to all my php files.
To solve the path issue i canged the offending line to:
include_once dirname(__FILE__) . '/Config.php';

Related

Symfony 5 - SQLSTATE[HY000] [2002] No such file or directory

I'm running a Symfony 5 project on my Ubuntu machine, and when I try to import my database I had created to my project I get the following error:
The thing is I'm going through a tutorial a second time to create my own project, and the first project works just fine. The DATABASE_URL in the .env files on these two project are the same (double and triple checked),
here's the .env
I've tried changing localhost to 127.0.0.1 but the error changes to 'Connection refused', and I also tried adding the real path to the mysql.sock to the php.ini. These are the solutions I've found that apply to my problem, nothing worked so far.
Any ideas would be greatly appreciated!!
Have you defined a path to your socket in the config/packages/doctrine.yaml? See the marked answer here
I think that setting localhost to 127.0.0.1 fixes your initial problem, but then you are getting connection refused as the server isn't accepting your credentials.
If you aren't using a socket, then you need to pass a password in to your DATABASE_URL:
DATABASE_URL=mysql://root:password#127.0.0.1:3306/beers?serverVersion=5.7
This problem sometimes occurs, because on Linux (unix) machines, we need to indicate unix_socket="value". You should find "mysql.sock" file in your local server folder and copy/paste the path as its value.
e.g. on my distant Gandi server this file is called "mysqld.sock" and lives in /srv/run/mysqld/ .
So, the distant .env is : DATABASE_URL="mysql://root:password#localhost/db_name?unix_socket=/srv/run/mysqld/mysqld.sock"
On my local ubuntu this file lives in /opt/lampp/var/mysql/mysql.sock . In my local .env file : DATABASE_URL="mysql://root:password#localhost/db_name?unix_socket=/opt/lampp/var/mysql/mysql.sock"
Hope, it helps someone having the same issue.

PHP: Warning: file_get_contents

I am no coder at all. Started learning html and php and bumped into a problem. There a few similar questions, but with my skill, I am not able to implement solutions.
$url = "https://www.bitstamp.net/api/ticker/";
$fgc = file_get_contents($url);
$json = json_decode($fgc, true);
$price = $json["last"];
$high = $json["high"];
$low = $json["low"];
$date = date("m-d-Y - h:i:sa");
$open = $json["open"];
After launching XAMPP on my machine and uploading php file, I get returned:
Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution in /opt/lampp/htdocs/crypto_ticker/api.php on line 5
Warning: file_get_contents(https://www.bitstamp.net/api/ticker/): failed to open stream: php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution in /opt/lampp/htdocs/crypto_ticker/api.php on line 5
Would someone please let me know what does that mean? Inability to connect to the page? The page loads itself perfectly fine, so must be something missing on my side.
And what do I do then?
Thank you in advance
Based on the error (Temporary failure in name resolution), it appears to be an issue with the PHP server accessing the website (bitstamp.net). While your web browser might be able to access it, the PHP service on the XAMPP server may not be able to communicate properly with it.
Try running nslookup www.bitstamp.net to ensure that a response is, in fact, provided. If it does resolve, try ensuring your connection is not incorrectly passing through a proxy and ensure your DNS settings are correct.
Based on other responses (https://stackoverflow.com/a/6361464/9549215), ensuring your ServerName directive in your Apache2 Virtual Host is correctly set MAY solve the issue; I have not tested this.
Best of luck with your problem!
You can see the exact error in your output.
Temporary failure in name resolution
so you can't resolve the hostname "www.bitstamp.net". So you should check if you server has the correct DNS settings and can resolve the domain. You can use nslookup or dig to check it.

oci_connect fails from command line but works in browser

I wrote a PHP script to run as a cron job and it wasn't running. The script worked fine in the browser, but I discovered when I ran it from the command line that I got this error:
Warning: oci_connect(): ORA-12154: TNS:could not resolve the connect identifier specified in /usr/apps/webdata/cron/PropogateDB.php on line 190
The line in question is:
$conn_NRB = oci_connect($user, $pass, "nrb.njbbnrbpd1");
As I said this script works fine in the browser.
I did some digging around and found the configuration settings for the database. I then tried this:
$conn_NRB = oci_connect($user, $pass, "(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SID=nrb)))");
With this code both the command line and the browser give me this error:
Warning: oci_connect() [function.oci-connect]: ORA-01017: invalid username/password; logon denied in /usr/apps/webdata/cron/PropogateDB.php on line 191
As far as I know there are no other usernames and passwords that I can use. (I'm trying to verify that now.)
I searched around on this site and discovered someone else with a similar problem, although his problem was reversed, and his cause was that the command line and the browser pointed to two different php.ini files. I just confirmed that both the browser and the command line invoke the same php.ini file. They both point to:
Configuration File (php.ini) Path => /usr/local/php/lib
Is there any other reason why a script would work in the browser but fail in the command line? I'm relatively new to Oracle SQL and I don't have direct access to the database so I'm not sure where to go from here.
Thanks.
The 'EasyConnect' string
(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SID=nrb)))
refers to localhost. 'In the browser' likely means: on the server (where the PHP script executes).
Unless you run the test on the command - line of the same server, you should change the connect string to refer to the Database server (which might be simply the host name of your web-server).
Beware of firewall restrictions that may come in the way. Prefer to use your company's tnsnames.ora to resolve database instances identifiers to their connect strings, if possible.

Having troubles configuring PHP to work with mySQL

I'm venturing out of the world of .NET and into the world of open-source. However, I've hit a few roadblocks while trying to get my development environment set up. And I'm kind of stuck on the most recent one.
I have installed: Apache 2.2, php 5.3.2, and mySQL 5.1.48
everything is working pretty much; apache is serving up PHP pages, and I'm able to create databases and tables in mySQL, however, I can't seem to get php to communicate correctly with mySQL. My php.ini has these lines un-commented:
extension=php_mysql.dll
extension=php_mysqli.dll
However, I keep getting this error message:
Warning: mysqli_connect(): (HY000/2002): 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:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\mysql_test.php on line 15
any help would be greatly appreciated.
(btw, the php code runs properly when I put it on my web host)
This is most likely a configuration problem with the MySql server. It sounds like it either isn't listening on the correct port or something else is going on with the connection. You have the extensions loaded correctly because PHP is able to find the mysql functions. I would make sure you don't have a firewall blocking any of the requisite ports (actually, just try turning off your firewall and see what happens...just don't forget to turn it back on ;-) ). Also make sure if you are following a tutorial you didn't deviate from the MySQL setup instructions at all. If those don't work, post an update and we can try again.
This looks like bug #45150 : MySQL functions cannot be used with 5.3.x on Vista when using "localhost" -- I've had this problem once, and it took me some time to figure out what was causing it...
If you are working with Windows Vista (and possibly seven ?), and trying to connect to MySQL using "localhost" as host, try to replace that by the corresponding IP address : "127.0.0.1"
Or try to edit the hosts file, and to un-comment the line that corresponds to localhost in IPv4 :
127.0.0.1 localhost
(Remove the # at the beginning of the line)
Or to comment the line that corresponds to localhost in IPv6 :
#::1 localhost
(Add a # at the beginning of the line)
And here's an interesting article about that : PHP 5.3 and MySQL connectivity problem
Use XAMPP - it'll remove any headaches like this for local development.
XAMPP is a very easy to install Apache Distribution for Linux, Solaris, Windows and Mac OS X. The package includes the Apache web server, MySQL, PHP, Perl, a FTP server and phpMyAdmin.

imagecreatefromjpeg() error - Why is my image manipulation script failing?

I am currently using an image manipulation script to do some work on uploaded images and I'm running into a problem in my dev environment. This same code works on my production server.
The error is:
PHP Warning: imagecreatefromjpeg(): php_network_getaddresses:
getaddrinfo failed: Name or service not known in /path/to/script.php
on line 75
The line of code on 75 is:
$this->img = imagecreatefromjpeg(PHPTHUMB."?src=/".$this->image_path);
which creates an image that is loaded from another made by phpThumb, which is used for further manipulation.
Any ideas on how to solve this? Can anyone shed some light on what the error means?
Thank you,
Edit:
Just as a further bit of insight, if i visit PHPTHUMB . "?src=/" . $this->image_path in my browser, the image loads fine
User agent in php.ini did not solve the problem as indicated here
EDIT (SOLUTION): I had to add the INTERNAL 192.168.204.XXX IP into the hosts file, so that http://dev.mysite.com resolved correctly. Trying both 127.0.0.1 and the external IP yielded no result, but the internal works perfectly. Thanks for everyone's efforts,
I'm totally not sure about the cause of that error, but this is my best guess.
There is a particular PHP Setting which enables you to treat URLs as files. Normally imagecreatefromjpeg accepts a filename, I think. Since you are passing a URL, you need to make sure the particular setting is enabled. I believe you can find more info about it here: http://us2.php.net/file
filename
Path to the file.
Tip
A URL can be used as a filename with this function if the fopen
wrappers have been enabled. See
fopen() for more details on how to
specify the filename and List of
Supported Protocols/Wrappers for a
list of supported URL protocols.
Fopen wrappers: http://us2.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen
Your dev environment may not be setup with this, but your production environment could be.
Edit
This page lists your error:
http://bugs.php.net/bug.php?id=11058
and mentions that a possible fix is by modifying your hosts file to explicitly include the ip/dns of the server you're accessing.
i tryed to reconfigure my named
configuration, without any result, to
fix this. the problem was solved by
adding the following line to my
/etc/hosts:
194.97.55.147 mx.freenet.de
Maybe you can try this?
did you also check that allow_url_fopen is set to On in php.ini?
I saw that they did mention that in the article you referred to in your post.
EDIT: Also, what OS and what PHP version are you running?
Just read this on php.net: "On Windows versions prior to PHP 4.3.0, the following functions do not support remote file accessing: include(), include_once(), require(), require_once() and the imagecreatefromXXX functions in the GD Functions extension."
EDIT2:
I just read what was written in the comments, your allow_url_fopen is set to On, sorry about that.
But OS, PHP version and phpinfo() would help in finding the problem.
The error message suggests something is wrong at a networking level. Thus, if possible, bypass that. Try using the path to the thumbnail generator, instead of a network address.
If that's not an option, try something that will give you more granular error messages. I suggest CURL. At the very least, it should yield a more informative error message.
The simple fact that this is failing is because on your webserver, your DNS resolution is broken.
If you have SSH access to your server, try the following command
dig hostname
where hostname is the host name from PHPTHUMB
This will probably fail.
Assuming that you're using Linux, my suggestion would be to edit your /etc/resolv.conf (as root)
and add the following line before any other lines beginning with "nameserver"
nameserver 4.2.2.2
This should hopefully fix things, though you may have to restart apache (or whatever webserver you're using)!
The issue here is that php fails to resolve the url that you provide as PHPTHUMB as stated in this bug report on php.net. This possibly means that it's the fault of the operating system/web server, in which case you can enter the ip address of the server you are connecting to into your hosts file manually.

Categories