Almost all PHP Telnet implementation examples have sockets (fsockopen). This does not work for me because it takes an invalid amount of time (~ 60 seconds).
I tried fsockopen for other purposes and found it slow, unlike cURL.
Question # 1: Why do sockets slow down?
Update: I found that we need to set stream_set_timeout , and we can control the socket runtime. I am wondering how to set the correct timeout or how to make it "stop waiting" after receiving a response.
I cannot get the same as cURL. Where should I put the commands that I need to send to telnet? Is CURLOPT_CUSTOMREQUEST selected correctly? I am doing something like this:
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); } }
And something similar to this:
$telnet = new TELNETcURL(); print_r($telnet->exec_cmd("<TELNET commands go here>"));
I get a "Maximum Run Time Exceeding 30 Seconds" in the curl_exec command.
Question No. 2: What is wrong with the cURL implementation?
source share