file_get_contents hangs - php

I am using Google GeoCoding services.
I have a PHP application which calls the Google Maps API, and receives JSON data.
The function which calls Google Maps host hangs until it times out, but only when I push to Godaddy Virtual Private server.
I have already ssh'd into the server and edited php.ini
I changed "safe mode" to "off"
I get this error message:
Message:
file_get_contents(http://maps.googleapis.com/maps/api/geocode/json?address=xYxY&sensor=false):
failed to open stream: Connection timed out
This works fine in my WAMP server but fails on live server. Any ideas why?

I have found the answer. What has been a week, now? I hope others find this solution. The virtual dedicated servers from GoDaddy are ipv6 enabled, but google maps API is having none of that. So tell Curl to force v4 request, like this:
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
credit is due in part to a blog where I found this information:
http://www.businesscorner.co.uk/disable-ipv6-in-curl-and-php/

Rather than just disabling IPv6, you can try to connect over one IP version and then swap to the other if the first attempt fails. This makes your implementation more robust to temporary routing issues on your and the remote end.
You can get this behavior in file_get_contents() by binding the connection to an interface with either inet6 or inet, and then try the other address family if the first attempt returns FAIL. I wrote up how to make file_get_contents() more routing-robust and dual-stack for anyone that is interested. I also shows you how to force connections to use IPv4 or IPv6 if you prefer to go down that route.

Use curl for getting external data. Many shared servers prevent use of file_get_contents for external data (http://www.php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen) due to security risks.
Plenty of curl examples online.

Check your network. Google doesn't block such request.
Check here.

Related

"Connection Refused" via PHP cURL from GoDaddy Hosting to internally hosted Radarr server's API (jQuery works though?!)

I'm at a complete loss as to why this is failing, so get ready for the novel describing exactly what I'm trying to accomplish and what I've tested so far.
Goal:
I'm building a webpage on my GoDaddy hosting account that I want to connect to my locally hosted Radarr server's API via PHP cURL.
I want to use PHP so I can obfuscate the IP, port, and APIKey on the client side. Plus, some of Radarr's API calls I would like to use cannot be run via query string.
Problem:
No matter what I try, I am getting nothing but the following error:
Failed to connect to <MY PUBLIC IP ADDRESS> port <PORT NUMBER>: Connection refused
(And yes, this is the publicly opened port, not the internal port on the server.)
Setup:
I installed Radarr on a FreeNAS (11.3-U5) jail hosted in my home. This is working beautifully.
I setup the FreeNAS jail with a dedicated IP address instead of allowing it to NAT internally, just to make sure it doesn't cause any issues.
I configured port forwarding on my router to allow external traffic into the Radarr server. This is working. Note: Xfinity is my ISP, but I purchased the router separately.
I have a GoDaddy hosting account where I've written a webpage to tie into Radarr's API on my local network. Currently, the webpage is also working beautifully to query data from the API, BUT it's connecting to Radarr via $.getJSON with JavaScript and jQuery.
(Before anyone says it, yes, I know GoDaddy hosting isn't the best option, but it's the one I'm using.)
Testing:
I swear I've tested EVERYTHING and I don't know why this is failing.
This IS working through the JavaScript/jQuery solution, meaning the Radarr server is up and running, the port is properly forwarded through my router, and GoDaddy's hosting isn't blocking the connection.
I CAN access the raw JSON API response in the browser simply by entering the full API URL call.
And while I'd say I am versed in PHP, I'm certainly no expert, so I even tried pulling a generic PHP cURL script from another StackOverflow post, changing the URL to mine, and running that with no luck. Note: The original script DID work to connect to another external API though.
Then I tried Postman, which worked, and I used Postman to create a PHP script, but that script failed.
All of the functional testing done on my local network was done using my publicly facing IP address. However, just to be certain, I also tried reaching the API through the browser off my network, and that worked just fine.
In a desperate attempt, I also reached out to GoDaddy support to see if they had any ideas. They went as far as creating a test PHP cURL script in the same hosting directory and had it simply pull Google's homepage. That worked just fine.
After GoDaddy was unable to help, I setup a free hosting account and free subdomain on AwardSpace, uploaded the script, and that failed too.
In digging into the Connection Refused error, someone suggested just trying to ping the IP through PHP. I attempted this, but it returned 100% packet loss. Also, I logged into my router and watched the incoming connections as I tested. Using the jQuery solution, I saw the incoming connection and it worked great. Using the PHP solution, it didn't register anything inbound.
Lastly, I have confirmed that the publicly opened port on my router is in the list of blocked port by Xfinity. However, since I can access the server externally through the browser and via the jQuery script, that can't be the issue.
There seem to be many, many, many articles online about the general Connection Refused error, but I haven't found a solution to my problem from any of them.
Does anyone have any suggestions?

Can I change WebSocket from ws to wss? (000webhost)

I had built a website as a dashboard (using 000webhost) to change the data in mySQL database via php which was working fine right now. After that, I have a Unity WebGL build uploaded on itch.io which was working fine as well until it need to get data from the database via php. When I was intended to get the data via accessing php files store in the 000webhost server, I got the following error show up in the web console from the accessing website.
Mixed Content: The page at 'https://.itch.io/' was loaded over HTTPS, but attempted to connect to the insecure WebSocket endpoint 'ws://:54998/'. This request has been blocked; this endpoint must be available over WSS
The link I was using to access the php files was using https instead of http. I am still new to these area, I would like to know can I change the WebSocket endpoint to wss instead of ws? Do I need a paid plan to solve this? I did saw that the Single Hosting plan did offer free SSL certificate. Please help me, I am open for any suggestion/advice, Thank You.
The Websocket Server itself need to setup the SSL Certificate. If it doesnt, you need a reverse proxy like nginx or apache to handle the SSL and forwarding to the unencrypted ws:// websocket.
Anyway, the websocket should run on default port 443/80 as well, since many firewalls are blocking non default ports for security reasons. In this case you will need the reverseproxy if you dont have a dedicated IP for the websocket server.
Use socket.io. Most of the problems are solved and there is no need to do more.
var socket = io.connect('https://www...', {secure: true});

Google Recaptcha check timeout due to IPv6 issue - php on IIS

I'm using google recaptcha v2 on my websites (html / php).
On the server side I am trying to contact google recaptcha to check the user's input:
$response = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$privatekey."&response=".$_POST['g-recaptcha-response']);
The line above is not working. It normally just times out. Occasionally it will work though.
IPv6 is the Issue
It appears to be an issue with the call to google resolving to IPv6 (from https://github.com/google/recaptcha/issues/193)
I have tried turning off IPv6 on my internet connection and that makes it work.
How do I fix this issue properly? I don't think I could turn off IPv6 on the servers (we use some shared hosting as well as a VPS). Is there a way to force it to resolve to IPv4?
Thanks!
Issue was that php could not verify the SSL certificate. The IPv4 / IPv6 was a bit of a red herring I think.
Great post here on that: file_get_contents(): SSL operation failed with code 1. And more

Couldn't open socket connection to server. Error (110): Connection timed out NuSoap

I am using NuSOAP - SOAP Toolkit for PHP to send SOAP request in WordPress Site. But every time I see following error
wsdl error: Getting http://(ServerIPAdress):(Port)/OUGWar/workflow_soap - HTTP ERROR: Couldn't open socket connection to server, Error (110): Connection Times out
I have tried following solution by searching on the internet and specifically on Stack Overflow but still I am not able to resolve this issue.
Things I have tried so far:
Increased the Timeout Time from the API
Open the required ports in my hosting server
Allowed the Server IP in my Hosting Firewall (mean server IP is whitelisted in my hosting firewall where I am sending SOAP Request)
Checked through phpinfo(); that fsockopen(); function is enabled, Safemode is off and all required functions for the api is enabled like fopen(); and other
Verified the WSDL sample with SOAP UI and Its working perfectly there so no problem in creating structure of WSDL Request and calling
Kindly help me if I am still missing something.
I found the answer myself (sharing in case if someone needs it). You need to restart the Fire Wall after white listing IP in the firewall.

Api call not working in online web page

when i trying to call the below api call using the below code:
$json = file_get_contents('http://freegeoip.net/json/'.$_SERVER['REMOTE_ADDR']);
echo $json; exit;
It's giving me response in one hosting.
When i trying the same api in reseller club hosting package site. It's not loading the page as well. It is giving connection time out error.
can any one suggest the solution for this.
Your host may block outgoing HTTP connections - there's nothing you can do about that (other than upgrading to a service plan which allows that).
Also hiding error messages with # is evil (imho)
i talk to the server guy's they resolved the issue. they said that outbound connections to port 80 from my website was blocked in there server which was causing the issue.

Categories