How to open TCP connection in PHP and send some String over that connection ( for example "test") ?
You can create a socket with socket_create, open it with socket_connect and write with socket_write. socket_write documentation on php.net
You can try the example from this link:
<?php
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: www.example.com\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
?>
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 was want to connect to my fingerprint device useing TCP/IP
i was setting ip address in device,
my php code:
<?php
$fp = fsockopen("192.168.1.211", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: www.example.com\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
?>
when i try to connect, i get error connection refused . but ping to device is work, i don't know where is the problem,
My fingerprint device specification:
Vendor :Solution
type : x101-c
i hope somebody can help and solve my problem,,
i really stack now....
but ping to device is work
Ping uses ICMP packets not TCP. If you're getting a connection refused then the (TCP) port is firewalled.
I'm writing a proxy server in PHP. The proxy server works and is able to render HTML, but JavaScript code will not work. Based on research I've done, many other proxy servers also have problems with JavaScript.
Here is the code I'm using.
#set_time_limit(60);
$fp = fsockopen(".$page.", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: ".$hostname."\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
$x=0;
while (!feof($fp)) {
echo fgets($fp, 128);
fclose($fp);
}
}
Help would be greatly appreciated.
I have a Pagerank checking script that i'd like to add proxies to. I usually use Curl so im not sure exactly how to go about making the request through a proxy. I want the request to pick a random proxy from the $proxies array and use that for the request. Can anyone walk me through how this is done using this script. Thanks in advance.
$proxyauth = "username:password";
$proxies = array(
"proxy1",
"proxy2",
"proxy3",
"proxy4",
"proxy5",
"proxy6",
"proxy7",
"proxy8",
"proxy9",
"proxy10"
);
function check($page){
// Open a socket to the toolbarqueries address, used by Google Toolbar
$socket = fsockopen("toolbarqueries.google.com", 80, $errno, $errstr, 30);
// If a connection can be established
if($socket) {
// Prep socket headers
$out = "GET /tbr?client=navclient-auto&ch=".$this->checkHash($this->createHash($page))
."&features=Rank&q=info:".$page."&num=100&filter=0 HTTP/1.1\r\n";
$out .= "Host: toolbarqueries.google.com\r\n";
$out .= "User-Agent: Mozilla/4.0 (compatible; GoogleToolbar 2.0.114-big; Windows XP 5.1)\r\n";
$out .= "Connection: Close\r\n\r\n";
// Write settings to the socket
fwrite($socket, $out);
// When a response is received...
$result = "";
while(!feof($socket)) {
$data = fgets($socket, 128);
$pos = strpos($data, "Rank_");
if($pos !== false){
$pagerank = substr($data, $pos + 9);
$result += $pagerank;
}
}
// Close the connection
fclose($socket);
// Return the rank!
return $result;
}
}
With fsockopen, instead of toolbarqueries.google.com you connect to the selected proxy. Then you send a complete URL with the GET request:
$socket = fsockopen("proxy5", 80, $errno, $errstr, 30);
...
$out = "GET http://toolbarqueries.google.com/tbr?client=..."
You can still send the Host header, but you don't need it.
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