Heim > Artikel > Backend-Entwicklung > php的curl也没这么复杂,phpcurl也复杂_PHP教程
许多同学在第一次使用curl的时候感觉一个头两个大(包括我在内),看着这一条条的curl_setopt函数完全摸不着头脑,不过在你花10分钟看了我的介绍后相信你以后也能轻松戏耍php的curl了
首先,请看一个curl代码(花10秒钟,略看一遍,然后跳到后文)
<span> 1</span> <?<span>php </span><span> 2</span> <span>$data</span> = "<soap:Envelope>[...]</soap:Envelope>"<span>; </span><span> 3</span> <span>$tuCurl</span> =<span> curl_init(); </span><span> 4</span> curl_setopt(<span>$tuCurl</span>, CURLOPT_URL, "https://example.com/path/for/soap/url/"<span>); </span><span> 5</span> curl_setopt(<span>$tuCurl</span>, CURLOPT_PORT , 443<span>); </span><span> 6</span> curl_setopt(<span>$tuCurl</span>, CURLOPT_VERBOSE, 0<span>); </span><span> 7</span> curl_setopt(<span>$tuCurl</span>, CURLOPT_HEADER, 0<span>); </span><span> 8</span> curl_setopt(<span>$tuCurl</span>, CURLOPT_SSLVERSION, 3<span>); </span><span> 9</span> curl_setopt(<span>$tuCurl</span>, CURLOPT_SSLCERT, <span>getcwd</span>() . "/client.pem"<span>); </span><span>10</span> curl_setopt(<span>$tuCurl</span>, CURLOPT_SSLKEY, <span>getcwd</span>() . "/keyout.pem"<span>); </span><span>11</span> curl_setopt(<span>$tuCurl</span>, CURLOPT_CAINFO, <span>getcwd</span>() . "/ca.pem"<span>); </span><span>12</span> curl_setopt(<span>$tuCurl</span>, CURLOPT_POST, 1<span>); </span><span>13</span> curl_setopt(<span>$tuCurl</span>, CURLOPT_SSL_VERIFYPEER, 1<span>); </span><span>14</span> curl_setopt(<span>$tuCurl</span>, CURLOPT_RETURNTRANSFER, 1<span>); </span><span>15</span> curl_setopt(<span>$tuCurl</span>, CURLOPT_POSTFIELDS, <span>$data</span><span>); </span><span>16</span> curl_setopt(<span>$tuCurl</span>, CURLOPT_HTTPHEADER, <span>array</span>("Content-Type: text/xml","SOAPAction: \"/soap/action/query\"", "Content-length: ".<span>strlen</span>(<span>$data</span><span>))); </span><span>17</span> <span>18</span> <span>$tuData</span> = curl_exec(<span>$tuCurl</span><span>); </span><span>19</span> <span>if</span>(!curl_errno(<span>$tuCurl</span><span>)){ </span><span>20</span> <span>$info</span> = curl_getinfo(<span>$tuCurl</span><span>); </span><span>21</span> <span>echo</span> 'Took ' . <span>$info</span>['total_time'] . ' seconds to send a request to ' . <span>$info</span>['url'<span>]; </span><span>22</span> } <span>else</span><span> { </span><span>23</span> <span>echo</span> 'Curl error: ' . curl_error(<span>$tuCurl</span><span>); </span><span>24</span> <span>} </span><span>25</span> <span>26</span> curl_close(<span>$tuCurl</span><span>); </span><span>27</span> <span>echo</span> <span>$tuData</span><span>; </span><span>28</span> ?>
WTF,这到底是在做什么?
想要学会这种“高端”的用法吗?
首先,相信你肯定知道网址大部分是由http开头的,那是因为他们需用通过http(超文本传送协议 HTTP-Hypertext transfer protocol)来进行数据传输,但是传输数据不是简单的将一句"Hello"传到服务器上就搞定的事情,发送者为了方便接受者理解发送者的实际意图以及知道发送人到底是何许人也,发送者往往要将许多额外信息一并发给接受者,就像寄信人需要在信件外套一个信封一样,信封上写着各种发信人的信息。所有的这些最终合并成了一个叫做报文(message)的玩意,也就构成了整个互联网的基础。
curl的工作就是通过http协议发送这些message (php的libcurl目前还支持https、ftp、telnet等其他协议)
现在再看代码,实际上代码只做了五件事情
下面给出使用GET和POST方法如何抓取和提交任意页面的数据<span> 1</span> <?<span>php </span><span> 2</span> <span>//</span><span>初始化</span> <span> 3</span> <span>$curl</span> =<span> curl_init(); </span><span> 4</span> <span>//</span><span>设置url</span> <span> 5</span> curl_setopt(<span>$curl</span>, CURLOPT_URL, 'http://www.baidu.com'<span>); </span><span> 6</span> <span>//</span><span>设置返回获取的输出为文本流</span> <span> 7</span> curl_setopt(<span>$curl</span>, CURLOPT_RETURNTRANSFER, <span>true</span><span>); </span><span> 8</span> <span>//</span><span>执行命令</span> <span> 9</span> <span>$data</span> = curl_exec(<span>$curl</span><span>); </span><span>10</span> <span>//</span><span>关闭URL请求</span> <span>11</span> curl_close(<span>$curl</span><span>); </span><span>12</span> <span>//</span><span>显示获得的数据</span> <span>13</span> <span>print_r</span>(<span>$data</span><span>); </span><span>14</span> ?> <span>15</span> <span>16</span> <?<span>php </span><span>17</span> <span>//</span><span>初始化</span> <span>18</span> <span>$curl</span> =<span> curl_init(); </span><span>19</span> <span>//</span><span>设置url</span> <span>20</span> curl_setopt(<span>$curl</span>, CURLOPT_URL, 'http://www.baidu.com'<span>); </span><span>21</span> <span>//</span><span>设置返回获取的输出为文本流</span> <span>22</span> curl_setopt(<span>$curl</span>, CURLOPT_RETURNTRANSFER, <span>true</span><span>); </span><span>23</span> <span>//</span><span>设置post方式提交</span> <span>24</span> curl_setopt(<span>$curl</span>, CURLOPT_POST, 1<span>); </span><span>25</span> <span>//</span><span>设置post数据</span> <span>26</span> curl_setopt(<span>$curl</span>, CURLOPT_POSTFIELDS, <span>array</span>("data"=>"value"<span>); </span><span>27</span> <span>//</span><span>执行命令</span> <span>28</span> <span>$data</span> = curl_exec(<span>$curl</span><span>); </span><span>29</span> <span>//</span><span>关闭URL请求</span> <span>30</span> curl_close(<span>$curl</span><span>); </span><span>31</span> <span>//</span><span>打印数据</span> <span>32</span> <span>print_r</span>(<span>$data</span><span>); </span><span>33</span> ?>
感兴趣的同学还可以参考php官方文档,学习更多curl用法