国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

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

2024-05-04 23:35:44
字體:
來源:轉載
供稿:網友

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

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

 

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 克山县| 九台市| 广南县| 肥东县| 宁远县| 法库县| 吉林市| 柘城县| 广德县| 汉沽区| 八宿县| 太原市| 兴文县| 九龙县| 黄冈市| 土默特左旗| 蛟河市| 马龙县| 铁岭县| 高州市| 衡水市| 资兴市| 伊吾县| 璧山县| 乌兰察布市| 滁州市| 通许县| 彭山县| 新龙县| 青岛市| 民丰县| 津市市| 曲水县| 大关县| 清流县| 定安县| 台中市| 沙湾县| 临沧市| 雷州市| 文成县|