好用的 curl 抓取 頁面的封裝函數

來源:互聯網
上載者:User

標籤: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 抓取 頁面的封裝函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.