http://www.btmao.org 为了从远程读取网页内容,我们常常用需要用PHP打开网页文件,而当网页文件很大或者网页根本不存在的时候,我们发现使用fopen的方法经常会出现超时。于是我们需要一个更加稳定的方法来解决这个问题。 我们想到使用fsockopen来帮助我们完
http://www.btmao.org
为了从远程读取网页内容,我们常常用需要用PHP打开网页文件,而当网页文件很大或者网页根本不存在的时候,我们发现使用fopen的方法经常会出现超时。于是我们需要一个更加稳定的方法来解决这个问题。
我们想到使用fsockopen来帮助我们完成工作。fsockopen是定下在network function下的一个函数,他使用的是socket来打开文件。
resource fsockopen ( string $hostname [, int $port [, int &$errno [, string &$errstr [, float $timeout ]]]] )
我们定义了一个类叫做http,下面是详细的代码。
php
class http
{
function forge_http_packet($url, & $host, & $port, & $packet)
{
// sure about the $url is start with "http://"
$strpos = strpos($url, '/', 7);
$filename = substr($url, $strpos, strlen($url) - $strpos);
$strpos2 = strpos($url, ':' , 7);
if ($strpos2 == false || $strpos2 > $strpos)
{
$host = substr($url, 7, $strpos - 7);
$port = 80;
}
else
{
$host = substr($url, 7, $strpos2 - 7);
$port = intval(substr($url, $strpos2 + 1, $strpos - $strpos2 - 1));
}
$encode_filename = str_replace(' ', '%20', $filename);
$packet = "GET ".$encode_filename." HTTP/1.1 ";
$packet .= "Accept: */* ";
$packet .= "Host: ".$host."";
$packet .= "User-Agent: OpenWebSpider ";
}
function chunked_result($chunked_html)
{
$html = "";
$start_pos = 0;
while ($start_pos strlen($chunked_html))
{
$final_pos = strpos($chunked_html, "", $start_pos);
$chunked_length = $this->hex2dec(substr($chunked_html, $start_pos, $final_pos - $start_pos));
$buf_html = substr($chunked_html, $final_pos + 2, $chunked_length);
$html .= $buf_html;
$start_pos = $final_pos + $chunked_length + 4;
}
return $html;
}
function hex2dec($hexstr)
{
$num = 0;
for ($i = 0 ; $i strlen($hexstr) ; $i++)
{
$num = $num * 16;
if ($hexstr[$i] >= '0' && $hexstr[$i] '9')
$num += ord($hexstr[$i]) - ord('0');
if ($hexstr[$i] >= 'A' && $hexstr[$i] 'F')
$num += ord($hexstr[$i]) - ord('A') + 10;
if ($hexstr[$i] >= 'a' && $hexstr[$i] 'f')
$num += ord($hexstr[$i]) - ord('a') + 10;
}
return $num;
}
function read_from_url($url)
{
$total_len = 0;
$this->forge_http_packet($url, $host, $port, $packet);
$handle = fsockopen($host, $port, $errno, $errstr, 5);
if ($handle) {
fwrite($handle, $packet);
$result = "";
while (!feof($handle)) {
stream_set_timeout($handle, 10);
$buffer = fgets($handle, 4096); //4K per 20 sec
$info = stream_get_meta_data($handle);
if ($info['timed_out'])
{
break;
}
$total_len += strlen($buffer);
$result .= $buffer;
if ($total_len > 1000000)
{ // more than 2M filesize, properly not correct url for news or list
return "";
}
}
fclose($handle);
$pos = strpos($result, "");
$header = substr($result, 0, $pos + 4);
if (strncasecmp($result, "HTTP/1.0 302", 12) == 0 ||
strncasecmp($result, "HTTP/1.1 302", 12) == 0 ||
strncasecmp($result, "HTTP/1.0 301", 12) == 0 ||
strncasecmp($result, "HTTP/1.1 301", 12) == 0)
{
$new_url_pos = strpos($result, " Location:");
if ($new_url_pos === false) return "";
$new_url_pos += 11;
$new_url_pos2 = strpos($result, "", $new_url_pos);
return $this->read_body_from_url(trim(substr($result,
$new_url_pos, $new_url_pos2 - $new_url_pos + 1)));
}
else if (strncasecmp($result, "HTTP/1.0 4", 10) == 0 ||
strncasecmp($result, "HTTP/1.1 4", 10) == 0 ||
strncasecmp($result, "HTTP/1.0 5", 10) == 0 ||
strncasecmp($result, "HTTP/1.1 5", 10) == 0)
{
$pos2 = strpos($result, "");
return substr($result, 0, $pos2);
}
if (strstr($header, "Transfer-Encoding: chunked") == false)
return strstr($result, "");
return $this->chunked_result(substr($result, $pos + 4, strlen($result) - ($pos + 4)));
}
return "";
}
}
?>
只需要调用上述类中的函数read_from_url($url)便可以完成工作。

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

SublimeText3漢化版
中文版,非常好用

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),