PHP connect to external ip with port - php

I have a dedicated server running a MSSQL and an apache server, only listening to port 8080 (the apache), and i have some XML in there that i want to access from another domain(hosted server).
I tried fsockopen like this:
<?php
$fp = fsockopen("199.175.125.222", 8080, $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);
}
?>
but got nothing, only a:
Warning: fsockopen() [function.fsockopen]: unable to connect to 199.175.125.222:8080 (Connection timed out) in /home/reb/public_html/testing/index.php on line 2
Connection timed out (110)
php_sockets is enabled and all...
Any ideas? now im trying WAMP on this dedicated server, to see if any config may help
Thank you

Related

PHP stream_socket_client not using proxy

I'm trying to get stream_socket_client working with proxy server.
Peace of code:
<?php
$context = stream_context_create(['http' => ['proxy' => '147.135.210.114:54566', 'request_fulluri' => true]]);
//$file = file_get_contents("http://www.google.com", false, $context);
$fp = stream_socket_client("tcp://www.google.com:80", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
fputs($fp, "GET / HTTP/1.0\r\nHost: www.google.com\r\nAccept: */*\r\n\r\n");
while (!feof($fp)) {
echo fgets($fp, 1024);
}
fclose($fp);
}
?>
While file_get_contents uses proxy (tcpdump -i any -A host 114.ip-147-135-210.eu) stream_socket_client simply omits it and goes directly to google.com. What am I doing wrong? My final goal is to connect to RabbitMQ (AMQP protocol) via proxy, but I can't even get simple HTTP connection working.
if anyone came here struggling, I ended up solving this by connecting to the proxy first then issuing http headers to get the content I wanted.
First create the socket to the proxy:
$sock = stream_socket_client(
"tcp://$proxy:$port",
$errno,
$errstr,30,
STREAM_CLIENT_CONNECT,
stream_context_create()
);
Second connect to the destination host you want:
$write = "CONNECT www.example.org HTTP/1.1\r\n";
$write .= "Proxy-Authorization: Basic ".base64_encode("$proxy_user:$proxy_pass)."\r\n";
$write .= "\r\n";
fwrite($sock, $write);
This should return a 200 code:
preg_match('/^HTTP\/\d\.\d 200/', fread($sock, 1024));
Now you can just issue GET (make sure you send all the HTTP header):
fwrite($sock, "GET / HTTP/1.1\r\n")
This has more details: https://stackoverflow.com/a/55010581/687976

PHP fsockopen error, connection refused(111), try to connect fingerprint device using TCP/IP

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.

Enabling JavaScript in a proxy server script

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.

PHP - Check if server is offline and not show a warning in XAMPP

i'm currently checking if a server(in this case 192.168.1.135) is online with this php code:
<?php
$fp = fsockopen("192.168.1.135", 443, $errno, $errstr, 0.4); //(line 47)
if (!$fp) {
echo "OFFLINE";
}
else{
echo "ONLINE";
}
?>
I have this code on my own server 192.168.1.130 using XAMPP (on LAN ofcourse)
So, if this server is offline the output will be:
Warning: fsockopen(): in C:\xampp\htdocs\index.php on line 47
OFFLINE
it should just be "OFFLINE". How to show just OFFLINE without the warning?
I think XAMPP will send me the warning when the server is down. but how should I do to not show the warning? I don't find a setting for this in XAMPP, anyone know?
if no, how should i do the line 47 correct even when the server is down(offline)?
When the server is online, this will work perfecly.
Use the silence operator # to suppress the warning:
$fp = #fsockopen("192.168.1.135", 443, $errno, $errstr, 0.4); //(line 47)
Side note: As you see, the # operator suppressed the warning as you requested. But be careful. A message like for example 'No route to host' will being also suppressed. (Mostly if your network connection isn't available) So you cannot be sure, that the server is offline. You should additionally output $errmsg if fsockopen() fails to see the reason.
Try this script
$fp = fsockopen("192.168.1.135", 443, $errno, $errstr, 0.4);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: 192.168.1.135\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}

How to connect telnet and send command and write output into text file using php

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

Categories