php的curl也没有这么复杂

WBOY
發布: 2016-06-13 12:14:53
原創
798 人瀏覽過

php的curl也没这么复杂

许多同学在第一次使用curl的时候感觉一个头两个大(包括我在内),看着这一条条的curl_setopt函数完全摸不着头脑,不过在你花10分钟看了我的介绍后相信你以后也能轻松戏耍php的curl了

首先,请看一个curl代码(花10秒钟,略看一遍,然后跳到后文)

<span style="color: #008080;"> 1</span> <span style="color: #000000;">php </span><span style="color: #008080;"> 2</span> <span style="color: #800080;">$data</span> = "<envelope>[...]</envelope>"<span style="color: #000000;">; </span><span style="color: #008080;"> 3</span> <span style="color: #800080;">$tuCurl</span> =<span style="color: #000000;"> curl_init(); </span><span style="color: #008080;"> 4</span> curl_setopt(<span style="color: #800080;">$tuCurl</span>, CURLOPT_URL, "https://example.com/path/for/soap/url/"<span style="color: #000000;">); </span><span style="color: #008080;"> 5</span> curl_setopt(<span style="color: #800080;">$tuCurl</span>, CURLOPT_PORT , 443<span style="color: #000000;">); </span><span style="color: #008080;"> 6</span> curl_setopt(<span style="color: #800080;">$tuCurl</span>, CURLOPT_VERBOSE, 0<span style="color: #000000;">); </span><span style="color: #008080;"> 7</span> curl_setopt(<span style="color: #800080;">$tuCurl</span>, CURLOPT_HEADER, 0<span style="color: #000000;">); </span><span style="color: #008080;"> 8</span> curl_setopt(<span style="color: #800080;">$tuCurl</span>, CURLOPT_SSLVERSION, 3<span style="color: #000000;">); </span><span style="color: #008080;"> 9</span> curl_setopt(<span style="color: #800080;">$tuCurl</span>, CURLOPT_SSLCERT, <span style="color: #008080;">getcwd</span>() . "/client.pem"<span style="color: #000000;">); </span><span style="color: #008080;">10</span> curl_setopt(<span style="color: #800080;">$tuCurl</span>, CURLOPT_SSLKEY, <span style="color: #008080;">getcwd</span>() . "/keyout.pem"<span style="color: #000000;">); </span><span style="color: #008080;">11</span> curl_setopt(<span style="color: #800080;">$tuCurl</span>, CURLOPT_CAINFO, <span style="color: #008080;">getcwd</span>() . "/ca.pem"<span style="color: #000000;">); </span><span style="color: #008080;">12</span> curl_setopt(<span style="color: #800080;">$tuCurl</span>, CURLOPT_POST, 1<span style="color: #000000;">); </span><span style="color: #008080;">13</span> curl_setopt(<span style="color: #800080;">$tuCurl</span>, CURLOPT_SSL_VERIFYPEER, 1<span style="color: #000000;">); </span><span style="color: #008080;">14</span> curl_setopt(<span style="color: #800080;">$tuCurl</span>, CURLOPT_RETURNTRANSFER, 1<span style="color: #000000;">); </span><span style="color: #008080;">15</span> curl_setopt(<span style="color: #800080;">$tuCurl</span>, CURLOPT_POSTFIELDS, <span style="color: #800080;">$data</span><span style="color: #000000;">); </span><span style="color: #008080;">16</span> curl_setopt(<span style="color: #800080;">$tuCurl</span>, CURLOPT_HTTPHEADER, <span style="color: #0000ff;">array</span>("Content-Type: text/xml","SOAPAction: \"/soap/action/query\"", "Content-length: ".<span style="color: #008080;">strlen</span>(<span style="color: #800080;">$data</span><span style="color: #000000;">))); </span><span style="color: #008080;">17</span> <span style="color: #008080;">18</span> <span style="color: #800080;">$tuData</span> = curl_exec(<span style="color: #800080;">$tuCurl</span><span style="color: #000000;">); </span><span style="color: #008080;">19</span> <span style="color: #0000ff;">if</span>(!curl_errno(<span style="color: #800080;">$tuCurl</span><span style="color: #000000;">)){ </span><span style="color: #008080;">20</span>   <span style="color: #800080;">$info</span> = curl_getinfo(<span style="color: #800080;">$tuCurl</span><span style="color: #000000;">); </span><span style="color: #008080;">21</span>   <span style="color: #0000ff;">echo</span> 'Took ' . <span style="color: #800080;">$info</span>['total_time'] . ' seconds to send a request to ' . <span style="color: #800080;">$info</span>['url'<span style="color: #000000;">]; </span><span style="color: #008080;">22</span> } <span style="color: #0000ff;">else</span><span style="color: #000000;"> { </span><span style="color: #008080;">23</span>   <span style="color: #0000ff;">echo</span> 'Curl error: ' . curl_error(<span style="color: #800080;">$tuCurl</span><span style="color: #000000;">); </span><span style="color: #008080;">24</span> <span style="color: #000000;">} </span><span style="color: #008080;">25</span> <span style="color: #008080;">26</span> curl_close(<span style="color: #800080;">$tuCurl</span><span style="color: #000000;">); </span><span style="color: #008080;">27</span> <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$tuData</span><span style="color: #000000;">; </span><span style="color: #008080;">28</span> ?>
登入後複製

WTF,这到底是在做什么?

想要学会这种“高端”的用法吗?

