note 79944 added to function.stream-get-line
| From: | MatJaggard at Tickets dot com | Date: | Thu, 20 Dec 2007 16:12:14 +0000 |
| Subject: | note 79944 added to function.stream-get-line | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-133815@lists.php.net to get a copy of this message | ||
I've spent quite a while trying to get stream_get_line to get a chunk encoded html file and to
finish correctly at the end so that I can pipeline requests.
This is the function I have come up with.
<?php
function getURLContents($url, $ip, $port, $ssl = false, $closeConnection = false)
{
if ($ssl)
$ssl = 'ssl://';
else
$ssl = '';
$fp = pfsockopen($ssl.$ip, $port, $errno, $errstr, MAX_TIME_TO_START_CONNECTION);
if ($fp)
{
$out = 'GET '.$url." HTTP/1.1\r\n";
$out .= 'Host: '.$ip.':'.$port."\r\n";
if ($closeConnection)
$out .= "Connection: close\r\n";
else
$out .= "Connection: keep-alive\r\n";
$out .= "\r\n";
if (!fwrite($fp, $out))
{
echo 'Problem writing to socket, opening a new connection.';
fclose($fp);
$fp = pfsockopen($ssl.$ip, $port, $errno, $errstr, MAX_TIME_TO_START_CONNECTION);
fwrite($fp, $out);
}
$theData = '';
$notDone = true;
stream_set_blocking($fp, 0);
$startTime = time();
$lastTime = $startTime;
while (!feof($fp) && !$done && (($startTime + MAX_TIME_FOR_THE_RESPONSE) >
time()))
{
usleep(100);
$theNewData = stream_get_line($fp, 1024, "\n");
$theData .= $theNewData;
$done = (trim($theNewData) === '0');
}
}
else
{
echo 'ERROR CONNECTING TO '.$ip.':'.$port;
return false;
}
if ($closeConnection)
fclose($fp);
return $theData;
}
?>
----
Server IP: 216.92.131.65
Probable Submitter: 62.173.122.153
----
Manual Page -- http://www.php.net/manual/en/function.stream-get-line.php
Edit -- https://master.php.net/note/edit/79944
Del: integrated -- https://master.php.net/note/delete/79944/integrated
Del: useless -- https://master.php.net/note/delete/79944/useless
Del: bad code -- https://master.php.net/note/delete/79944/bad+code
Del: spam -- https://master.php.net/note/delete/79944/spam
Del: non-english -- https://master.php.net/note/delete/79944/non-english
Del: in docs -- https://master.php.net/note/delete/79944/in+docs
Del: other reasons-- https://master.php.net/note/delete/79944
Reject -- https://master.php.net/note/reject/79944
Search -- https://master.php.net/manage/user-notes.php