問題描述
我在充當(dāng) WebSocket 服務(wù)器的服務(wù)器上有進(jìn)程(不是用 Ratchet 編寫的).我希望能夠使用 PHP(作為客戶端)向該進(jìn)程發(fā)送數(shù)據(jù).
I have process on server which acts as WebSocket server (not written in Ratchet). I want to be able to send data to this process using PHP (as client).
我發(fā)現(xiàn)了很多像這樣作為 TCP 發(fā)送的示例:
I found a lot of examples to send as TCP like this:
<?php
$addr = gethostbyname("localhost");
$client = stream_socket_client("tcp://$addr:8887", $errno, $errorMessage);
if ($client === false) {
throw new UnexpectedValueException("Failed to connect: $errorMessage");
}
fwrite($client, "GET / HTTP/1.0
Host: localhost
Accept: */*
");
echo stream_get_contents($client);
?>
我只需要向進(jìn)程發(fā)送消息并關(guān)閉連接即可.我期望的結(jié)果是 webSocket 的結(jié)果稍后會(huì)打印或回顯"到 PHP 頁(yè)面.
All I need I to send message to the process and close the connection. The result that I expect is the result from the webSocket will be later printed or "echo" to the PHP page.
有沒有辦法讓它在 php 中與 curl 一起工作?
Is there a way to make it work with curl in php?
推薦答案
我在 github 上找到了這段代碼,(我找不到我從哪里得到它的確切 repo,因?yàn)槲乙呀?jīng)查看并嘗試了很多)
I have found this code on github, (I can't find the exact repo where I got it from because I have looked and tried a lot of them)
<?php
class WebsocketClient {
private $_Socket = null;
public function __construct($host, $port) {
$this->_connect($host, $port);
}
public function __destruct() {
$this->_disconnect();
}
public function sendData($data) {
// send actual data:
fwrite($this->_Socket, "x00" . $data . "xff") or die('Error:' . $errno . ':' . $errstr);
$wsData = fread($this->_Socket, 2000);
$retData = trim($wsData, "x00xff");
return $retData;
}
private function _connect($host, $port) {
$key1 = $this->_generateRandomString(32);
$key2 = $this->_generateRandomString(32);
$key3 = $this->_generateRandomString(8, false, true);
$header = "GET /echo HTTP/1.1
";
$header.= "Upgrade: WebSocket
";
$header.= "Connection: Upgrade
";
$header.= "Host: " . $host . ":" . $port . "
";
$header.= "Origin: http://localhost
";
$header.= "Sec-WebSocket-Key1: " . $key1 . "
";
$header.= "Sec-WebSocket-Key2: " . $key2 . "
";
$header.= "
";
$header.= $key3;
$this->_Socket = fsockopen($host, $port, $errno, $errstr, 2);
fwrite($this->_Socket, $header) or die('Error: ' . $errno . ':' . $errstr);
$response = fread($this->_Socket, 2000);
/**
* @todo: check response here. Currently not implemented cause "2 key handshake" is already deprecated.
* See: http://en.wikipedia.org/wiki/WebSocket#WebSocket_Protocol_Handshake
*/
return true;
}
private function _disconnect() {
fclose($this->_Socket);
}
private function _generateRandomString($length = 10, $addSpaces = true, $addNumbers = true) {
$characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"§$%&/()=[]{}';
$useChars = array();
// select some random chars:
for ($i = 0; $i < $length; $i++) {
$useChars[] = $characters[mt_rand(0, strlen($characters) - 1)];
}
// add spaces and numbers:
if ($addSpaces === true) {
array_push($useChars, ' ', ' ', ' ', ' ', ' ', ' ');
}
if ($addNumbers === true) {
array_push($useChars, rand(0, 9), rand(0, 9), rand(0, 9));
}
shuffle($useChars);
$randomString = trim(implode('', $useChars));
$randomString = substr($randomString, 0, $length);
return $randomString;
}
}
$WebSocketClient = new WebsocketClient('localhost', 8887);
echo $WebSocketClient->sendData("MyUserNameFromPHP");
unset($WebSocketClient);
?>
在我嘗試過的 7 個(gè) php websocket 客戶端中,這是我唯一能夠使用的客戶端.它不需要任何外部文件或框架.這是執(zhí)行不需要持久連接到 webSocket 服務(wù)器的短命令的簡(jiǎn)單實(shí)現(xiàn).
Out of 7 php websocket clients that I tried, this is the only one that I was able to work with. It doesn't requires any external files or frameworks. This is simple implementation for executing short command that doesn't require persistent connection to webSocket server.
希望能幫到你們,省時(shí)省力?。?!
I hope it helps you guys , and you will save some time !!!
這篇關(guān)于如何使用 WebSocket 從 PHP 發(fā)送數(shù)據(jù)/文本進(jìn)行處理?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!