溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

使用php怎么遠(yuǎn)程抓取七牛圖片

發(fā)布時(shí)間:2021-01-26 15:14:27 來(lái)源:億速云 閱讀:181 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)使用php怎么遠(yuǎn)程抓取七牛圖片,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

1.梳理下思路

先判斷用戶的頭像是否在七牛,若不存在,本地如果有則抓取到七牛,然后進(jìn)行批量抓取

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);//查看單個(gè)文件信息
     if ($err !== null) {
       return false;
     } else {
       return true;
     }
   }

3.移動(dòng)單個(gè)文件到七牛

 /**
    * 移動(dòng)單個(gè)頭像到七牛
    * @param string $remotePath 遠(yuǎn)程文件在服務(wù)器上的物理地址
    * @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 遠(yuǎn)程文件訪問(wèn)地址
       *$bucket桶
       *$key 存儲(chǔ)在七牛的文件名
       */
       $ret = Qiniu_RS_Fetch($client,$accessUrl, $bucket, $key);
       if($ret != NULL)
       {
         $errlog = 'fail.log';//寫錯(cuò)誤日志
         file_put_contents($errlog,  "{$key}\n", FILE_APPEND);
       }
       else
       {
         var_dump($ret);
       }
     }else{
       var_dump('已有頭像');
     }
   }

4.批量抓取

function t(){
    header("Content-type: text/html; charset=utf-8");
    set_time_limit(0);
    $start = intval($_GET['num']);//開(kāi)始位置
    $str = '';
    /*從數(shù)據(jù)庫(kù)查詢記錄*/
    $link = mysql_connect("localhost", "root", "root") or die("error");
    $db_selected = mysql_select_db("test", $link);
    mysql_query ("SET NAMES UTF8");
    $limit = 10;//每次查詢數(shù)
     $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 = '已經(jīng)更新'.$start.'條<br />';
      $str .= '<script>location.href="'.$url.'";</script>';//更新$limit條,跳轉(zhuǎn)防止腳本無(wú)響應(yīng)
    }
    else
    {
      echo '結(jié)束';
    } 
    echo $str;
    ob_flush();
    flush();
    sleep(2);
    unset($sql,$result,$row);
  }

關(guān)于使用php怎么遠(yuǎn)程抓取七牛圖片就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI