Connection time out while reading contents from a server - php

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 .

Related

PHP cannot connect to MySql database on live web server

$con=mysqli_connect('localhost:3306','Punya','password','saain');
My PHP website, which is live is unable to connect to database. It works fine on my Pcs local server. When I write localhost:3306, it executes the echo statement I wrote for debugging.
If I write just localhost, it gives:
http 500 error
.
Cannot say much without knowing the actual error. But I could assume a few things based on what you have given.
When you pass localhost as the hostname to the mysqli_connect() function, it tries to create the connection using a Unix socket instead of TCP/IP. So it seems like either you don't have permissions to access the socket, or you need to get the correct socket information from your hosting provider.
Also, when you pass it as localhost:3306, you force it to connect via TCP/IP instead of socket. That's why it works.
However, you can force mysqli_connect() function to connect via TCP/IP by providing the hostname as 127.0.0.1.
$con = mysqli_connect('127.0.0.1','Punya','password','saain');
If this doesn't work, I assume they have defined a different default port for mysql in the php configuration. You can see it by printing it like:
echo ini_get("mysqli.default_port");
In case this default port is different from 3306 (which is unlikely), you can pass the port as the fifth paramter to mysqli_connect() function like this.
$con = mysqli_connect('127.0.0.1','Punya','password','saain', 3306);
You need to contact your hosting provider to get more details on this. Until you do that, your solution is to connect as shown above.
I hope you understand why it happens :) Feel free to ask if you have any doubts.
Try to check in
check here
Synax errors
check in your 👇
file.php
Check what version your live server is running and what version our location machine is running. most of the time environmental factors make these types of errors. I'm going to assume your hosting environment is a shared hosting for this answer
There are couple of things you can do identify the problem first.
Enable Errors
if you are hosted on a cPanel shared hosting server I doubt you can change the php.ini file. what you can do is enable PHP errors on runtime so you will have a proper error than just a 500 error. put the below codes in the top of your PHP file
<?php
error_reporting(E_ALL);
ini_set("display_errors", "On");
PHP info file
create a php.info file to identify what sort environment you are hosting your website on.
<?php
phpinfo();
?>
Cpanel has there own MYSQL server domain and port. this information you should get when you are creating a database in cPanel.
Once you have the error. paste it here we might be able to give you a better answer how to fix it

file_get_contents not working - connection refused

After reading the following links:
get url content PHP
file_get_contents failed to open stream: Connection refused godaddy server with remote connection server
PHP file_get_contents() returns "failed to open stream: HTTP request failed!"
I'm having a problem with the function file_get_contents or even curl in one of my servers at hostgator, they are not working, returning the PHP error failed to open stream: Connection refused. I've tried cURL with USERAGENT set with no result too. It's a simple weather service that I'm creating, it returns the altitude, wind direction, speed and temperature on a certain coordinate of the globe.
Return sample: 30000;221;2;-32;1;
In the other side (request side), I have a web server running IIS 7.5, with all router firewalls, computers firewalls and antivirus softwares disabled only for testing, and is still refusing the connection ONLY for the hostgator servers. I've tried the same code in other web hosting providers, and the code is working properly.
This service will handle a lot of requests per minute, and this seems to me that something has blocked my connection between hostgator and my server due to the number of requests. But I don't know where!!
The page is perfectly accesible via browser.
This is my enviorioment at the hostgator side:
allow_url_fopen: On
allow_url_include: On
OpenSSL: Enabled
Here is my PHP code:
$datalink = "http://#####.########.###:8280/weather.php?waypoint_lat=-10.981925&waypoint_lon=-37.077377&altitude=30000";
$weather_layer = file_get_contents($datalink);
echo "Layer ($datalink):" .$weather_layer."<br>";
Isn't Hostgator blocking the requests because of the DDoS protection? Give them a call, my hosting provider was blocking connections to my other server because they were thinking it was some hacker DDoSing using my hosting.
Also, there might be problem with the port in URL - Hostgator cannot proccess it?

SFTP not connecting to remote server

I am trying to connect to a remote SFTP server using PHP. My code works fine when I connect to a local SFTP account but it times out for remote host. I have made sure through FTP client that host information is correct and its connecting fine.
I am using phpseclib library and my three line code is below.
require_once("phpseclib/Net/SFTP.php");
$sftp = new Net_SFTP('remote_host_IP');
var_dump($sftp->login('<username>', '<password>'));
It returns false (meaning not connected).
What I have done
I have whitelisted script in mod_security just in case its blocking that.
I have tried same script on my local computer and it connects successfully to remote SFTP.
Any valuable hint please?
Do define('NET_SSH2_LOGGING', 2) before initializing Net_SFTP and then do $sftp->getLog() after $sftp->login()
That'll provide enough info with which a diagnostic can be made.
This might help others. You need to make sure TCP_OUT port is open on your remote server to make it work.
Thanks for the help!

"php_connect_nonb() failed: Operation now in progress (115)" happens intermittently

