Implementation of seven cattle crawl remote picture _php based on PHP example

Source: Internet
Author: User
Tags php example

Because the company's website before the user head is stored in their own server, and later feel the management is inconvenient, the new user avatar has been uploaded to the seven cattle, in order to facilitate the unified management, the leader said the local server head all migrated to seven cattle.

1. Comb the idea

First judge the user's head whether in seven cattle, if not, if there is a local grab to seven cows, and then bulk crawl

2. Seven cattle to determine whether the picture exists

 /**
    * View seven cattle URL exists
    * @param string $url
   /function url_exists ($url) {
     require_once (common_path.) Qiniu/rs.php ");
     Require_once (Common_path.) Qiniu/http.php ");
     $parts =parse_url ($url);
     $bucket//barrel
     $key =substr ($parts [' path '],);//Seven cow filename
    //Key
     $accessKey; 
     $secretKey;
     Qiniu_setkeys ($accessKey, $secretKey);
     $client = new qiniu_machttpclient (null);
     List ($ret, $err) = Qiniu_rs_stat ($client, $bucket, $key);//view Single file information
     if ($err!== null) {return
       false;
     } E LSE {return
       true;
     }
   }

3. Move a single file to seven cows

 /**
    * Move a single head to seven cattle
    * @param string $remotePath The physical address of the remote file on the server
    * @param string $qiniuPath Seven cattle file address
   * * function Onefetch ($remotePath, $qiniuPath) {
     if (! $this->url_exists ($qiniuPath) && file_exists ($ RemotePath)) {
       require_once (common_path.) Qiniu/rs.php ");
       Require_once (Common_path.) Qiniu/http.php ");
       Require_once (Common_path.) Qiniu/conf.php ");
       $client = new Qiniu_machttpclient (NULL);
       /* $ACCESSURL remote file access address
       * $bucket Bucket
       * $key the filename stored in seven
       cattle
       /$ret = Qiniu_rs_fetch ($client, $ACCESSURL, $ Bucket, $key);
       if ($ret!= NULL)
       {
         $errlog = ' fail.log ';//write error log
         file_put_contents ($errlog,  "{$key}\n", File_ APPEND);
       }
       else
       {
         var_dump ($ret);
       }
     } else{
       var_dump (' existing Avatar ');
     }
   

4. Batch Crawl

 function t () {header ("content-type:text/html;
    Charset=utf-8 ");
    Set_time_limit (0);
    $start = intval ($_get[' num ']);//start position $str = ';
    * * from the database query record/$link = mysql_connect ("localhost", "root", "root") or Die ("error");
    $db _selected = mysql_select_db ("Test", $link);
    mysql_query ("SET NAMES UTF8"); 
     $limit = 10;//per query number $sql = "SELECT * from test ORDER BY id desc limit {$start},{$limit}";
    $result = mysql_query ($sql) or Die (Mysql_error ()); if (mysql_num_rows ($result)) {while ($row =mysql_fetch_assoc ($result)) {Onefetch ($row [' RemotePath '], $ro
      w[' Qiniupath ']);
      } $start + 10;
      $url = "/t?num={$start}";
      $str = ' has been updated '. $start. ' <br/> '; $str. = ' <script>location.href= '. $url. ' ";
    </script>//Update $limit bar, jump prevent script no response} else {echo ' end ';
    Echo $str;
    Ob_flush ();
    Flush ();
    Sleep (2);
  Unset ($sql, $result, $row); }

The above content is based on PHP to achieve seven cattle crawl remote picture of the entire content, I hope you like.

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.