php file_get_content /curl no response - php

i am trying to use googles geoloacating api with php: file_get_content.
This works fine on my testsystem but it wont work live. The code is excatly the same and the servers are also the same. I got some errormails like:
file_get_contents(): php_network_getaddresses: getaddrinfo failed:
Name or service not known
allow_url_fopen is available and curl is also available.
I alsso tryed the same with curl but the result was the same live didnt work.
Hope some of you can give me some tips.

Related

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.

Guzzle / curl can't resolve host

I'm trying to run both my API and my client in the same Vagrant VM. In the client I'd like to use Guzzle. When I try to set up a simple test, I get the following error from curl:
Fatal error: Uncaught exception 'GuzzleHttp\Exception\RequestException' with message '[curl] (#6) See http://curl.haxx.se/libcurl/c/libcurl-errors.html for an explanation of cURL errors [url]
When I use a Github url instead, it all works fine. One thing I'm sure of, is that there is no typo in my url.
I have both client and API pointing to the ip-address of my VM, and both run fine separately.
I also ran into a topic on which it was suggested to use a cacert.pem certificate in php.ini, which I have tried, but it didn't work.
Any that knows how to solve this?
Stupid of me. I had to place 127.0.0.1 api.dev in the hosts file on my VM.
The error number 6 (CURLE_COULDNT_RESOLVE_HOST) means that libcurl failed to resolve a host name to an IP address.

GAE php "urlfetch error"

i want to get a webpage content via my gae php application.
but the code doesn't work.
my code is:
echo file_get_contents("http://www.navyfield.com", false);
and i tried my code on php-minishell,http://php-minishell.appspot.com/
it didnt work either.
here is the result
Google App Engine/1.8.7
PHP 5.4.19
>>> file_get_contents("http://www.navyfield.com", false);
file_get_contents(http://www.navyfield.com): failed to open stream: Fetch error
anyone got idea about this?
Done some checking it looks like that site is blocking the connection. I tried with URL fetch and with a raw socket from our IP range.

php code (getting json results)

i am completely new to php from python. in python, i am fairly familiar with getting json results from webpages. however whatever i seem to do in php is not working. the code i am using is.
<?php
$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=hello&safe=off";
$data = json_decode(file_get_contents($url));
echo $data;
?>`
yet this does absolutely nothing. is there anything wrong with what i am doing? also i ran it through ideone.com and got these errors.
`PHP Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/VQMKwy/prog.php on line 3
PHP Warning:file_get_contents(http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=hello&safe=off): failed to open stream: php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/VQMKwy/prog.php on line 3`
As soon as you'll solve the problem posted above (which seems related to your server configuration, as file_get_contents works fine for me) you'll encounter another error.
json_decode will return an object of class stdClass. You can't just echo that out.
The fastest way to echo an array is to use
var_dump($data);
apart from that, the code runs fine on my local machine. I've just replaced echo with var_dump
The hint posted by Rob should solve the problem but if it doesn't and if you're running on Apache try to run this command:
sudo rc.d stop httpd; sudo rc.d start httpd
this will force Apache to stop and start thus updating /etc/resolv.conf

PHP php_network_getaddresses: getaddrinfo failed: No such host is known

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';

Categories