We send some files across to a third party with a PHP cron job via FTP.
However sometimes we get the following error:
ErrorException [ 2 ]: ftp_put(): php_connect_nonb() failed: Operation
now in progress (115) ~ MODPATH/fileop/classes/Drivers/Fileop/Ftp.php [ 37 ]
When I say "sometimes" I mean exactly that; most times it goes across fine but about 1 in 5 times we get that error. It's not to do with the files themselves, because they will go happily if we try again.
We've found similar issues online - relating to a bug in PHP with NAT devices or to do with firewall configuration but again the implication is that if this were the case it would never work.
So, why would this work some times and not others?
ftp_set_option($ftpconn, FTP_USEPASVADDRESS, false);
This line of code before setting passivity of the connection ftp_pasv($ftpconn, true);
Solved my problem
FTP(S) uses random ports to set up data connections; an intermittent success rate indicates that not all ports are allowed by a firewall on the client and/or server machines. The port range for incoming (PASV) data connections can be set in the FTP server.
This page has a nice summary:
The easy way is to simply allow FTP servers and clients unlimited
access through your firewall, but if you like to limit their access to
"known" ports, you have to understand the 4 different scenarios.
1) The FTP server should be allowed to accept TCP connections to port
21, and to make TCP connections from port 20 to any (remote ephemeral)
port.
2) The FTP server should be allowed to accept TCP connections to port
21, AND to accept TCP connections to any ephemeral port as well!
3) The FTP client should be allowed to make TCP connections to port
21, and to accept TCP connections from port 20 to any ephemeral port.
4) The FTP client should be allowed to make TCP connections to port
21, and to make TCP connections to any other (remote ephemeral) port
as well!
So, I'm writing this answer after doing some investigation on my FTP server and reading the link you provided elitehosts.com.
I'm using FileZilla FTP server, and there is a specific setting that I had to enter to make it work. Going into the server settings, there is an area titled "Passive mode settings". In that dialog, there is an area titled "IPv4 specific", and within that area there is a setting labeled "External Server IP Address for passive mode transfers:". It's a radio button selection set, and it was on "Default", but since the FTP server is NAT'ed, I changed that radio selection from "Default" to "Use the following IP:" and entered in the external-facing IP address of my gateway provided by my ISP.
After I set this up, it worked! Not terribly sure if your FTP server is NAT'ed, but I thought I would provide the answer on this thread because it seems related.
In addition to Cees answer, I am running vsftp on ec2 and had to comment out the listen_ipv6=YES, listen=YES then "service vsftpd restart".
Although documentation says it will listen on ipv4 as well it wasn't and this resolved the issue.
For me all I had to do was to remove the ftp_pasv( $ftpconn, true ); and everything worked perfectly. I'm not yet sure why but I am trying to find out and I will surely come back when I do get the reason behind it.
This should be a comment under jj_dev2 comment, but I cannot add one due to reputation. But maybe it will be helpful for someone, so I post it here.
We had the same issue as described in the original post. In our case it worked with many customers - except one.
The solution in jj_dev2 comment did work for us. So we investigated what does ftp_set_option($conn, FTP_USEPASVADDRESS, false) actually do. And based on that we found out that in fact customer's FTPS server was configured incorrectly.
In response to PASV command (ftp_pasv($conn, true)) FTP server returns an IP address which the PHP FTP client then will use for data transfers. In our case the FTP server was returning an internal IP address and not the public IP address that we connect to. Customer had to fix their FTP server settings so FTP server would send external IP address in the PASV command response.

PHP ftp_connect() returns false always except with first used ftp server..!

The problem is ..
I just used ftp_connect() for the first time on my server
<?php
$ftp_connect = ftp_connect('ftp.server.com') or die('<b>FTP Connection Error ...</b>');
$login_result = ftp_login($ftp_connect, 'ftp_username','ftp_password') or die('<b>FTP Login Error ...Check Your UserName and Password</b>');
ftp_pasv($ftp_connect,true);
?>
It worked fine and returned true.Every thing works fine with that ftp server (ftp.server.com)
Then next time I used the same server and everything was fine...BUT
when I tried with another server (say: ftp.server2.com) it returned false,i.e host not found ..!!
I tried with all my friends ftp servers and nothing could connect except that one(ftp.server.com)
what's actually going on ... why is this happening and how can I solve this ... right now i can't connect any ftp server (except my ftp.server.com) ....
Thanx in advance ...
And The above thing works fine on my pc (IIS,PHP 5.3.8)
Updated :
ahhh ....... figured out the problem .... but didn't find a solution.!!!!
the server (here: ftp.server.com) has the same IP which the host server, where the above php is hosted - has.... and soo the ftp_connect() worked on it....
reason is both the servers ftp and http host are from same web hosting provider...
Now its clear, (I think) that firewall is the main reason for all the problem .... how to fix that firewall...?? plz help.... Thanx again..
You are not specifying a port number. PHP's ftp_connect() will use 21 as the default port number if you don't specify one. This script may have worked for the first server because it was running on port 21, but the other servers may be failing because they're running on different ports. Ask your friend(s) what port they're using, or use something like nmap to find it yourself.

Categories