PHP methods for getting all links to the web page (with demo source code download) and demo source code download

Source: Internet
Author: User

PHP methods for getting all links to the web page (with demo source code download) and demo source code download

The example in this article describes how PHP obtains all the connections on the web page. We will share this with you for your reference. The details are as follows:

Function getHtml ($ url, $ charset = 'utf-8') {$ curl = curl_init (); // curl_setopt ($ curl, CURLOPT_HTTPHEADER, array ('x-FORWARDED-FOR: 192.168.168.1 ', 'client-IP: 192.168.168.1'); // IP curl_setopt ($ curl, CURLOPT_URL, $ url ); curl_setopt ($ curl, CURLOPT_REFERER, ""); // $ user_agent = isset ($ _ SERVER ['HTTP _ USER_AGENT '])? $ _ SERVER ['HTTP _ USER_AGENT ']: 'mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/100 '; curl_setopt ($ curl, CURLOPT_USERAGENT, $ user_agent); // you only need to return the HTTP header // curl_setopt ($ curl, CURLOPT_HEADER, 1 ); // The page content is not required. // curl_setopt ($ curl, CURLOPT_NOBODY, 1); // return the result instead of curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1 ); $ html = curl_exec ($ curl); // $ info = Curl_getinfo ($ curl); // echo var_dump ($ info); if ($ html = false) {// echo "cURL Error :". curl_error ($ ch); return '';} curl_close ($ curl); if ($ charset! = 'Utf-8') {$ html = iconv ($ charset, "UTF-8", $ html);} return $ html;} header ("Content-type: text/html; charset = UTF-8 "); include ('simple _ html_dom.php '); // to open extension = php_mbstring.dll // $ url = 'HTTP: // www.baidu.com/s? Wd = kaka '; $ url = 'HTTP: // www.163.com/'{{str_html = getHtml ($ url, 'gbk'); $ html = str_get_html ($ str_html ); $ links = $ html-> find ('A'); foreach ($ links as $ link) {$ txt = trim ($ link-> plaintext ); echo $ link-> href. '['. $ txt. '] <br>';} $ html = null;

Click here to download the complete instance code.

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.