七牛云异步第三方资源抓取
七牛云的文档,这块写的不太行呀,坑了我一早上之后才搞完
/* * 七牛云异步第三方资源抓取 * $img_url 文件url * @return Qiniu\Http\Response Object */ public function qiniuFetch($img_url) { $secrectKey = set('secrectKey'); $accessKey = set('accessKey'); $qiniubucket = set('qiniubucket'); $qiniudomain = set('qiniudomain'); $zone = 'z2'; // 华东:z0;华北z1;华南:z2;北美:na0;东南亚:as0 $file_dir = 'article/'.date('Ymd').'/'; // 文件路径 $file_type = pathinfo(parse_url($img_url)['path'])['extension'];// 文件类型(文件直接上传,七牛云不会指定文件类型,需要自己定义) $file_name = time() . uniqid().'.'.$file_type; // 文件名 $bodyArr = ["url" => $img_url, "bucket" => $qiniubucket, "key" => $file_dir . $file_name]; $body = json_encode($bodyArr); $this->auth = new Auth($accessKey, $secrectKey); $url = 'http://api-'.$zone.'.qiniu.com/sisyphus/fetch'; $method = 'POST'; $contentType = 'application/json'; $rtcToken = $this->auth->authorizationV2($url, $method, $body, $contentType); $rtcToken['Content-Type'] = $contentType; $result = Client::post($url, $body, $rtcToken); // 成功则返回七牛云url,失败则返回原url if ($result->statusCode == 200) { $data['img_yun_url'] = 'http://'.$qiniudomain.'/'.$file_dir . $file_name; } else { $data['img_yun_url'] = $img_url; } return $data; }