TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Script Giveaway (http://www.talkphp.com/script-giveaway/)
-   -   Reverse TinyURLs (http://www.talkphp.com/script-giveaway/1693-reverse-tinyurls.html)

Wildhoney 12-10-2007 10:27 AM

Reverse TinyURLs
 
php Code:
function tinyurl_reverse($szAddress)
{
    $szAddress = explode('.com/', $szAddress);
    $szAddress = 'http://preview.tinyurl.com/'.$szAddress[1];
    $szDocument = file_get_contents($szAddress);
    preg_match('~redirecturl" href="(.*)">~i', $szDocument, $aMatches);
   
    if(isset($aMatches[1]))
    {
        return $aMatches[1];
    }
   
    return null;
}

echo tinyurl_reverse('http://tinyurl.com/388onbs');

Snazzy ^^

Haris 12-10-2007 10:31 AM

Cool, now all we need is a library. :-(

Wildhoney 12-10-2007 10:45 AM

You do have a point ;-) I'll have to get onto it this week sometime.

Village Idiot 12-11-2007 05:16 AM

Nice! (char limit)

deflated 12-29-2007 05:52 AM

Quote:

Originally Posted by Haris (Post 6153)
Cool, now all we need is a library. :-(

What sort of a library do you think of? A class with the ability to be extended by further link encryption services? If yes, I could contribute some functions but I'm still wondering about the legality of those decryption functions. There are services prohibiting the development and the usage of those 'tools'.
It's not that much work to develop such a class because they are quite simple. I think we only need one method which we pass the URL.

cachepl0x 09-26-2009 01:10 PM

PHP Code:

<?php

final class Main
{
    protected static 
$result$address null;
    
    protected function 
__construct() {}
    protected function     
__clone() {}
    
    public static function 
Prepare($address)
    {
        
$address  explode('.com/'$address);
        
$address  'http://preview.tinyurl.com/' $address[1];
        
$document file_get_contents($address);
        
        
preg_match ('~redirecturl" href="(.*)">~i'$document$matches);
    
        if (isset(
$matches[1])) return $matches[1];
        
        return 
null;
    }
    
    public static function 
Result($address)
    {
        if (!isset(
            
self::$address)) {
            
self::$address self::Prepare($address);
        }
        echo 
"<a href='" self::$address "'>" self::$address "</a>";
    }
}

?>

<form action="<?php echo $PHP_SELF;?>" method="post">
<input type="text" name="url" />
<input type="submit" />
</form>

<?php Main::Result($_POST['url']); ?>

Just sayin. 8-)

Salathe 09-26-2009 01:52 PM

I know this is an old topic, but given the recent bump it should be ok to have a quick chat on the subject.

When trying to resolve where TinyURLs are supposed to go, wouldn't it make more sense just to send a HEAD request to the TinyURL and see where it points rather than scraping the service-specific preview page in the hope the HTML reflects what you want and the value there is accurate.

The HEAD request method should also work across a wide number of URL shortening services making your code much easier if you wish/need to handle multiple services.

ETbyrne 09-26-2009 09:54 PM

I recently made a script that works for all URL shortening services: http://www.evanbot.com/article/url-lengthener/25

It uses cURL to get the redirect headers to find the original URL.

adamdecaf 09-27-2009 04:45 PM

I'm not sure if you want this comment on your blog.

This script has a great starting direction. For example, if you wanted to give users the visibility of the long url, and still give the short url service the correct traffic you could do something like this.

PHP Code:

<?php

// Grab the link
$url $_POST['url'];

// Now get the destination.
require 'longurl.php';
$send_to longurl::get('http://tinyurl.com/past9o');

// Now print out the link
echo '<a href="' $url '">' $send_to '</a>';

?>

One major problem is users and search engines would see this and most likely think that the site is trying to send users to the wrong page.

You could however do something similar to whitehouse.gov and display a popup whenever the user wants to visit a short URL.

Better yet, when the user click the short url you could have a default setting that changes the <a>'s visible text to that longer URL. (The href="" could change based on the user preference.) This would give users added security and comfort in where they are headed.

ETbyrne 09-28-2009 01:07 AM

That's not an issue with the script, but rather an issue with your implementation. Simply make the href attribute the long URL and the link text same as the original link text to avoid confusion. Remember the script only gets the URL, it is up to you implement it correctly.

adamdecaf 09-28-2009 01:18 AM

Quote:

Originally Posted by ETbyrne (Post 28568)
That's not an issue with the script, but rather an issue with your implementation. Simply make the href attribute the long URL and the link text same as the original link text to avoid confusion. Remember the script only gets the URL, it is up to you implement it correctly.

I guess you didn't understand what I was saying. A search engine and advanced user would notice that the <a>.innerHTML is different from the href="", thus they would not click it and vote it lower in results.

ETbyrne 09-28-2009 09:37 PM

No I understood you correctly... Maybe a good example would help:

PHP Code:

$long_url longurl::get('http://tinyurl.com/past9o');

echo 
"<a href='$long_url'>link text</a>"

Notice I use the long URL for the href attribute and I do not use the short URL for the link text.

ioan1k 09-28-2009 09:59 PM

Wouldn't this defeat the purpose of the tinyUrl in the first place,

I could see getting the original link back to parse as you wish, but the user is posting it as that is how they want it displayed ... to get link text you would need to parse the URL and get it from there (unless you have a WYSIWYG) that changes the link text ( this to would defeat the purpose of tinyURL parsing.. )
if the link has a list of parameters that are added to the query string in the HREF attribute they will still be weary of the link, especially if it is not a trusted site .... ?

adamdecaf 09-28-2009 11:21 PM

Quote:

Originally Posted by ETbyrne (Post 28588)
No I understood you correctly... Maybe a good example would help:

PHP Code:

$long_url longurl::get('http://tinyurl.com/past9o');

echo 
"<a href='$long_url'>link text</a>"

Notice I use the long URL for the href attribute and I do not use the short URL for the link text.

I'm talking about showing users the full link but then directing them to the short link. This will give (sadly) most users the confidence of going to a safe link, while giving that short url service the proper hits.

The issue comes into play when search engines and advanced users see what is going on, both when presented with two different links will become paranoid. The advanced user may copy the visible text and paste that in, while the search engine may mark the page as spam/malicious.

My goal was to give all parties involved the best outcome, but in further contemplation I've decided that doing something similar to search.twitter.com would be better. The user should have the option to expand the link (for security), and the search engine should be able to mark the page as safe.

ETbyrne 09-29-2009 05:23 PM

If I remember correctly, this topic was about finding where tinyURLs went. That's what my script does. No more, no less.

How useful it is (or isn't) depends on how you use it. A good example of implementation would be say for blog comment posts, where the comment author may provide a link to his/her website. In this case you may want to automatically convert the URL to its full version if given a short URL.

hebeyl 07-05-2012 01:28 AM

I'll have to get onto it this week sometime.....








__________________________________________
We cease loving ourselves if no one loves us
WOW Gold


All times are GMT. The time now is 03:30 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0