Home >Backend Development >PHP Tutorial >PHP curl
What is curl
curl is an open source file transfer tool that uses URL syntax to work in command line mode.
Use of PHP
Using PHP's cURL library can easily and effectively capture web pages. You only need to run a script and analyze the web pages you crawled, and then you can get the data you want programmatically. Whether you want to retrieve partial data from a link, take an XML file and import it into a database, or even simply retrieve the content of a web page, cURL is a powerful PHP library.
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 data
<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 use proxy server
<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>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });
The above has introduced PHP curl, including aspects of it. I hope it will be helpful to friends who are interested in PHP tutorials.