PHP 中几乎所有 TELNET 实现的示例都使用套接字 ( fsockopen )。这对我不起作用,因为它花费了无法接受的时间(约 60 秒)。
我曾为其他目的尝试过fsockopen,但发现它与 cURL 相比速度较慢。
问题 1:为什么套接字这么慢?
更新:我发现我们需要设置stream_set_timeout函数,我们可以控制socket的执行时间。我很好奇如何设置正确的超时或如何在收到响应后使其“停止等待”。
我无法用 cURL 实现同样的事情。我应该把需要发送到 telnet 的命令放在哪里?是CURLOPT_CUSTOMREQUEST正确的选择吗?我正在做这样的事情:
class TELNETcURL{
public $errno;
public $errstr;
private $curl_handle;
private $curl_options = array(
CURLOPT_URL => "telnet://XXX.XXX.XXX.XXX:<port>",
CURLOPT_TIMEOUT => 40,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HEADER => FALSE,
CURLOPT_PROTOCOLS => CURLPROTO_TELNET
);
function __construct(){
$this->curl_handle = curl_init();
curl_setopt_array($this->curl_handle, $this->curl_options);
}
public function exec_cmd($query) {
curl_setopt($this->curl_handle, CURLOPT_CUSTOMREQUEST, $query."\r\n");
$output = curl_exec($this->curl_handle);
return $output;
}
function __destruct(){
curl_close($this->curl_handle); …Run Code Online (Sandbox Code Playgroud)