reading from external https path by php - php

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;
}

Related

Can't connect to WebSocket server with fsockopen

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');

PHP stream_socket_client with timeout on wss (ssl:// port 443) not working

I think I came down to a very specific PHP bug, that the timeouts set by stream_set_timeout() doesn't get obeyed, ONLY if using SSL websockets.
I am able to reproduce it with the following simple script:
<?php
$context = stream_context_create();
$fp = stream_socket_client ("ssl://echo.websocket.org:443",$errno,$errstr,5,STREAM_CLIENT_CONNECT,$context);
stream_set_timeout($fp, 5);
if (!$fp) {
echo "Unable to open\n";
} else {
//fwrite($fp, "GET / HTTP/1.0\r\n\r\n");
stream_set_timeout($fp, 5);
$res = fread($fp, 2000);
$info = stream_get_meta_data($fp);
//fclose($fp);
if ($info['timed_out']) {
echo 'Connection timed out!';
} else {
echo $res;
}
}
If you switch out ssl://echo.websocket.org:443 with tcp://echo.websocket.org:80, the timeout gets triggered correctly after 5 seconds.
Is this a PHP bug or am I missing some sort of setting?

TOR - change identity on every request

I would like to force TOR change the IP on every request. Or at least every few seconds.
Also, it would be great if the solution will work both on Linux and Windows.
Anyone knows how can I achieve this?
I found this PHP code which claims to be what I am looking for but I cannot understand what it is doing.
<?php
$fp = fsockopen('127.0.0.1', 9051, $errno, $errstr, 30);
$auth_code = 'YOUR_PASSWORD';
if ($fp) {
echo "Connected to TOR port<br />";
}
else {
echo "Cant connect to TOR port<br />";
}
fputs($fp, "AUTHENTICATE \"".$auth_code."\"\r\n");
$response = fread($fp, 1024);
list($code, $text) = explode(' ', $response, 2);
if ($code = '250') {
echo "Authenticated 250 OK<br />";
}
else {
echo "Authentication failed<br />";
}
fputs($fp, "SIGNAL NEWNYM\r\n");
$response = fread($fp, 1024);
list($code, $text) = explode(' ', $response, 2);
if ($code = '250') {
echo "New Identity OK<br />";
}
else {
echo "SIGNAL NEWNYM failed<br />";
die();
}
fclose($fp);
?>
Somebody?
It connects via Socket to your local tor relay, authenticates and sends a SIGNAL NEWNYM, which forces the tor relay to change the identity and exit node.
See more at
identity changing in tor
tor options
There is even a SE site about tor.

Response to fsockopen

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.

Php read socket stream update

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.

Categories