I am using a code to connect to an smtp server.
For this I am using stream_socket_client but when I a pass a proxy using stream_context_create the socket doesn't take into consideration the proxy.
Here is the code
$xt = stream_context_create(array(
'http' => array(
'proxy' => 'tcp://PROXY:PORT',
)
)
);
$fp = stream_socket_client("tcp://icanhazip.com:80", $errno, $errstr, 3,STREAM_CLIENT_CONNECT,$xt);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
fwrite($fp, "GET / HTTP/1.0\r\nHost: icanhazip.com\r\nAccept: */*\r\n\r\n");
while (!feof($fp)) {
echo fgets($fp);
}
fclose($fp);
}
This code is for testing IP and it outputs the same IP. What am I doing wrong ?
Thanks
Related
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
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 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'm using PHPMailer and it uses fsockopen to access the SMTP server.
But the machine has two IPs with different reverse DNS records. So in email headers I got the following:
Received: from one-server.tld (HELO another-server.tld) ...
I need to hide one-server.tld in favor of another-server.tld. But I need both IPs with their current RDNS settings.
I think its not possible using fsockopen. But its possible in curl, fopen and stream functions. What you need is stream_socket_client() function.
Here are some ways to achieve it.
Using context parameters which can be used in fopen function family and stream function family. See the example.
$opts = array(
'socket' => array(
'bindto' => '192.168.0.100:0',
),
);
// create the context...
$context = stream_context_create($opts);
$contents = fopen('http://www.example.com', 'r', false, $context);
Also stream_socket_client
$fp = stream_socket_client("tcp://www.example.com:80", $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $opts);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
fwrite($fp, "GET / HTTP/1.0\r\nHost: www.example.com\r\nAccept: */*\r\n\r\n");
while (!feof($fp)) {
echo fgets($fp, 1024);
}
fclose($fp);
}
Using socket_bind. PHP.NET got a simple example here.