Argh,
I have no result with the $contentType and $mimeType.
Hard to retrieve file extension based on the mime type or content type !
I will continue to search
Edit 1 : Good News, I am now able to generate short URL :
http://bit.ly/eR7Y3G (it’s a demo url). But it’s incomplete, I need the document extension.
Here is my code, I think it needs some tweaking and security checks
<?php
/* Retrieve properties from DB */
$Bitly_KEY = $modx->getOption('Bitly_KEY', $scriptProperties, '');
$Bitly_LOGIN = $modx->getOption('Bitly_LOGIN', $scriptProperties, '');
$siteURL = $modx->getOption('site_url');
$docInfo = $modx->getObject('modResource',18);
$docname = $docInfo->get('alias');
//damn I am unable to retrieve file extension
/* Check if it is in folder */
$parent = $docInfo->get('parent');
$path='';
/* Checks if everything is correct */
if (empty($Bitly_KEY)){
$output="-No Bit.ly key, please create an account @ bit.ly website";
return $output;
die();
}
else
{
$output=$Bitly_KEY;
}
if (empty($Bitly_LOGIN)){
$output="-No Bit.ly key, please create an account @ bit.ly website";
return $output;
die();
}
else
{
$output=$Bitly_LOGIN;
}
//loop through parents
while ($parent != 0) {
$docInfo = $modx->getObject('modResource',$parent);
$alias = $docInfo->get('alias');
$path = $alias . '/' . $path;
$parent = $docInfo->get('parent');
}
$longurl = $siteURL . $path . $docname . $extension;
$bitly = 'http://api.bit.ly/shorten?version=2.0.1&longUrl='.urlencode($longurl).'&login='.$Bitly_LOGIN.'&apiKey='.$Bitly_KEY.'&format=xml';
$response = file_get_contents($bitly);
//credits to davidwalsh.name
//parse depending on desired format
if(strtolower('xml') == 'json'){
$json = @json_decode($response,true);
return $json['results'][$longurl]['shortUrl'];
}
else //xml
{
$xml = simplexml_load_string($response);
return 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
}