PHP through curl to achieve the timing task of the Image capture function Example _php tips

Source: Internet
Author: User
Tags foreach curl php programming php regular expression regular expression

The example of this article tells the PHP through curl to realize the timing task of the image capture function. Share to everyone for your reference, specific as follows:

below for you to introduce a PHP timing task through the curl image of the capture example, hope that the example to everyone help, the basic idea is through a URL connection, will all the pictures of the address crawl down, and then loop open the picture, using the file operation function download down, save to the local, The alt attribute of the picture is also crawled down, and finally the data is saved to its own database.

Nonsense not to say, look at the program can understand, which, need to use PHP timed task and PHP a Third-party plug-in simple_html_dom.php use, reference simple_html_dom download and use.

<?php function GetLink ($url) {include_once (' simple_html_dom.php ');
  $ch = Curl_init ();
  curl_setopt ($ch, Curlopt_url, $url);
  curl_setopt ($ch, Curlopt_header,false);
  curl_setopt ($ch, curlopt_returntransfer,1);
  $output = curl_exec ($ch);
  Curl_close ($ch);
  $html = new Simple_html_dom ();
 $html->load ($output);
  $links = Array ();
 $arr = Array ();
  $title = Array (); foreach ($html->find (' a ') as $element) {if (Preg_match (' #^\/content_[0-9]+_1\.html$ #i ', $element->href)) {Arra
  Y_push ($links, ' http://www.jb51.net '. $element->href);
 Array_push ($title, $element->title);
 }} $links = Array_values (Array_unique ($links));
 $title = Array_values (Array_unique ($title));
 $arr [' links '] = $links;
 $arr [' title '] = $title;
 return $arr;
 function loadimg ($url, $dirname) {include_once (' simple_html_dom.php ');
 $ch = Curl_init ();
 curl_setopt ($ch, Curlopt_url, $url);
 curl_setopt ($ch, Curlopt_header,false);
 curl_setopt ($ch, curlopt_returntransfer,1); $output = Curl_exec ($CH);
 Curl_close ($ch);
 $html = new Simple_html_dom ();
 $html->load ($output);
 $arr = Array ();
 foreach ($html->find (' img[w] ') as $element) {$image = $element->src;
  } $data = file_get_contents ($image);
    $info = getimagesize ($image);//Get picture information, size, format switch ($info [2]) {Case 1: $str = ' gif ';
   Break
    Case 2: $str = ' jpg ';
   Break
    Case 3: $str = ' png ';
   Break
    Default:continue;
  Break } if ($info [1] < | | $info [0] < continue;//picture is too small, not a valuable picture, skip this cycle $filename = time (). Rand (1,999999).
  $STR;
  if (!is_dir ($dirname)) {mkdir ($dirname, 0777,true);
  } $fp = fopen ($dirname. $filename, ' W ');
  Fwrite ($fp, $data);
  Fclose ($FP);
return $dirname. $filename;
  } do{set_time_limit (0);
  Ignore_user_abort ();
  $img = GetLink (' http://www.jb51.net/qutu_1.html ');
  $count = count ($img [' links ']);
  $arr = Array ();
  For ($i =0 $i < $count; $i + +) {$arr []=loadimg ($img [' Links '] [$i], ' images/');
  $img [' url '] = $arr; Echo ' <br/> ';
  $img [' title '];
  $res = Array ();
  $len = count ($img [' title ']);
  Re-assemble the data into our common two-dimensional array, facilitate the database processing of data for ($i =0; $i < $len; $i + +) {$res [$i] [' title '] = $img [' title '] [$i];
  $res [$i] [' url '] = $img [' url '] [$i]; foreach ($res as $item) {echo '  '. $item [title]. '
  <br/> ';
  } $interval = 24*3600;
  Sleep ($interval);

 }while (true);?>

More about PHP Interested readers can view the site topics: "Php Curl Usage Summary", "PHP array" operation Skills Daquan, "PHP Sorting algorithm Summary", "PHP common traversal algorithm and skills summary", "PHP Data structure and algorithm tutorial", " PHP Programming algorithm Summary, "PHP Mathematical Calculation Skills Summary", "PHP Regular Expression Usage summary", "PHP operation and operator Usage Summary", "PHP string (String) Usage summary" and "PHP common database Operation skill Summary"

I hope this article will help you with the PHP program design.

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.