note 97962 deleted from function.fsockopen by cmb
| From: | cmb@php.net | Date: | Sun, 07 Jul 2019 07:42:23 +0000 |
| Subject: | note 97962 deleted from function.fsockopen by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-211489@lists.php.net to get a copy of this message | ||
Note Submitter: SoupNazi
----
I would like to save people some time. I spent a couple hours trying to de-hunkify the response
from apache2. I found out that if you simply make a 1.0 request vs. a 1.1 request you will get no
hunks and so this simple script below will get the page you want in plain text, no hunks:
<?php
$data=false;
$url='www.example.com';
$fp = @fsockopen($url, 80, $errno, $errstr, 1);
if ($fp) {
stream_set_timeout($fp,1);
$out = "GET / HTTP/1.0\r\n";
$out .= "Host: www.example.com\r\n";
$out .= "Connection: Close\r\n\r\n";
if (fwrite($fp, $out)) {
$content='';
$header = "not yet";
while (!feof($fp)) {
$data=true;
$line=fgets($fp,128);
if ($line=="\r\n" && $header=="not yet") {
$header = "passed";
}
if ($header=="passed") {
$content.=$line;
}
}
fclose ($fp);
}
}
if (!$data) {
print "Unable to retrieve all or part of $url";
} else {
print "$content";
}
?>
I borrowed some of the script from another poster here - the portion removing the returned header.