I'm using this PHP library to query a Minecraft server and get some information about it (players online, max players, etc.). It connects to the server via a socket and sends a request for the information, the server sends data back. When I was hosting the code on XAMPP on my personal computer, everything worked perfectly. Then I got a web host (Bluehost) and uploaded all the code and now the request times out every single time. I've used command prompt to ping the server address to ensure that it's up and responding, and it is.
Here's my code:
function pingPlayers($ip) {
try {
$Query = new MinecraftPing($ip);
$response = $Query->Query();
$players = $response['players']['online'];
$maxPlayers = $response['players']['max'];
$online = true;
}
catch(MinecraftPingException $e) {
$players = "0";
$maxPlayers = "0";
$online = false;
var_dump($e);
} finally {
$Query->Close();
}
return array("players" => $players, "maxPlayers" => $maxPlayers, "status" => $online);
}
The MinecraftPing class and all of it's functions can be found here.
I am getting a MinecraftPingException returned, and it says "Failed to connect or create a socket: 110 (Connection timed out)". My first guess was that the host is blocking outgoing requests, but I contacted them and they said nothing is being blocked. I'm not getting any other errors besides the MinecraftPingException.
Related
I am writing a program connecting a web service in PHP to an MQTT broker. The broker is running Mosquitto on a Raspberry Pi.
The idea is to have the web service send a request (a form is submitted) and then send a publish to the MQTT broker and then wait for a reply.
However, the problem is that the loop seems to cause a PHP fatal error due to the fact that it is an endless loop.
I've tried adding the quitstop() function to quit the loop after a message is received, but the program crashes before it reaches that point.
MQTT is still very new to me, but I need to send the request and then keep the loop open until I receive the answer in order to proceed with my program.
This is the code to handle the form submit:
require("phpMQTT.php");
$server = "xxx.xxx.xxx.xx"; // change if necessary
$port = 1883; // change if necessary
$username = "username"; // set your username
$password = "password"; // set your password
$client_id = "phpMQTT-request-1234"; // make sure this is unique for connecting to sever - you could use uniqid()
$mqtt = new phpMQTT($server, $port, $client_id);
$msg = $_POST['box'];
if (!empty($msg)) {
if ($mqtt->connect(true, null, $username, $password)) {
$mqtt->publish("dev/test", $msg, 0);
$mqtt->close();
}
subscribeToTopic($mqtt);
}
function subscribeToTopic($mqtt)
{
$topics['dev/test'] = array("qos" => 0, "function" => "procmsg");
$mqtt->subscribe($topics, 0);
while ($mqtt->proc()) {
}
$mqtt->close();
}
function procmsg($topic, $msg)
{
global $mqtt;
echo $msg;
quitstop($mqtt);
}
function quitstop($mqtt)
{
$mqtt->close();
}
I have a question about pinging certain game servers to see if they are alive or not, most work fine with just your standard PHP ping but others are from steam... and steam is making it more difficult for me to just send a ping to see if it is alive or not, i tried Steam Condenser but... have no idea how to use it since there is no documentation.
my question
how can i ping UDP game servers such as Arma 2, Arma 3, etc. this is what i tried:
$host = '128.0.0.1';
$port = 1234;
$waitTimeoutInSeconds = 7;
if($fp = fsockopen("udp://".$host,$port,$errCode,$errStr))
{
echo 'Online';
}
else
{
echo 'Offline';
}
the problem with that is that i always get "true" returned no matter what IP or port i use, how can i make this work?
EDIT
I also tried
$host = '128.0.0.1';
$port = 1234;
$waitTimeoutInSeconds = 7;
if($fp = fsockopen("udp://".$host,$port,$errCode,$errStr))
{
$write = fwrite($fp,"x00");
if (!$write)
{
echo 'offline';
}
else
{
echo 'online';
}
}
else
{
echo 'offline';
}
fclose($fp);
however with this one i get error code 0 which seems to be a problem with the initializing of the socket?
I tried this one, by removing the "udp://" :
$host = '128.0.0.1';
$port = 1234;
$waitTimeoutInSeconds = 7;
if($fp = fsockopen($host,$port,$errCode,$errStr))
{
echo 'Online';
}
else
{
echo 'Offline';
}
And it works. Here is the return text :
Warning: fsockopen(): unable to connect to 128.0.0.1:1234 (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. ) in C:\xampp\htdocs\test.php on line 6
Offline
It don't think you should put the "udp://" because just using the port number is enough.
What about using the OS's ping utility, execute terminal ping command using PHP. You can read it here : http://php.net/manual/en/function.shell-exec.php
I've been trying to connect to a Twitch chat via IRC. I've added an echo function to test where I had connected or not as that page was blank and an account didn't join the IRC.
Here is my code:
<?php
set_time_limit(0);
ini_set('display_errors', 'on');
$datatwitch= array(
'server' => 'irc.twitch.tv',
'port' => 6667,
'nick' => 'greatbritishbgbot',
'name' => 'greatbritishbgbot',
'pass' => 'oauth:HERE',
);
?>
<?php
//The server host is the IP or DNS of the IRC server.
$server_host = $datatwitch['server'];
//Server Port, this is the port that the irc server is running on. Deafult: 6667
$server_port = $datatwitch['port'];
//Server Chanel, After connecting to the IRC server this is the channel it will join.
$server_chan = "#greatbritishbg";
//login password
$nickname = $datatwitch['name'];
$nickname_pass = $datatwitch['pass'];
//Ok, We have a nickname, now lets connect.
$server = array(); //we will use an array to store all the server data.
//Open the socket connection to the IRC server
$server['SOCKET'] = #fsockopen($server_host, $server_port, $errno, $errstr, 2);
if($server['SOCKET'])
{
//Ok, we have connected to the server, now we have to send the login commands.
echo "connected";
SendCommand("PASS $nickname_pass \n\r"); //Sends the password not needed for most servers
SendCommand("NICK $nickname\n\r"); //sends the nickname
SendCommand("USER $nickname USING PHP IRC\n\r"); //sends the user must have 4 paramters
while(!feof($server['SOCKET'])) //while we are connected to the server
{
$server['READ_BUFFER'] = fgets($server['SOCKET'], 1024); //get a line of data from the server
echo "[RECIVE] ".$server['READ_BUFFER']."<br>\n\r"; //display the recived data from the server
/*
IRC Sends a "PING" command to the client which must be anwsered with a "PONG"
Or the client gets Disconnected
*/
//Now lets check to see if we have joined the server
if(strpos($server['READ_BUFFER'], "422")) //422 is the message number of the MOTD for the server (The last thing displayed after a successful connection)
{
//If we have joined the server
SendCommand("JOIN $server_chan\n\r"); //Join the chanel
}
if(substr($server['READ_BUFFER'], 0, 6) == "PING :") //If the server has sent the ping command
{
SendCommand("PONG :".substr($server['READ_BUFFER'], 6)."\n\r"); //Reply with pong
//As you can see i dont have it reply with just "PONG"
//It sends PONG and the data recived after the "PING" text on that recived line
//Reason being is some irc servers have a "No Spoof" feature that sends a key after the PING
//Command that must be replied with PONG and the same key sent.
}
flush(); //This flushes the output buffer forcing the text in the while loop to be displayed "On demand"
}
} else {
echo "connection failed";
}
function SendCommand ($cmd)
{
global $server; //Extends our $server array to this function
#fwrite($server['SOCKET'], $cmd, strlen($cmd)); //sends the command to the server
echo "[SEND] $cmd <br>"; //displays it on the screen
}
?>
It appears I can't Get passed if($server['SOCKET']). Is there anyway I can diagnose this? As I have directly connect with the details in hexChat.
The server had actually been preventing me to use fsocket. After contacting my host and moving away from a shared host it started to work.
Please take a look at this answer: https://stackoverflow.com/a/24835967/1163786
Especially:
$socket = fsockopen($bot["Host"], $bot["Port"], $error1, $error2);
if(!$socket) {
echo 'Crap! fsockopen failed. Details: ' . $error1 . ': ' . $error2;
}
To get the details about the socket error = reason for socket not connecting.
Currently, you have $errno, $errstr on fsockopen, but echo only "connection failed".
I have some code that uses PHP and LDAP to connect to AD:
$host = 'ldap://stack.overflow.com';
$port = 389;
$username = 'stackOverflow';
$password = 'IaMP4ssWord';
$dn = 'CN=Users, DC=STACK, DC=OVERFLOW, DC=COM';
$cond = '(&(objectcategory=user)(displayname=*))';//All users that have a displayname
if($ldap = ldap_connect($host, $port))
{
if(ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3))
{
if(ldap_bind($ldap, $username, $password))
{
$attrs = array('displayname', 'mail');
if($rs = ldap_search($ldap, $dn, $cond, $attrs))
{
$results = ldap_get_entries($ldap, $rs);
echo "<pre>";print_r($result);echo "</pre>";//Print the results
}
}
else
{ echo 'Binding failed';}
}
else
{ echo 'Setting options failed';}
}
else
{ echo 'Connection failed'; }
Now this code works just fine. It print out every user that has a displayname in AD.
Problem is for the username/password binding i am using my own user credential to bind to the server.
I would like to know if there is a way to bind using the servers credentials.
I am setup using PHP 5.3 + IIS on windows server 2008 R2 for both the server with IIS and the one that has AD.(two different VM).
I also know that IIS has a AD account named IISStackOverflow but I don't know the password or even if it has a password...
Thanks!
Oh! I tried changing $username to IISStackOverflow and $password to ''
But it gave invalid credential error.
--EDIT--
Do I have to do the binding part at all? (If I am only reading data)
As you run it from server itself, and you just want to read I would try to use :
...
if(ldap_bind($ldap))
...
According to PHP documentation if bind_rdn and bind_password are not specified, an anonymous bind is attempted.
Then if your anonymous logon is refused (this should not be, because running under IIS on the server your code is at least executed as a domain user) you will find there how to enable anonymous LDAP binds to Windows Server. This used to work forme on W2K8, Inever test it on W2K12.
I am using a fairly straightforward php class emailtodb to import emails to a mysql database. Everything works perfectly as hoped when I use it to check email accounts on the local server. The issue is I took the connection script and put it on another server and now it cant connect. To summarize hopefully what im trying to say:
server1.com -accessing-> myemail#server1.com = works perfect
server2.com -accessing-> myemail#server1.com = Warning: imap_open() [function.imap-open]: Couldn't open stream
This is the code im using to connect to the server
$mysql_pconnect = mysql_pconnect($cfg["db_host"], $cfg["db_user"], $cfg["db_pass"]);
if(!$mysql_pconnect){echo "Connection Failed"; exit; }
$db = mysql_select_db($cfg["db_name"], $mysql_pconnect);
if(!$db){echo"DB Select Failed"; exit;}
$edb = new EMAIL_TO_DB();
$edb->connect('mail.MYSERVER.com', '/pop3:110/notls', $email, $pass);
$edb->do_action();
which actually goes to this function to actually connect (I believe)
function connect($host, $port, $login, $pass){
$this->IMAP_host = $host;
$this->IMAP_login = $login;
$this->link = imap_open("{". $host . $port."}INBOX", $login, $pass);
if($this->link) {
$this->status = 'Connected';
} else {
$this->error[] = imap_last_error();
$this->status = 'Not connected';
}
}
Finally I would say there is one major difference between the two servers, the new server has an SSL where the first does not so the new mail connection is going from HTTPS to HTTP but I dont know if that would have anything to do with it.
I was playing with this script just this evening. Have you tried something like this? (assuming you're switching from POP3 to IMAP with SSL)
$edb->connect('mail.server.com:993', '/imap/ssl/novalidate-cert', 'myname#server.com', 'password');
I discovered that my PHP environment didn't have PHP complied with IMAP-SSL (--with-imap-ssl in phpinfo() ), so that was causing some early problems for me too.