Tiny URL using PHP for auto-twitts

1.3k views Asked by At

I'm trying to implement auto post on Twitter using PHP.

But because of twitt's character limit I wanted to know how can I use tinyURL, instead of placing the full link, like http://www.appdropp.com/ios/stone-age-the-board-game/564247778

You see it's quite long...

I know services like:

  • tinyurl.com
  • goo.gl

But how can I use these services in bulk, to generate hundreds of links every day with PHP?

2

There are 2 answers

0
Dmitri A On BEST ANSWER

You can check Google API, but I'm not sure about that much bulk. I can suggest 3 solutions for you:

  1. Create short links on your host using PHP.
  2. Twitter will shorten the URL for you if you tweet DIRECTLY. But, you cannot do it using auto-posting. So, count characters (like message + URL <= 140) and keep your automatic tweets length less than 140 characters.
  3. You can also try this (Check the PHP Source Code Example)

    function CompressURL($url) {
       $ch = curl_init();
       curl_setopt($ch, CURLOPT_URL, "http://"."to.ly/api.php?longurl=".urlencode($url));
       curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
       curl_setopt($ch, CURLOPT_HEADER, 0);
    
       $shorturl = curl_exec ($ch);
       curl_close ($ch);
    
       return $shorturl;
    }
    echo CompressURL("http://twitter.com"); // Test
    
0
The Mighty Programmer On

Note that This method depends upon TinyURL page structure which may be changed in near future and don't use it in that much bulk Or ask them for API ?

You can use this way.

  1. Encode your url.
    use urlencode
  2. Add your encode url to
    $url='http://tinyurl.com/create.php?source=indexpage&url=<encoded url>
  3. Create a dom object
    $doc=new DomDocuement();
  4. Load the page.
    $doc->loadHTMLFile($url); // this is page containing shorten url
  5. Grab the node that contains shorten url ..Second blockquote contains shorten url
    $nodelist=$doc->getElementsByTagName('blockquote');
    $blockquote=$nodelist->item(1) // grabbing shorten url blockquote 0:first 1:second
  6. Now Grab shorten url :
    $shorten_url=$blockquote->$firstChild->NodeValue
  7. Use any where you like.

    For more info see tiny url page structure
    Read more about DOMDocument
    For goo.gl, read: https://developers.google.com/url-shortener