I have a PHP Class to push message with socket like :
function __construct()
{
$this->socket = fsockopen($this->host, $this->port, $errno, $errstr, 0); //I tried with 99999 for timeout too
}
function push($params)
{
$req = 'GET /push?'.$params." HTTP/1.1\r\n"
. 'Host: '.$this->host."\r\n"
. "Content-Type: application/x-www-form-urlencoded\r\n"
. 'Content-Length: '.strlen($params)."\r\n"
. "Connection: keep-alive\r\n\r\n";
fwrite($this->socket, $req);
}
But if I tried to push 2 or more message, only one is receive by NodeJS serveur :
foreach(['foo=2', 'bar=42'] as $loop)
{
$pusher->push($loop);
}
Now, if i put this line $this->socket = fsockopen($this->host, $this->port, $errno, $errstr, 0); just before fwrite, all messages will be send...
So why I can't use only one connexion for multiple request with same soket?
I use "keep-alive" and I don't call fclose, so I don't understand why my nodeJS server receive only one message in first case...
Related
I would like to create a locally persistent connection with my site.
I would like to create a single connection where I can exchange infinite messages.
I used the code below.
but it's really persistent? inside the "while" open (fsockopen) and close (fclose) the connection, but if I take them out of the "while" gives me problems.
$url="localhost/socket/socket.php";
$host=substr($url,0,strpos($url,"/"));
$req=substr($url,strpos($url,"/"));
$var=fopen("out.txt","a+");
while(1){
$fp = fsockopen($host, 80, $errno, $errstr, 30);
if ($fp) {
$data = "test=1";
$request = "POST ".$req." HTTP/1.1\r\n";
$request .= "Host: ".$host."\r\n";
$request .= "Content-Length: " . strlen($data) . "\r\n";
$request .= "Content-Type: application/x-www-form-urlencoded\r\n\r\n";
$request .= $data;
fwrite($fp, $request);
while (!feof($fp)) {
$json = fgets ($fp);
echo $json;
fwrite($var, $json);
}
}
sleep(10);
fclose($fp);
}
If you want a persistent connection you should use pfsockopen instead of fsockopen. Look here for more details.
I am trying to figure out if there is a way to do a curl post, but without receiving the response.
I know I can prevent the response from being displayed by setting CURLOPT_RETURNTRANSFER to false, but I don't even want to receive it.
I am working on a proxy and need to do the post request to a webservice, but the response it gives is MASSIVE and ends up timing out my connection (even when set to 200 seconds).
Even if it did work, that's just way too long since I don't care what the response is at all.
I can't seem to find a way to do this.
Try using a socket rather than using cURL:
$requestBody = 'myparams=something&something=somethingelse';
$socket = fsockopen("mysite.com", 80, $errno, $errstr, 15);
if (!$socket) {
// Handle the error, can't connect
} else {
$http = "POST /path/to/post/to HTTP/1.1\r\n";
$http .= "Host: mysite.com\r\n";
$http .= "Content-Type: application/x-www-form-urlencoded\r\n";
$http .= "Content-length: " . strlen($post_data) . "\r\n";
$http .= "Connection: close\r\n\r\n";
$http .= $requestBody . "\r\n\r\n";
fwrite($socket, $http);
fclose($socket);
}
This will submit the POST request and not wait around for the response.
I have a working websocket Server (python + Tornado) which accepts Connections on port 8973. I can connect by a easy JavaScript / jquery instruction like:
ws = new WebSocket("ws://192.168.41.170:8973/rt");
But I need my php script to connect to this websocket server and send a message. I tried all most available solutions like
https://github.com/lemmingzshadow/php-websocket/
$host = '192.168.41.170'; //where is the websocket server
$port = 8973;
$local = "http://192.168.41.2/"; //url where this script run
$data = 'hello world!'; //data to be send
$head = "GET / HTTP/1.1"."\r\n".
"Upgrade: WebSocket"."\r\n".
"Connection: Upgrade"."\r\n".
"Origin: $local"."\r\n".
"Host: $host"."\r\n".
"Content-Length: ".strlen($data)."\r\n"."\r\n";
//WebSocket handshake
$sock = fsockopen($host, $port, $errno, $errstr, 2);
fwrite($sock, $head ) or die('error:'.$errno.':'.$errstr);
$headers = fread($sock, 2000);
fwrite($sock, "\x00$data\xff" ) or die('error:'.$errno.':'.$errstr);
$wsdata = fread($sock, 2000); //receives the data included in the websocket package "\x00DATA\xff"
fclose($sock);
But this all dont work. Has anybody a working code snippet? I dont need a php-websocket server! Thanks
I think your problem is in how you are packing the data. Without knowing which protocol you are trying to use, I can't tell you how to pack it! But, there is a working code snippet in one answer to this question.
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 want to send post variables with fsock,
but when i try this:
$post_arr = array ("a" => "b");
$addr = 'http://1.2.3.4/confirmation.html';
$fp = fsockopen($addr, 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$req = '';
foreach ($post_arr as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&" . $key . "=" . $value;
}
$header = "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
fwrite($fp, $header);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
I get 'Unable to find the socket transport "http" ',
any ideas why?
fsockopen() opens a socket. Sockets do not know anything about Layer5+ protocols such as HTTP.
$fp = fsockopen('1.2.3.4', 80, $errno, $errstr, 30);
To request a certain path, send it in the request: GET /confirmation.html
To specify the domain, send it in the Host header: Host: 1.2.3.4
You might want to consider using the curl extension. There is usually no good reason to build HTTP requests manually in PHP.