时间:2021-07-01 10:21:17 帮助过:21人阅读
- <br><?php <br>$url='http://t.qq.com'; <br>$lines_array=file($url); <br>$lines_string=implode('',$lines_array); <br>echo htmlspecialchars($lines_string); <br>?> <br><br> <br>2. file_get_contents()函数 <br>使用file_get_contents和fopen必须空间开启allow_url_fopen。方法:编辑php.ini,设置 allow_url_fopen = On,allow_url_fopen关闭时fopen和file_get_contents都不能打开远程文件。 <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><?php <br>$url='http://t.qq.com'; <br>$lines_string=file_get_contents($url); <br>echo htmlspecialchars($lines_string); <br>?> <br><br> <br>3. fopen()->fread()->fclose()模式 <br><br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><?php <br>$url='http://t.qq.com'; <br>$handle=fopen($url,"rb"); <br>$lines_string=""; <br>do{ <br>$data=fread($handle,1024); <br>if(strlen($data)==0){break;} <br>$lines_string.=$data; <br>}while(true); <br>fclose($handle); <br>echo htmlspecialchars($lines_string); <br>?> <br> <br><br>4. curl方式 <br>使用curl必须空间开启curl。方法:windows下修改php.ini,将extension=php_curl.dll前面的分号去掉,而且需要拷贝ssleay32.dll和libeay32.dll到C:\WINDOWS\system32下;Linux下要安装curl扩展。 <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><?php <br>$url='http://t.qq.com'; <br>$ch=curl_init(); <br>$timeout=5; <br>curl_setopt($ch, CURLOPT_URL, $url); <br>curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); <br>curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); <br>$lines_string=curl_exec($ch); <br>curl_close($ch); <br>echo htmlspecialchars($lines_string); <br>?> <br> <br><br>5. fsockopen()函数 socket模式 <br>socket模式能否正确执行,也跟服务器的设置有关系,具体可以通过phpinfo查看服务器开启了哪些通信协议,比如我的本地php socket没开启http,只能使用udp测试一下了。 <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><?php <br>$fp = fsockopen("udp://127.0.0.1", 13, $errno, $errstr); <br>if (!$fp) { <br>echo "ERROR: $errno - $errstr<br />\n"; <br>} else { <br>fwrite($fp, "\n"); <br>echo fread($fp, 26); <br>fclose($fp); <br>} <br>?> <br> <br><br>6. 插件 <br>网上应该有比较多的插件,snoopy插件是在网上搜到的,有兴趣的可以研究一下。 <br><br>PHP解析xml(html) <br><br>1. 正则表达式: <br><br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><?php <br>$url='http://t.qq.com'; <br>$lines_string=file_get_contents($url); <br>eregi('<title>(.*)</title>',$lines_string,$title); <br>echo htmlspecialchars($title[0]); <br>?> <br> <br><br>2. PHP DOMDocument()对象 <br>如果远程的html或xml存在语法错误,php在解析dom的时候会报错。 <br><br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><?php <br>$url='http://www.136web.cn'; <br>$html=new DOMDocument(); <br>$html->loadHTMLFile($url); <br>$title=$html->getElementsByTagName('title'); <br>echo $title->item(0)->nodeValue; <br>?> <br> <br><br>3. 插件 <br>本文以PHP Simple HTML DOM Parser为例,进行简单介绍,simple_html_dom的语法类似jQuery,它让php操作dom,就像使用jQuery操作dom一样的简单。 <br><span><u></u></span> 代码如下:<pre class="brush:php;toolbar:false layui-box layui-code-view layui-code-notepad"><ol class="layui-code-ol"><li><br><?php <br>$url='http://t.qq.com'; <br>include_once('../simplehtmldom/simple_html_dom.php'); <br>$html=file_get_html($url); <br>$title=$html->find('title'); <br>echo $title[0]->plaintext; <br>?> <br> <br><br>当然中国人是富有创造性的,老外往往会在技术上领先,但中国人往往会在使用上更胜一筹,往往做出一些让老外不敢想的功能,比如php的远程抓取与分析,本来是为数据的整合提供方便。但国人很喜欢这个,于是乎大量的采集站,它们本身不创造任何有价值的内容,就是靠抓取别人的网站内容,并把它据为己有。在百度里输入“php小”关键词,suggest列表第一个就是“php小偷程序”,然后把同样的关键词放入google,哥只能笑而不语。</li><li> </li><li> </li></ol></pre></li></ol></pre></li></ol></pre></li></ol></pre></li></ol></pre></li></ol></pre></li></ol></pre>