I have a strange thing, fsockopen works fine with PHP 5.5 on WAMP, but fails with PHP 5.6 (also WAMP, same server).
I am using a library, there is this code:
$url = parse_url($uri);
if ('https' == $url['scheme']) {
$host = 'ssl://'.$url['host'];
$port = 443;
} else {
$host = $url['host'];
$port = 80;
}
$fp = fsockopen($host, $port, $errno, $errstr, 30);
if (!$fp) {
throw new WebToPayException(sprintf('Cannot connect to %s', $uri), WebToPayException::E_INVALID);
}
It returns false (empty), $errno is 0, $errstr is empty.
I've checked differences of enabled PHP extensions, both for 5.5 and 5.6 in WAMP, everything is the same. It's same computer, same Apache server, and there are two PHP versions.
I've Googled for this, but couldn't find the solution. It is something PHP configuration related but I couldn't find what it is.
It would be awesome if someone could help :) Thanks!
Related
I am about to write a function which returns true or false. If the function is able to connect to the game server (doesn't need anything further, just to check if it's running) via UDP and a specific ip, it'll return true. If not, it'll return false. Unfortunately, I didn't find anything useful by searching the web.
Edit: For TCP, I'm using this and it's working like a charm.
function loginServer()
{
$ipAddr = "localhost";
$port = 1234;
$fp = fsockopen($ipAddr, $port, $errno, $errstr);
if(!$fp)
{
return false;
}
else
{
fclose($fp);
return true;
}
}
I made this code which is supposed to show whether the program running on the specified port is running.
Now the problem is that it shows "Offline" when my port is clearly open. It however does show online for Google on port 80. What am I doing wrong?
$server = '77.251.97.234';
$port = 43594;
$timeOut = 1;
if ($socket = #fsockopen($server, $port, $errorNo, $errorStr, $timeOut)) {
echo '<span style="color:green;">Online</span>';
} else {
echo '<span style="color:red;">Offline</span>';
}
I'm trying to check service on port 9998. To do so I'm using fsockopen() function.
Like this:
$host = "1.1.1.1";
$port = "9998";
$checkconn = fsockopen($host, $port, $errno, $errstr, 1);
if($checkconn >= 1){
echo 'ok';
} else {
echo "$errstr";
}
It always returns "Connection Timed Out - 110", but the port is open and there is service running there.
If I change the port (like 80), it returns successful, but why not on the 9998 port?
A 1 second connect timeout is not very long. Network lag can be enough to trigger that even when the host/port is valid. I would suggest using at least 2.5 - 5 seconds instead.
Also, you should be checking the result of fsockopen() for FALSE instead of < 1 to know when it fails.
Try this:
$host = "1.1.1.1";
$port = "9998";
$checkconn = fsockopen($host, $port, $errno, $errstr, 5);
if(!$checkconn){
echo "($errno) $errstr";
} else {
echo 'ok';
}
I programmed a website for minecraft servers and I have a problem.
my website is:
Serv-Craft
My host is iPage and on the servers section I check if the server is offline or online.
The problem is that all of the servers that I check are online and the website output that they are online.
I tried to put my website on another host and my code worked
The other site: Serv-Craft Temp Host
What to do??
My code is:
<?php
function GetServerStatus($site, $port)
{
$status = array("<img src='images/icons/off.png' title='Offline'><br>Offline", "<img src='images/icons/online.png' title='Online!'><br>Online");
$fp = fsockopen($site, $port, $errno, $errstr, 0.35);
if (!$fp) {
return $status[0];
} else {
return $status[1];
}
}
print_r(GetServerStatus($server_ip, $server_port));
?>
Please check phpinfo if allow url fopen is activated.
I'm setting up reporting on my PHP script with etsy's open source statsd library.
I'm basing my connection on their given example, but I'm running into an issue where it seems like fsockopen is ignoring try/catch and is printing its errors.
Everything works grand when the statsd server is up and running - but if it's down or the php script cannot connect to it:
Warning: fsockopen(): php_network_getaddresses: getaddrinfo failed:
Name or service not known
I even attempted adding # in front of fsockopen, but no dice. It also seems to be completely ignoring the timeout setting, as it takes about 20 seconds to return the error:
try {
$host = 'stats.thisserverisdown.com';
$port = '8125';
if ( $fp = #fsockopen("udp://$host", $port, $errno, $errstr, 1) ) {
if (!(get_resource_type($fp) == 'stream')) { return false; }
stream_set_timeout($fp,1);
stream_set_blocking($fp,false);
foreach ($sampledData as $stat => $value) {
#fwrite($fp, "$stat:$value");
}
#fclose($fp);
}
return true;
} catch (Exception $e) {
return false;
}
fsockopen is not throwing the error. Because fsockopen has to resolve the hostname you provided, it calls getaddinfo(), which is failing.
Try supplying an IP address, or:
fsockopen( #"udp://$host", ...