1. 程式人生 > >php curl請求頁面數據

php curl請求頁面數據

transfer init pty code 提交 tput col close field

  1    /**
  2      *
  3      * [curl_post post方式請求]
  4      *
  5      * @param  [type] $url  [description]
  6      *
  7      * @param  string $data [description]
  8      *
  9      * @return [type]       [description]
 10      *
 11      */
 12 
 13 
 14     protected function curl_post($url,$data
=‘‘) 15 16 { 17 18 if(empty($url) || empty($data)){ 19 20 return false; 21 22 } 23 24 $postUrl = $url; 25 26 $curlPost = $data; 27 28 //初始化curl 29 30 $ch = curl_init(); 31 32 //抓取指定網頁 33 34 curl_setopt($ch
, CURLOPT_URL,$postUrl); 35 36 //設置header 37 38 curl_setopt($ch, CURLOPT_HEADER, 0); 39 40 //要求結果為字符串且輸出到屏幕上 41 42 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 43 44 //post提交方式 45 46 curl_setopt($ch, CURLOPT_POST, 1); 47 48 curl_setopt($ch
, CURLOPT_POSTFIELDS, $curlPost); 49 50 //運行curl 51 52 $data = curl_exec($ch); 53 54 curl_close($ch); 55 56 57 58 return $data; 59 60 } 61 62 /** 63 * 64 * [curl_get get方式請求] 65 * 66 * @param [type] $url [description] 67 * 68 * @param string $data [description] 69 * 70 * @return [type] [description] 71 * 72 */ 73 74 protected function curl_get($url,$data=‘‘) 75 76 { 77 78 if(empty($url)){ 79 80 return false; 81 82 } 83 84 //初始化 85 86 $ch = curl_init(); 87 88 89 90 curl_setopt($ch, CURLOPT_URL,$url); 91 92 // 執行後不直接打印出來 93 94 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 95 96 curl_setopt($ch, CURLOPT_HEADER, false); 97 98 // 跳過證書檢查 99 100 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 101 102 // 不從證書中檢查SSL加密算法是否存在 103 104 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 105 106 107 108 //執行並獲取HTML文檔內容 109 110 $output = curl_exec($ch); 111 112 113 114 //釋放curl句柄 115 116 curl_close($ch); 117 118 $output = json_decode($output,1); 119 120 return $output; 121 122 }

php curl請求頁面數據