<?php
function streamUntilStringFound($url, $string, $timeout = 30){
// remove the protocol - prevent the errors
$url = parse_url($url);
unset($url['scheme']);
$url = implode("", $url);
// start the stream
$fp = @fsockopen($url, 80, $errno, $errstr, $timeout);
if (!$fp) {
$buffer = "Invalid URL!"; // use $errstr to show the exact error
} else {
$out = "GET / HTTP/1.1\r\n";
$out .= "Host: $url\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
$buffer = "";
while (!feof($fp)) {
$buffer .= fgets($fp, 128);
// string found - stop downloading any new content
if (strpos(strtolower($buffer), $string) !== false) break;
}
fclose($fp);
}
return $buffer;
}
// download all content until closing </head> is found
$content = streamUntilStringFound("whoapi.com", "</head>");
// show us what is found
echo "<pre>".htmlspecialchars($content);
?>
Important note: (thanks to @GordonM)
allow_url_fopen
needs to be enabled in php.ini
to use fsockopen()
.