Home > Backend Development > PHP Tutorial > 基于php实现七牛抓取远程图片_PHP

基于php实现七牛抓取远程图片_PHP

WBOY
Release: 2016-05-29 11:47:55
Original
692 people have browsed it

由于公司网站之前的用户头像都是存储在自己的服务器上的,后来感觉管理不方便,新增加的用户头像都上传到了七牛,为了方便统一管理,领导说把本地服务器的头像全部迁移到了七牛。

1.梳理下思路

先判断用户的头像是否在七牛,若不存在,本地如果有则抓取到七牛,然后进行批量抓取

2.七牛判断图片是否存在

 /**
    * 查看七牛url是否存在
    * @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 ;//桶
     $key=substr($parts['path'], );//七牛文件名
    //密钥
     $accessKey ; 
     $secretKey ;
     Qiniu_SetKeys($accessKey, $secretKey);
     $client = new Qiniu_MacHttpClient(null);
     list($ret, $err) = Qiniu_RS_Stat($client, $bucket, $key);//查看单个文件信息
     if ($err !== null) {
       return false;
     } else {
       return true;
     }
   }
Copy after login

3.移动单个文件到七牛

 /**
    * 移动单个头像到七牛
    * @param string $remotePath 远程文件在服务器上的物理地址
    * @param string $qiniuPath 七牛文件地址
    */
   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 远程文件访问地址
       *$bucket桶
       *$key 存储在七牛的文件名
       */
       $ret = Qiniu_RS_Fetch($client,$accessUrl, $bucket, $key);
       if($ret != NULL)
       {
         $errlog = 'fail.log';//写错误日志
         file_put_contents($errlog,  "{$key}\n", FILE_APPEND);
       }
       else
       {
         var_dump($ret);
       }
     }else{
       var_dump('已有头像');
     }
   }
Copy after login

4.批量抓取

function t(){
    header("Content-type: text/html; charset=utf-8");
    set_time_limit(0);
    $start = intval($_GET['num']);//开始位置
    $str = '';
    /*从数据库查询记录*/
    $link = mysql_connect("localhost", "root", "root") or die("error");
    $db_selected = mysql_select_db("test", $link);
    mysql_query ("SET NAMES UTF8");
    $limit = 10;//每次查询数
     $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'], $row['qiniuPath']);
      }  
      $start += 10;
      $url = "/t?num={$start}";
      $str = '已经更新'.$start.'条<br />';
      $str .= '<script>location.href="'.$url.'";</script>';//更新$limit条,跳转防止脚本无响应
    }
    else
    {
      echo '结束';
    } 
    echo $str;
    ob_flush();
    flush();
    sleep(2);
    unset($sql,$result,$row);
  }
Copy after login

以上内容是基于php实现七牛抓取远程图片的全部内容,希望大家喜欢。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template