/** * Downloads a file * Returns the base filename, raw file data and mime type returned by Fileinfo * * <br />对应API: {@link http://vdisk.weibo.com/developers/index.php?module=api&action=apidoc#files_get files(GET)} * * @param string $file Path to file, relative to root, including path * @param string $outFile Filename to write the downloaded file to * @param string $revision The revision of the file to retrieve * @return array */ public function getFile($file, $outFile = false, $revision = null) { // Only allow php response format for this call if ($this->responseFormat !== 'php') { throw new Exception('This method only supports the `php` response format'); } $file = $this->encodePath($file); $params = array('rev' => $revision); $call = 'files/' . $this->root . '/' . $file . '?' . http_build_query($params); $response = $this->fetch('GET', self::API_URL, $call); $handle = null; if ($outFile !== false) { // Create a file handle if $outFile is specified if (!($handle = fopen($outFile, 'w'))) { throw new Exception("Unable to open file handle for {$outFile}"); } else { $this->OAuth->setOutFile($handle); if (isset($response['headers']['location'])) { $this->fetch('GET', $response['headers']['location']); } } } // Close the file handle if one was opened if ($handle) { fclose($handle); } return array('name' => $outFile ? $outFile : basename($file), 'mime' => $this->getMimeType($outFile ?: $response['body'], $outFile), 'meta' => json_decode($response['headers']['x-vdisk-metadata']), 'download_url' => $response['headers']['location']); }