什麼是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></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教程有興趣的朋友有幫助。