什么是curl
curl是利用URL语法在命令行方式下工作的开源文件传输工具。
PHP 的用处
使用PHP的cURL库可以简单和有效地去抓网页。你只需要运行一个脚本,然后分析一下你所抓取的网页,然后就可以以程序的方式得到你想要的数据了。无论是你想从从一个链接上取部分数据,或是取一个XML文件并把其导入数据库,那怕就是简单的获取网页内容,cURL 是一个功能强大的PHP库。
demo1
<code><span><span><?php </span><span>//1.初始化,创建一个新cURL资源 </span><span>$ch</span> = curl_init(); <span>//2.设置URL和相应的选项</span> curl_setopt(<span>$ch</span>, CURLOPT_URL, <span>"http://www.lampbrother.net/"</span>) curl_setopt(<span>$ch</span>, CURLOPT_HEADER, <span>0</span>); <span>//3.抓取URL并把它传递给浏览器 </span><span>$data</span> = curl_exec(<span>$ch</span>); <span>//4.显示获得的数据</span> var_dump(<span>$data</span>); <span>//5.关闭cURL资源,并且释放系统资源 </span> curl_close(<span>$ch</span>); <span>?></span></span></span></code>
demo2 post数据
<code>﹤?php <span>$phoneNumber</span> = <span>'13812345678'</span>; <span>$message</span> = <span>'This message was generated by curl and php'</span>; <span>$curlPost</span> = <span>'pNUMBER='</span> . urlencode(<span>$phoneNumber</span>) . <span>'&MESSAGE='</span> . urlencode(<span>$message</span>) . <span>'&SUBMIT=Send'</span>; <span>$ch</span> = curl_init(); curl_setopt(<span>$ch</span>, CURLOPT_URL, <span>'http://www.lxvoip.com/sendSMS.php'</span>); curl_setopt(<span>$ch</span>, CURLOPT_HEADER, <span>1</span>); curl_setopt(<span>$ch</span>, CURLOPT_RETURNTRANSFER, <span>1</span>); curl_setopt(<span>$ch</span>, CURLOPT_POST, <span>1</span>); curl_setopt(<span>$ch</span>, CURLOPT_POSTFIELDS, <span>$curlPost</span>); <span>$data</span> = curl_<span>exec</span>(); curl_close(<span>$ch</span>); ?﹥</code>
demo3 使用代理服务器
<code>﹤?php <span>$ch</span> = curl_init(); curl_setopt(<span>$ch</span>, CURLOPT_URL, <span>'http://www.cmx8.cn'</span>); curl_setopt(<span>$ch</span>, CURLOPT_HEADER, <span>1</span>); curl_setopt(<span>$ch</span>, CURLOPT_RETURNTRANSFER, <span>1</span>); curl_setopt(<span>$ch</span>, CURLOPT_HTTPPROXYTUNNEL, <span>1</span>); curl_setopt(<span>$ch</span>, CURLOPT_PROXY, <span>'proxy.lxvoip.com:1080'</span>); curl_setopt(<span>$ch</span>, CURLOPT_PROXYUSERPWD, <span>'user:password'</span>); <span>$data</span> = curl_<span>exec</span>(); curl_close(<span>$ch</span>); ?﹥</code>
以上就介绍了PHP curl,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。