note 27755 modified in function.fsockopen by danbrown

From: Date: Sun, 20 Jul 2008 20:02:05 +0000
Subject: note 27755 modified in function.fsockopen by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-142330@lists.php.net to get a copy of this message
Here is some code to help out abit more with the EOF problem. I had a problem where I needed to strip out so many lines of server input to get back right data i wanted. <?php $port=4000; $host="localhost"; $message="test"; $status=senddata($host,$port,$message); print "$status"; function senddata($host,$port,$message) { #takes in account servers that do not return EOF character #send data to server and get back input #function globals $linenumber="2"; #amount of lines to get rid of before we give input $lineamount="1"; #amount of lines to read after we give input $fp = fsockopen("$host", $port, $errno, $errstr, 30); if (!$fp) { echo "$errstr ($errno)"; } else { for ($i = 1; $i < $linenumber+1; $i++) { fread ($fp,1); $bytes_left = socket_get_status($fp); if ($bytes_left > 0) { fread($fp, $bytes_left[unread_bytes]); } } fputs($fp, "$message\r\n"); for ($i = 1; $i < $lineamount+1; $i++) { $status.=fread($fp,1); $bytes_left = socket_get_status($fp); if ($bytes_left > 0) { $status.=fread($fp, $bytes_left[unread_bytes]); } } fclose ($fp); } return $status; } ?> --was-- <? $port=4000; $host="localhost"; $message="test"; $status=senddata($host,$port,$message); print "$status"; function senddata($host,$port,$message) { #takes in account servers that do not return EOF character #send data to server and get back input #function globals $linenumber="2"; #amount of lines to get rid of before we give input $lineamount="1"; #amount of lines to read after we give input $fp = fsockopen("$host", $port, $errno, $errstr, 30); if (!$fp) { echo "$errstr ($errno)"; } else { for ($i = 1; $i < $linenumber+1; $i++) { fread ($fp,1); $bytes_left = socket_get_status($fp); if ($bytes_left > 0) { fread($fp, $bytes_left[unread_bytes]); } } fputs($fp, "$message\r\n"); for ($i = 1; $i < $lineamount+1; $i++) { $status.=fread($fp,1); $bytes_left = socket_get_status($fp); if ($bytes_left > 0) { $status.=fread($fp, $bytes_left[unread_bytes]); } } fclose ($fp); } return $status; } ?> Here is some code to help out abit more with the EOF problem. I had a problem where I needed to strip out so many lines of server input to get back right data i wanted. UPDATE http://php.net/manual/en/function.fsockopen.php

« previous php.notes (#142330) next »