Many students feel overwhelmed when they first use curl (including me) ), looking at these curl_setopt functions is completely confusing, but after you spend 10 minutes reading my introduction, I believe you can easily play with PHP’s curl in the future
First, please look at a curl code (take 10 seconds, skim through it, and then skip to the following)
<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, what on earth are you doing?
Want to learn this "high-end" usage?
First of all, I believe you must know that most URLs start with http. That is because they need to transmit data through http (Hypertext Transfer Protocol HTTP-Hypertext transfer protocol), but transmitting data is not simple. It's just a matter of sending "Hello" to the server. In order for the recipient to understand the actual intention of the sender and know who the sender is, the sender often has to send a lot of additional information to the recipient. , just like the sender needs to put an envelope around the letter, with various sender's information written on the envelope. All of these were eventually combined into something called a message, which formed the basis of the entire Internet.
Curl’s job is to send these messages through the http protocol (php’s libcurl currently also supports https, ftp, telnet and other protocols)
Now look at the code. In fact, the code only does five things
Here is how to crawl and submit data from any page using GET and POST methods<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> ?>
Interested students can also refer to the official PHP documentation to learn more curl usage