PHP Multi-threaded crawl Web page code sharing

Source: Internet
Author: User
Tags php language
This article introduces, with PHP implementation of multi-threaded crawl Web page code, there is a need for friends reference.

In PHP, you can use curl to perform various transfers of files, such as simulating a browser sending a get,post request. The PHP language itself does not support multi-threading, so the development of the crawler is not efficient, so the use of Curl Multi Functions to achieve concurrent multi-threaded access to multiple URL addresses.

For the basic content of curl, you can refer to the following article: PHP Curl application Example Analysis PHP Curl Usage Example code PHP Curl Learning Summary

This section describes an example of using Curl Multi Functions to download files in parallel with multithreading.

Example 1, get content to write directly to a file

!--? php/*** multi-threaded Crawl Web content * Edit by bbs.it-home.org*/$urls = Array (' HTTP://WW w.sina.com.cn/', ' http://www.sohu.com/', ' http://bbs.it-home.org/');   The URL of the page to crawl $save _to= '/test.txt ';   Write file to crawl content $st = fopen ($save _to, "a");       $MH = Curl_multi_init ();     foreach ($urls as $i =--> $url) {$conn [$i] = Curl_init ($url); curl_setopt ($conn [$i], Curlopt_useragent, "mozilla/4.0 (compatible; MSIE 7.0;     Windows NT 6.0) ");     curl_setopt ($conn [$i], Curlopt_header, 0);     curl_setopt ($conn [$i], curlopt_connecttimeout,60); curl_setopt ($conn [$i], Curlopt_file, $st);   Writes the crawled code to the file Curl_multi_add_handle ($MH, $conn [$i]);   }//Initialize do {curl_multi_exec ($MH, $active);  } while ($active);     Executes a foreach ($urls as $i = = $url) {curl_multi_remove_handle ($MH, $conn [$i]);   Curl_close ($conn [$i]);   }//End cleanup Curl_multi_close ($MH); Fclose ($st);; 

Example 2, get content into a variable, and then write to a file

     $url) {$conn [$i] = Curl_init ($url); curl_setopt ($conn [$i], Curlopt_useragent, "mozilla/4.0 (compatible; MSIE 7.0;     Windows NT 6.0) ");     curl_setopt ($conn [$i], Curlopt_header, 0);     curl_setopt ($conn [$i], curlopt_connecttimeout,60);  curl_setopt ($conn [$i],curlopt_returntransfer,true);   Set the crawl substitution code into a string, not output to the browser curl_multi_add_handle ($MH, $conn [$i]);   } do {curl_multi_exec ($MH, $active);        } while ($active);  foreach ($urls as $i + $url) {$data = Curl_multi_getcontent ($conn [$i]);//Get Crawled Code string fwrite ($st, $data); Writes a string to the file.   It is also possible to deposit the database.     }//Get data variable and write to file foreach ($urls as $i + = $url) {curl_multi_remove_handle ($MH, $conn [$i]);   Curl_close ($conn [$i]);   } curl_multi_close ($MH);  Fclose ($st); ?>
  • 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.