<?php// Get link from url// Should be in format of {username}/{encoded_id}/files/{path}// {node_id} can be omitted so that it always points to the latest revision// Example: ?link=atlassian/rKGEo/files/snippet.txt$link=filter_input(INPUT_GET,'link',FILTER_SANITIZE_URL);if(empty($link)){trigger_error('Link parameter must be specified.',E_USER_ERROR);}// URL for Bitbucket Snippets API$bb='https://bitbucket.org/!api/2.0/snippets/';// Add link to Bitbucket URL$url=$bb.$link;// Temp file needed to get mime type$tmpfname=tempnam(sys_get_temp_dir(),"bitbucket_snippet_");$fp=fopen($tmpfname,"w");// Setup curl parameters for download$ch=curl_init();curl_setopt($ch,CURLOPT_URL,$url);curl_setopt($ch,CURLOPT_FILE,$fp);// Must be set to true so that PHP follows any "Location:" headercurl_setopt($ch,CURLOPT_FOLLOWLOCATION,true);// Download filecurl_exec($ch);// Close temp filefclose($fp);// Get HTTP status code$code=curl_getinfo($ch,CURLINFO_HTTP_CODE);if($code!=200){$output="No cURL data returned for $url [".$code."]";if(curl_error($ch)){$output.="\n".curl_error($ch);}trigger_error($output,E_USER_ERROR);}// Close connectioncurl_close($ch);if(filesize($tmpfname)<1){trigger_error("Error retrieving file.",E_USER_ERROR);}// Set Content Type header from file since Bitbucket does not return proper Content Type// Must use special magic file for programming files// https://bitbucket.org/snippets/mythics/yjLgz/$finfo=newfinfo(FILEINFO_MIME_TYPE,'/usr/local/apache/conf/programming.magic');$mime=$finfo->file($tmpfname);header('Content-Type: '.$mime);// Output file to browserreadfile($tmpfname);// Delete temp fileunlink($tmpfname);?>
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.