public function testContentMd5IsBasedOnSubsection()
 {
     $this->assertNotSame($this->body->getContentMd5(), $this->decorated->getContentMd5());
 }
Example #2
0
 /**
  * 下载文件
  *
  * @param string $url            
  * @throws Exception
  * @return array
  */
 protected function getFileByUrl($url = '')
 {
     if (filter_var($url, FILTER_VALIDATE_URL) === false) {
         throw new Exception('无效的URL');
     }
     $client = new Client($url);
     $request = $client->get();
     $request->getCurlOptions()->set(CURLOPT_SSLVERSION, 1);
     // CURL_SSLVERSION_TLSv1
     $response = $client->send($request);
     if ($response->isSuccessful()) {
         $disposition = $response->getContentDisposition();
         // $disposition = iconv('UTF-8', 'GBK//IGNORE', $disposition);
         // $reDispo = '/^.*?filename=(?<f>[^\s]+|\x22[^\x22]+\x22)\x3B?.*$/m';
         $reDispo = '/^.*?filename=(?<f>.*\\.[^\\s]+|\\x22[^\\x22]+\\x22)\\x3B?.*$/m';
         if (preg_match($reDispo, $disposition, $mDispo)) {
             $filename = trim($mDispo['f'], ' ";');
         } else {
             $filename = uniqid() . '.jpg';
         }
         $entityBody = $response->getBody();
         $filter = $entityBody->getContentEncoding();
         if ($filter !== false) {
             $entityBody->uncompress($filter);
         }
         $length = $entityBody->getContentLength();
         $objReader = new ReadLimitEntityBody($entityBody, $length);
         $fileBytes = $objReader->read($length);
         return array('name' => $filename, 'bytes' => $fileBytes);
     } else {
         throw new Exception("获取文件失败,请检查下载文件的URL是否有效");
     }
 }