萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> PHP cURL初始化和執行方法入門級代碼

PHP cURL初始化和執行方法入門級代碼

   這篇文章主要介紹了PHP cURL初始化和執行方法入門級代碼,本文直接給出代碼示例,代碼中包含詳細注釋,需要的朋友可以參考下

  這個是采集基礎,最好熟悉一下

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 $ch = curl_init(); # 設定url和把結果返回,是否返回頭部 curl_setopt($ch, CURLOPT_URL, 'http://www.baidu.com/'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($this->ch, CURLOPT_HEADER, 1);   # cookie文件設定 curl_setopt($this->ch, CURLOPT_COOKIEJAR, $cookie_file); curl_setopt($this->ch, CURLOPT_COOKIEFILE, $cookie_file);   # 額外頭部 curl_setopt($this->ch, CURLOPT_HTTPHEADER, array('User-Agent: Mozilla/5.0'));   # 設定post curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $poststring);   # 連接、執行過期時間 curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, 5); curl_setopt($this->ch, CURLOPT_TIMEOUT, 30);   # 是否跟隨301 302 curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($this->ch, CURLOPT_MAXREDIRS, 10);   # refer curl_setopt($this->ch, CURLOPT_REFERER, $refer);   # http版本和端口重用設置 curl_setopt($this->ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); curl_setopt($this->ch, CURLOPT_FORBID_REUSE, 1);   # 支持https curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, 0);   # 如果需要進行毫秒超時,需要增加: curl_setopt($this->ch, CURLOPT_NOSIGNAL, 1);   # 執行 $response = curl_exec($ch); if(curl_errno($ch)){ curl_error($ch); exit(); } curl_close($ch);
copyright © 萬盛學電腦網 all rights reserved