PHP Curl Crawl class Share

Source: Internet
Author: User

Class usualfunfornetworkhelper{/*** * POST request data */public static function httpspost ($url, $data = null, $hea        DERs = Null) {$curl = Curl_init ();        curl_setopt ($curl, curlopt_url, $url);        curl_setopt ($curl, curlopt_ssl_verifypeer, FALSE);        curl_setopt ($curl, curlopt_ssl_verifyhost, FALSE);            If (!empty ($data)) {curl_setopt ($curl, curlopt_post, 1);        curl_setopt ($curl, curlopt_postfields, $data);        } curl_setopt ($curl, curlopt_returntransfer, 1);        $output = curl_exec ($curl);        Curl_close ($curl); Header ("content-type:text/html;        Charset=utf-8 ");    Return $output; /*** * HTTP Get access method */public static function HttpGet ($url, $headers = Null) {$curl = Curl_init        ();        curl_setopt ($curl, curlopt_url, $url);        curl_setopt ($curl, curlopt_ssl_verifypeer, FALSE);        curl_setopt ($curl, curlopt_ssl_verifyhost, FALSE); curl_setopt ($curl, Curlopt_useragenT, $_server[' http_user_agent '); Simulates user-used browser curl_setopt ($curl, curlopt_followlocation, 1); Use automatic jump curl_setopt ($curl, curlopt_autoreferer, 1); Auto set referer curl_setopt ($curl, curlopt_timeout, 30); Set timeout limit to prevent dead loops//curl_setopt ($curl, curlopt_header, 0); Displays the contents of the returned header area curl_setopt ($curl, curlopt_returntransfer, 1);        The information obtained is returned as a file stream $output = curl_exec ($curl);        Echo ' ['. $output. '] ';    Return $output; /** * Get Pictures * @param $url * @param $content _type * @return bool|int|mixed */public static func        tion httpgetimg ($url,& $content _type,& $error = ") {$content _type = ';        $count = 0;        $e _no = 28;            While ($count < && $e _no = = = 28)//network timeout, 10 repeat requests {$curl = Curl_init ();            curl_setopt ($curl, curlopt_url, $url);            curl_setopt ($curl, curlopt_ssl_verifypeer, FALSE); curl_setopt ($curl, curlopt_ssl_verifyhost, FALSE);            curl_setopt ($curl, curlopt_useragent, $_server[' http_user_agent '); Simulates user-used browser curl_setopt ($curl, curlopt_followlocation, 1); Use automatic jump curl_setopt ($curl, curlopt_autoreferer, 1); Auto set referer curl_setopt ($curl, curlopt_timeout, 5); Set timeout limit to prevent dead loops//curl_setopt ($curl, curlopt_header, 0); Displays the contents of the returned header area curl_setopt ($curl, curlopt_returntransfer, 1);            The information obtained is returned as a file stream $output = curl_exec ($curl);            $content _type = Curl_getinfo ($curl, curlinfo_content_type);            $e _no = Curl_errno ($curl);            Curl_close ($curl);        $count + +;            } if ($e _no!== 0) {$error = $e _no;            If ($e _no = = =) {log::write (' request picture Timeout 10 times, discard, url: '. $url);        } return false;        } $content _type = Strval ($content _type); $ok _type=[' application/octet-stream ', ' image/bmp ', ' image/giF ', ' image/jpeg ', ' image/png '];        If (!in_array ($content _type, $ok _type)) {return false;    } return $output;  }}

  

PHP Curl Crawl class Share

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.