首先,相信你肯定知道网址大部分是由http开头的,那是因为他们需用通过http(超文本传送协议 HTTP-Hypertext transfer protocol)来进行数据传输,但是传输数据不是简单的将一句"Hello"传到服务器上就搞定的事情,发送者为了方便接受者理解发送者的实际意图以及知道发送人到底是何许人也,发送者往往要将许多额外信息一并发给接受者,就像寄信人需要在信件外套一个信封一样,信封上写着各种发信人的信息。所有的这些最终合并成了一个叫做报文(message)的玩意,也就构成了整个互联网的基础。

curl的工作就是通过http协议发送这些message (php的libcurl目前还支持https、ftp、telnet等其他协议)

现在再看代码,实际上代码只做了五件事情

  1. curl_init()初始化curl
  2. curl_setopt()设置传输数据和参数
  3. curl_exec()执行传输并获取返回数据
  4. curl_errono()返回错误码
  5. curl_close()关闭curl

下面给出使用GET和POST方法如何抓取和提交任意页面的数据

<span style="color: #008080;"> 1</span> <span style="color: #000000;">php</span><span style="color: #008080;"> 2</span>     <span style="color: #008000;">//</span><span style="color: #008000;">初始化</span><span style="color: #008080;"> 3</span>     <span style="color: #800080;">$curl</span> =<span style="color: #000000;"> curl_init();</span><span style="color: #008080;"> 4</span>     <span style="color: #008000;">//</span><span style="color: #008000;">设置url</span><span style="color: #008080;"> 5</span>     curl_setopt(<span style="color: #800080;">$curl</span>, CURLOPT_URL, 'http://www.baidu.com'<span style="color: #000000;">);</span><span style="color: #008080;"> 6</span>     <span style="color: #008000;">//</span><span style="color: #008000;">设置返回获取的输出为文本流</span><span style="color: #008080;"> 7</span>     curl_setopt(<span style="color: #800080;">$curl</span>, CURLOPT_RETURNTRANSFER, <span style="color: #0000ff;">true</span><span style="color: #000000;">);</span><span style="color: #008080;"> 8</span>     <span style="color: #008000;">//</span><span style="color: #008000;">执行命令</span><span style="color: #008080;"> 9</span>     <span style="color: #800080;">$data</span> = curl_exec(<span style="color: #800080;">$curl</span><span style="color: #000000;">);</span><span style="color: #008080;">10</span>     <span style="color: #008000;">//</span><span style="color: #008000;">关闭URL请求</span><span style="color: #008080;">11</span>     curl_close(<span style="color: #800080;">$curl</span><span style="color: #000000;">);</span><span style="color: #008080;">12</span>     <span style="color: #008000;">//</span><span style="color: #008000;">显示获得的数据</span><span style="color: #008080;">13</span>     <span style="color: #008080;">print_r</span>(<span style="color: #800080;">$data</span><span style="color: #000000;">);</span><span style="color: #008080;">14</span> ?><span style="color: #008080;">15</span> <span style="color: #008080;">16</span> <span style="color: #000000;">php</span><span style="color: #008080;">17</span>     <span style="color: #008000;">//</span><span style="color: #008000;">初始化</span><span style="color: #008080;">18</span>     <span style="color: #800080;">$curl</span> =<span style="color: #000000;"> curl_init();</span><span style="color: #008080;">19</span>     <span style="color: #008000;">//</span><span style="color: #008000;">设置url</span><span style="color: #008080;">20</span>     curl_setopt(<span style="color: #800080;">$curl</span>, CURLOPT_URL, 'http://www.baidu.com'<span style="color: #000000;">);</span><span style="color: #008080;">21</span>     <span style="color: #008000;">//</span><span style="color: #008000;">设置返回获取的输出为文本流</span><span style="color: #008080;">22</span>     curl_setopt(<span style="color: #800080;">$curl</span>, CURLOPT_RETURNTRANSFER, <span style="color: #0000ff;">true</span><span style="color: #000000;">);</span><span style="color: #008080;">23</span>     <span style="color: #008000;">//</span><span style="color: #008000;">设置post方式提交</span><span style="color: #008080;">24</span>     curl_setopt(<span style="color: #800080;">$curl</span>, CURLOPT_POST, 1<span style="color: #000000;">);</span><span style="color: #008080;">25</span>     <span style="color: #008000;">//</span><span style="color: #008000;">设置post数据</span><span style="color: #008080;">26</span>     curl_setopt(<span style="color: #800080;">$curl</span>, CURLOPT_POSTFIELDS, <span style="color: #0000ff;">array</span>("data"=>"value"<span style="color: #000000;">);</span><span style="color: #008080;">27</span>     <span style="color: #008000;">//</span><span style="color: #008000;">执行命令</span><span style="color: #008080;">28</span>     <span style="color: #800080;">$data</span> = curl_exec(<span style="color: #800080;">$curl</span><span style="color: #000000;">);</span><span style="color: #008080;">29</span>     <span style="color: #008000;">//</span><span style="color: #008000;">关闭URL请求</span><span style="color: #008080;">30</span>     curl_close(<span style="color: #800080;">$curl</span><span style="color: #000000;">);</span><span style="color: #008080;">31</span>     <span style="color: #008000;">//</span><span style="color: #008000;">打印数据</span><span style="color: #008080;">32</span>     <span style="color: #008080;">print_r</span>(<span style="color: #800080;">$data</span><span style="color: #000000;">);</span><span style="color: #008080;">33</span> ?>
登入後複製

感兴趣的同学还可以参考php官方文档,学习更多curl用法

 

2楼Ronaldo7
你能post数据到baidu????????
Re: 深蓝的镰刀
@Ronaldo7,举个栗子吧
1楼安度
你和深蓝的右手什么关系
Re: 深蓝的镰刀
@安度,我记得没错他应该是专注游戏引擎的大神吧
相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板