PHP流下载网站内容,直到找到字符串 [英] PHP stream download website content until string found

查看:132
本文介绍了PHP流下载网站内容,直到找到字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

主题说这一切。我需要启动一个网站流,并阻止它。 < / head> 被找到。我想这样做,以保持两端的带宽,并节省脚本运行时间。



我不想将整个页面内容下载到内存中;我需要一个内容流,在PHP中。



谢谢社区,我爱你们:)

解决方案

 <?php 

function streamUntilStringFound($ url,$ string, $ timeout = 30){

//删除协议 - 防止错误
$ url = parse_url($ url);
unset($ url ['scheme']);
$ url = implode(,$ url);

//启动流
$ fp = @fsockopen($ url,80,$ errno,$ errstr,$ timeout);
if(!$ fp){
$ buffer =Invalid URL!; //使用$ errstr显示确切的错误
} else {
$ out =GET / HTTP / 1.1\r\\\
;
$ out。=主机:$ url\r\\\
;
$ out。=Connection:Close\r\\\
\r\\\
;
fwrite($ fp,$ out);
$ buffer =;
while(!feof($ fp)){
$ buffer。= fgets($ fp,128);
//发现字符串 - 停止下载任何新内容
if(strpos(struts($ buffer),$ string)!== false)break;
}
fclose($ fp);
}

return $ buffer;

}

//下载所有内容,直到关闭< / head>被发现
$ content = streamUntilStringFound(whoapi.com,< / head>);

//显示我们发现什么
echo< pre>。htmlspecialchars($ content);

?>

重要说明: (感谢@GordonM) em>



allow_url_fopen 需要在 php.ini 使用 fsockopen()


Subject says it all. I need to start a stream of a website and stop it when e.g. </head> is found. I would like to do it to preserve bandwidth on both ends and to save script running time.

I don't want to download the whole page content to a memory; I need a stream of content coming in blocks, in PHP.

Thank you community, I love you guys :)

解决方案

<?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().

这篇关于PHP流下载网站内容,直到找到字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