標籤:init 數組 log get 方法 set 參數 out 技術分享
由於經常使用php curl 抓取頁面的內容,在此mark 平時自己封裝的 curl函數,(其實 現在也開始用 Python 來爬了~ ^-^)
/** * 封裝curl方法 * @author FredGui * @param string $url 必選 介面地址 * @param string $post 可選 如果是post訪問填寫post參數數組 * @param int $timeout 可選 逾時時間 * @param string $cookie * @param int $decode * @return mixed|null */function curlHtml($url, $post = ‘‘, $timeout = 30, $cookie = ‘‘, $decode = 1){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); if ($post) { curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); } curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); if ($cookie) { curl_setopt($ch, CURLOPT_COOKIE, $cookie); } $data = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($httpCode == ‘200‘) { if ($decode == 1 && !is_null(json_decode($data))) { $data = json_decode($data, true); } } else { $data = NULL; } curl_close($ch); return $data;}$html = curlHtml(‘http://www.baidu.com‘);echo ‘<pre>‘;//var_dump($html);
var_dump(strip_tags($html));exit;
把 頁面內的 html標籤去掉了:如下(網頁的所有內容)
本文地址:http://www.cnblogs.com/guixiaoming/p/6424160.html
好用的 curl 抓取 頁面的封裝函數