能做什么
<code>PHP中的curl库最基本的用途就是去简单有效地*抓取网页的内容*,然后利用抓取到的网页数据经程序的方式进行处理和分析,最终得到你所想要的数据。当然了,这是最基本的用法,还有其他复杂有趣的用途,这取决于你的curl_setopt()行数的设置 </code>
怎么用
<code>首先,你的PHP中必须安装有curl的扩展插件,即phpinfo中有如下显示 ![这里写图片描述](http://img.blog.csdn.net/20150309200749680) 然后就可以开始使用了,具体步骤: 1.用curl_init()初始化一个curl会话 2.通过curl_setopt()来设置需要的全部选项 3.用curl_exec()来执行会话 4.执行完后使用curl_close()关闭会话 </code>
ps:curl_setopt()是curl使用的关键,很多复杂的用法都是基于此
一个简单的例子
<code><?php <span>//初始化一个curl对象</span><span>$curl</span>=curl_init(<span>"http://www.baidu.com"</span>); <span>//将抓取的数据已文本的形式保存</span><span>$fp</span>=<span>fopen</span>(<span>"test.txt"</span>); <span>//设置curl_setopt()参数</span> curl_setopt(<span>$ch</span>,CURLOPT_FILE,<span>$fp</span>); curl_setopt(<span>$ch</span>,CURLOPT_HEADER,<span>0</span>); <span>//执行curl_exec();</span> curl_exec(<span>$ch</span>); <span>//关闭curl会话</span> curl_exec(<span>$ch</span>); curl_close(<span>$ch</span>); <span>fclose</span>(<span>$fp</span>);</code>
以上例子来自官方文档
利用CURL实现抓取GOOGLE的host脚本
<code><?php <span>//初始化curl对话</span><span>$curl</span>=curl_init(); <span>//设置curl_setopt</span> curl_setopt(<span>$curl</span>, CURLOPT_URL, <span>'http://www.360kb.com/kb/2_122.html'</span>); curl_setopt(<span>$curl</span>,CURLOPT_HEADER,<span>0</span>); curl_setopt(<span>$curl</span>, CURLOPT_RETURNTRANSFER, true); <span>//执行curl</span><span>$data</span>=curl_exec(<span>$curl</span>); <span>//关闭curl资源</span> curl_close(<span>$curl</span>); <span>//清楚HTML标签</span><span>$data</span>=strip_tags(<span>$data</span>); <span>//处理的读入数据</span><span>$data</span>=substr(<span>$data</span>, strpos(<span>$data</span>, <span>'#google hosts'</span>)); <span>$data</span>=substr(<span>$data</span>,<span>0</span>,strpos(<span>$data</span>, <span>'#google hosts 2015 end'</span>)); <span>$data</span>=str_replace(<span>' '</span>, <span>' '</span>, <span>$data</span>); <span>//写入host</span><span>$host</span>=file_get_contents(<span>"C:\\Windows\\System32\\drivers\\etc\\HOSTS"</span>); <span>$host</span>=substr(<span>$host</span>,<span>0</span>,strpos(<span>$host</span>, <span>'#google hosts'</span>)); <span>$host</span>.=<span>$data</span>; <span>$fh</span>=<span>fopen</span>(<span>"C:\\Windows\\System32\\drivers\\etc\\HOSTS"</span>, <span>'w'</span>); <span>fwrite</span>(<span>fopen</span>(<span>"C:\\Windows\\System32\\drivers\\etc\\HOSTS"</span>, <span>'w'</span>), <span>$host</span>); <span>fclose</span>(<span>$fh</span>); </code>
以上就介绍了PHP中CURL应用与基于的修改Google实现,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。