I connect to an application for a push/stream data.
With the following code I have a right answer from the app (outcome=OK|item=THEITEM) but can't figure out how to get the var "last_value" and its updates.
The loop is commented out, I have no echo with it (loop)
$port = ('5333');
$address = ('127.0.0.1');
$fp = stream_socket_client("tcp://$address:$port", $errno, $errstr, 1);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$in = "function=subscribe|item=THEITEM|schema=last_value";
fwrite($fp, $in."\n");
//while (!feof($fp)) {
echo fgets($fp, 1024);
//}
fclose($fp);
}
This returns outcome=OK|item=THEITEM.
The updates should be like THEITEM|last_value.
Related
Please, help me to connect WebSocket with php.
I try this code, but I've got 400 Bad Request
$fp = fsockopen("cs.money", 443, $errno, $errstr, 30);
if (!$fp)
{
echo "$errstr ($errno)<br />n";
}
else
{
$out = "GET / HTTP/1.1rnrn";
///Send data
fwrite($fp, $out);
///Receive data - in small chunks :)
while (!feof($fp))
{
echo fgets($fp, 128);
}
fclose($fp);
}
I've solved my problem with js.
var WebSocket = require('ws');
var ws = new WebSocket('wss://cs.money/ws');
I'm working on a Server list viewer
and somebody told me that i should use UdpSocket to send data to the master server and receive data from it (in this case for MW2-IW4 game)
So this is the command or the socket you use
"\xff\xff\xff\getservers IW4 <MASTERSERVERPORTHERE> full empty\x00"
so i tried to work with this code
<?php
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
$msg = "\xff\xff\xff\getservers IW4 <MASTERSERVERPORT> full empty\x00";
$len = strlen($msg);
socket_sendto($sock, $msg, $len, 0, 'MASTERSERVERIP' , 1223);
socket_close($sock);
?>
but didn't receive any data , just getting the number 36 ...
PS:
i don't know much things about sockets.
i figured out how to do it a while ago and remembered this post, if anybody is still interested, this is how you do it:
<?php
$ip = "127.0.0.1";
$port = 1337;
$socket = fsockopen('udp://'.$ip, $port);
stream_set_blocking($socket, 0);
stream_set_timeout($socket, 1); //1 = timeout
fwrite($socket, "YOUR UDP COMMAND HERE\n");
$time=time()+1; //1=timeout
$returned = "";
while($time > time()) {
$returned .= fgets($socket);
}
echo($returned); // the returned value
#fclose($socket); //we are done, so better close it.
?>
Just use fsockopen it's a lot easier,
$fp = fsockopen("udp://127.0.0.1", 13, $errno, $errstr);
if (!$fp) echo "ERROR: $errno - $errstr<br />\n";
fwrite($fp, "\n");
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
I want to read some phrases from an external website which protocol is https.
I've do this for websites with http protocol by this code:
$homepage = file_get_contents('https://www.examlple.com/');
echo $homepage;
but it does not work for https sites. then I used this one:
$fp = fsockopen("https://www.example.com/", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
fwrite($fp, "Data sent by socket");
$content = "";
while (!feof($fp)) { //This looped forever
$content .= fread($fp, 1024);
}
fclose($fp);
echo $content;
}
but I always get an error:
Unable to find the socket transport "http" - did you forget to enable
it when you configured PHP? (2)
actualy the case is to fetch my site's statistics from analytics.
Fsockopen does not know about "http, https". Please remove the http part from your domain and use SSL Port for connection.
See example, this should work:
// open ssl connection - dont add "http or https!"
$host = "ssl://" . "example.com";
$port = 443;
$fp = fsockopen($host,$port);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
fwrite($fp, "Data sent by socket");
$content = "";
while (!feof($fp)) { //This looped forever
$content .= fread($fp, 1024);
}
fclose($fp);
echo $content;
}
Hi I'm trying to write a client-server function. I've written part of the code from the client side asking the server to login, I now need some simple code to start me off that I could use as a response from the server.
Client code:
<?php
$fp = fsockopen("XXXX.XXXXX.com", 1980, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "<UserStatus timestamp='0' type='login' UserID='####' key='XXXX' /> \r\n";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
?>
Please can some give me an example of some code.
i need to telnet to a port and send command and write the output into a txt file using PHP.How i do it?
in this forum have a same question name telnet connection using PHP but their have a solution link and the solution link is not open so i have to make the question again.
Also i try the code below from php site but it does not save the proper output into a text file.Code:
<?php
$fp = fsockopen("localhost", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: localhost\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
?>
So,please help me to solve the problem.How i telnet to localhost port 80 and send command GET / HTTP/1.1 and write the output into a text file?
With a simple additition, your example script can write the output to a file, of course:
<?php
$fp = fsockopen("localhost", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: localhost\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
$output = '';
while (!feof($fp)) {
$output .= fgets($fp, 128);
}
fclose($fp);
file_put_contents( 'output.txt', $output );
}
Then again, I agree with Eduard7; it's easier not to do the request manually and just let PHP solve it for you:
<?php
// This is much easier, I imagine?
file_put_contents( 'output.txt', file_get_contents( 'http://localhost' ) );
You really want to do this with telnet? What about:
echo file_get_contents("http://127.0.0.1:80");
Or if You want to customize the request, you can use cURL - http://php.net/manual/en/book.curl.php