PHP error doing file_get_contents on a Hostinger host: failed to open stream - php

I'm trying to use the http://ip-api.com/json/ api for get and show the information of a visitant that conects to my website on hostinguer.com.
When I do the next, it should responds me with a json with all the info in a json format:
echo file_get_contents("http://ip-api.com/json/{$user_ip}");
But I get the following error message:
Warning: file_get_contents(http://ip-api.com/json/[MY PUBLIC IP]): failed to open stream: Connection refused in /home/[HOSTINGUER USER]/public_html/ip2.php on line 31
And other strange thing: if I use another API it works correctly and returns me the correct Json! The other API command:
echo file_get_contents("http://ipinfo.io/{$user_ip}/json");
So, I want to use the ip-api.com API cause the results are more accurate, but only works if I use ipinfo.io API... Why can I do the petition to a website and not to the other?
Otherwhise, I tried both in local using curl or writting in my webrowser and it works correctly. Also I tried in local in a Lammp and the both works perfectly. And finally I tried somthing like this post: PHP file_get_contents() returns "failed to open stream: HTTP request failed!" in hostinguer and it doesn't works..
I thought maybe is something in the hostinguer configuration but...
Thanks in advance!

Your IP address is banned. If you're using normal web hosting, your outgoing IP address is shared with other users, who probably did more requests than allowed.
Go to http://ip-api.com/docs/unban and enter your server's outgoing IP address (check it via http://ipinfo.io/json)

Related

Google reCaptcha validation (siteverify) time out

I am trying to validate Google reCaptcha on my website (I am using godaddy server (cpanel)). But when I try to verify server side, it takes too long, and and then I get a time out.
Can anyone point out what is causing the problem?
if(isset($this->data['g-recaptcha-response']) && !empty($this->data['g-recaptcha-response'])){
$u = "https://www.google.com/recaptcha/api/siteverify?secret=".DataSecret."&response=".$this->data['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR'];
$response = #file_get_contents($u);
$arr = json_decode($response,true);
}
I get this error message:
Warning (2): file_get_contents(https://www.google.com/recaptcha/api/siteverify?secret={XXXXXXXXXX}&response={XXXXXXXXXXXX}&remoteip=xxx.x.xx.xx): failed to open stream: Connection timed out
NOTE: On local host (which is on the same server), file_get_contents is working. I just have issue with recaptcha.
#Yogesh Saroya,
Have you checked allow_url_fopen=On, allow_url_include=On in your server settings? I think these two settings are required.

Integrate IP Restricted API on PHP

I am integrating an IP-Restricted API from a company called XendPay. They allowed requests from the Server IP only. I know PHP scripts run on server but I am not getting response when I request data.
Here is an example:
$jsonRequestURL = 'http://uat.xendpay.com/partner/api/1.0/{partner_key}/quote?countryFrom=PL&countryTo=GB&currencyFrom=PLN&currencyTo=GBP&deliveryType=BANK_TRANSFER&paymentMethod=banktransferlocal&amount=1000.00&amountCurrency=PLN&indent=true';
json_decode ( file_get_contents ( $jsonRequestURL ) );
It sometimes stuck here or after waiting quite long, gives the following error:
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.
I am not sure but I assume that request is not being sent through Server IP as I have called above mentioned json_decode() on pageload.
Can anyone suggest what should I do?

Connection time out while reading contents from a server

Hi Here's what i am trying to do .
$contents = file_get_contents('http://123.34.56.123:3000/?location=Example'); //Not the original ip
//Node script running on port 3000
Everything is working fine on localhost and when i access this url directly from browser but on my web server i ma getting this error
file_get_contents(http://123.34.56.123:3000/?location=Example) Failed to open stream: Connection timed out in ....
And i have tried connecting with curl , sockets and file stream but everything getting the connection is timing out though it's working fine on my local server
I have also used ini_set('max_execution_time', 300); but it didn't help anything
Note : file_get_contents('http://www.google.com'); Works fine on the web server
Anyone can help me understand why i am getting this error ? i have tried almost everything i could to fix this issue.
First of all you need to check that whether the content which you are fetching from resource is encoded or not, means if the content is encoded with JSON then you need to use json script to decode it and after that you will be able to be get the content..
also check the resource URL and the port of the server that you are using.
If your port 3000 is blocked then it also not works fine.
I though it will help you.
Seems like the server i was using was blocking connection on port 3000 , and it makes complete sense as normally web host use 80 and 443 port only to make i/o connection . I contacted with my web host they unlocked the 3000 port and it's working perfectly now .

file_get_contents - failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found

I'm having some weird problems with file_get_contents after moving my site to a new domain. I had to set up a new domain and IP address (using Plesk) to get a new ssl certificate working. Now my file_get_contents calling a script on the same domain is giving me this:
failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
If I call the same url using file_get_contents on another server it works fine, and if I call www.google.com from the server thats failing that works, so it only seems to be if I call a url on the same sever!
I have a feeling it might have something to do with having two IPs with two different ssl certificates on the one server, when i file_get_contents / (index page) of the server from the server I get the plesk 'this is a new domain' page so its like apache isnt looking up the right virtual host when its called from its own sever.
To clarify (hopefully!):
On the server hosting the domain:
file_get_contents('https://mydomain.com?limit=4&offset=0&s_date=2012-02-05&e_date=2012-03-13&order=release_date&dir=desc&cid=12');
gives "failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found"
file_get_contents('http://www.google.com');
works correctly
On another server:
file_get_contents('https://mydomain.com?limit=4&offset=0&s_date=2012-02-05&e_date=2012-03-13&order=release_date&dir=desc&cid=12');
works fine.
I have tried turning ssl off and I still get the same problem.
I've had this problem too, when I working on a little test server at home. The domain name is resolved to your external IP address, and a request is sent. But because the request is coming from inside your network, the router doesn't recognise it as a normal request. It probably has a web interface for configuring it, and tries to return a page from its own management system, which is then not found at the path you specified.
In that case, I was working on a Windows PC, and I could solve it by adding the domain I was testing to my hosts file, specifying 127.0.0.1 as the IP-address (or the IP-address of the server, if it is another machine within the same network). In Linux there should be a similar solution, I think.
The problem isn't PHP or your server, but your router.
If you just need to handle the warning when the URL is not found (as I did), you may just do this to turn Warnings into Exceptions:
set_error_handler(
function ($err_severity, $err_msg, $err_file, $err_line, array $err_context) {
// do not throw an exception if the #-operator is used (suppress)
if (error_reporting() === 0) return false;
throw new ErrorException( $err_msg, 0, $err_severity, $err_file, $err_line );
},
E_WARNING
);
try {
$contents = file_get_contents($your_url);
} catch (Exception $e) {
echo $e->getMessage();
}
restore_error_handler();
Solution based on this thread/question.
Most hosting provides now block the furl_open parameter which allows you to use file_get_contents() to load data from an external url.
You can use CURL or a PHP client library like Guzzle
Try to do this :
file_get_contents('https://mydomain.com?'.urlencode('limit=4&offset=0&s_date=2012-02-05&e_date=2012-03-13&order=release_date&dir=desc&cid=12'));
I got the same error in CodeIgniter 3. I was doing like this
file_get_contents(base_url('database.json'));
and then this
file_get_contents(site_url('database.json'));
My problem get resolved after I changed it to this
file_get_contents(__DIR__.'/database.php');
Reason behind this was, I was trying to get the internal resource from external url which these methods base_url and site_url return. While __DIR__ return internal url.

file_get_contents failing

I have a blog on tumblr, and I'm trying to access it via php and tumblrs api.
the api endpoint is:
http://blog.example.com/api/read/json?start=0&num=10&debug=1
and that seems to work fine.
However, when I do:
$result = file_get_contents("http://blog.example.com/api/read/json?start=0&num=10&debug=1");
print_r($result);
the browser outputs
Warning: file_get_contents() [function.file-get-contents]: php_network_getaddresses: getaddrinfo failed: Name or service not known in /path/to/file/test.php on line 5
Warning: file_get_contents(http://blog.example.com/api/read/json?start=0&num=10&debug=1) [function.file-get-contents]: failed to open stream: php_network_getaddresses: getaddrinfo failed: Name or service not known in /path/to/file/test.php on line 5
This also fails with Curl.
Ideas?
I think this has something to do with the fact that my blog is on a subdomain. The reason I say this, is, if I run the same script on a blog located on the root url, it works fine. Is there a correct way to escape subdomains?
This code is failing because the DNS lookup is failing. This could be because your server is having trouble contacting its name servers. Or its because blog.example.com doesn't exist (it fails in my browser so it should fail everywhere). You could try changing your name servers to google's: 8.8.8.8 and 8.8.4.4. These DNS servers work well.
New Tumblr API: http://www.tumblr.com/docs/en/api/v2
If you have your domain with your tumblr account it will work. An example

Categories