Hi guys, its me again.
I was wondering…
I have an upload site that outputs direct links to upload files. Would it be possible to output the URL’s as shortened bit.ly (or other site) shortened links?
Thanks
Hi guys, its me again.
I was wondering…
I have an upload site that outputs direct links to upload files. Would it be possible to output the URL’s as shortened bit.ly (or other site) shortened links?
Thanks
Yes, just use bit.ly’s API.
When a new file is uploaded, have the URL you would assign be passed to the API, then save it in your DB as well so you can check it later if you ever need too, but also provide it to the user on your confirmation/successful page.
Bear with me.
So i have the API key, and uploaded files final URL is called $final in my code
So i want to have something in place that converts $final (long url) to bit.ly short URL. Would this work?
function bitly($url) {
$content = file_get_contents(“http://api.bit.ly/v3/shorten?login=myloginname
&apiKey=myAPI KEY
&longUrl=”.$final.“&format=xml”);
$element = new SimpleXmlElement($content);
$bitly = $element->data->url;
if($bitly){
echo $bitly;}
else{
echo ‘0’;
}
}
urlencode $final,
and you’re better off telling it to format it as txt, and just echo the result.
function bitly($url) {
$content = file_get_contents(“http://api.bit.ly/v3/shorten?login=myloginname
&apiKey=myAPI KEY
&longUrl=”.$final.“&format=txt”);
$element = new SimpleXmlElement($content);
$bitly = $element->data->url;
if($bitly){
echo $bitly;}
else{
echo ‘0’;
}
}
Ive altered the format to txt, not quite sure on the urlencode?
[FPHP]urlencode[/FPHP]
and if you’re getting it by txt, you dont need the xml stuff ($element or $bitly), just echo content.
Thanks Starlion, im with you now.
It seems to work when you try it manually, the page echo’s out a shortened URL.
Echoing the content (shortened URL), just echo $content; ?
E.g
http://api.bit.ly/v3/shorten
?login=NAME
&apiKey=API_KEY
&longUrl=http://wwwdomain.com/email.txt
&format=txt
So just to update (learning as we go)…my current code stands as
function bitly($final) {
$content = file_get_contents(“http://api.bit.ly/v3/shorten?login=NAME
&apiKey=API_KEY
&longUrl=‘.urlencode($final).’&format=txt”);
}
And im echoing out $content elsewhere which it doesnt seem to do (blank)
Once you leave the function, $content does not exist - it is defined for the duration of the function only. Variable scope.
Thanks once again. Even when it is all together (i have been trying in the mean time all sorts of things):
//Input type because i want a text box with the link in it.
<input type=“text” size=“28” onClick=select() value="<?php
function bitly($final) {
$content = file_get_contents(“http://api.bit.ly/v3/shorten?login=NAME
&apiKey=API_KEY
&longUrl=‘.urlencode($final).’&format=txt”);
} echo $content; ?>" READONLY><p />
It still echos a blank text box.
I appreciate i suck, but i am trying.
your function ends at the }. Move the } to the other side of your echo line.
for that matter, if you’re trying to actually do it there, why is it wrapped in a function at all?
You mean just have it standing as:
<?php
$content = file_get_contents(“http://api.bit.ly/v3/shorten?login=NAME
&apiKey=API_KEY
&longUrl=‘.urlencode($final).’&format=txt”);
?>
//Input type because i want a text box with the link in it.
<input type=“text” size=“28” onClick=select() value=“<?php echo $content; ?>” READONLY><p />
As oppose to:
function bitly($final) {
$content = file_get_contents(“http://api.bit.ly/v3/shorten?login=NAME
&apiKey=API_KEY
&longUrl=‘.urlencode($final).’&format=txt”);
echo $content;
}
?>" READONLY><p />
For some reason, it doesnt want to echo it out.