Download & Extend

configuring twitter module to work with other url shortening services

Project:Twitter
Version:7.x-3.x-dev
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:active

Issue Summary

I'm curious if its possible to change the default url shrinking service in the module code? I'd like to use ow.ly because of the statistics they offer on HootSuite. Thanks. Kevin

Comments

#1

Version:6.x-2.5» 6.x-2.6

Same here. I'd like to know if this is possible as well.

#2

@twitter.module

line 150: $response = drupal_http_request("http://tinyurl.com/api-create.php?url=" . $url);

you'll just have to replace that api url with the one you are interested in.
like if you wish to go for bit.ly then just take a look at their api documentation and act accordingly.

#3

careless reader am i :)
ow.ly api is still under development from what i've read. there's a request form on their site under the Developer API link where you can sign up and be informed when it's ready.

#4

hello,

as i had the same request, but for bit.ly, I have made some modifications to the twitter module.

prerequisites:

  • bit.ly has an api , which it is necessary to subscribe. then you receive an api key.
  • bit.ly provides two formats : json and xml ( not plain text as tinyurl )

    So you can get the result with decode_json or simplexml.
    For a better compatibilty I choose the xml way ( decode_json >= php 5.2 , simplexml > = php 5.1.3 )

    The shortened url is located in node root->results->nodeKeyVal->shortUrl

    Let me know if you have found another tweak.

    It would be nice to upgrade the twitter module to provide api selection for the url shortener in administration page ...

    --------------------

    twitter.module file , line 145 , function twitter_shorten_url :

    <?php
    function twitter_shorten_url($url) {
      if (
    module_exists('shorten')) {
        return
    shorten_url($url);
      }
      else {

       
    $response = drupal_http_request("http://api.bit.ly/shorten?version=2.0.1&format=xml&longUrl=".$url."&login=YOURLOGIN&apiKey=R_YOURAPIKEY");
        if (
    $response->code == 200) {

         
    $xml_response = simplexml_load_string($response->data);
          if(
    $xml_response !== FALSE && $xml_response->errorCode == 0 )
            return
    $xml_response->results->nodeKeyVal->shortUrl   ;
          else
            return
    $url;

        }
        else {
          return
    $url;
        }
      }
    }
    ?>
  • #5

    At present, installing the 'shorten' module allows you to use any service that module supports to do te URL shortening. Basically, you tell Shorten module what service you want to use whenever it shortens URLs, and Twitter will respect its settings. At least for the 2.x branch there aren't any plans to change that; I'm assuming you'd want to override the shorten module's settings just for Twitter, correct?

    #6

    @eaton : correct, an it was 2 lines above ( if (module_exists('shorten')) ) :\

    I just regret that this option is not mentioned in the description...

    #7

    I'm looking to use the shortening service at http://s.coop

    Their documentation states:

    s.coop has easy access to its API for developers who want that bit more.

    To create a s.coop URL immediately you simply query our servers via an HTTP request and a standard JSON, XML or Simple formatted reply will contain the shortened URL.

    Example:

    http://s.coop/devapi.php?action=shorturl&url=WEBADDRESS&format=RETURN

    Simply change WEBADDRESS to the URL you want shortening

    And change RETURN to either json, xml or simple

    I'm not a coder, but this sounds almost do-able even for me. Can anyone offer any pointers to get me going on this?

    Thanks
    Graham

    #8

    Update: to use the URL shortening service at http://s.coop you should make the following change to the Twitter module code:

    change the tinyurl url to s.coop, which is on line 150 according to #2 above.

    Make a straight change from:

    $response = drupal_http_request("http://tinyurl.com/api-create.php?url=". $url);

    to

    $response = drupal_http_request("http://s.coop/devapi.php?action=shorturl&format=simple&url=". $url);

    Thanks to Anthony at http://s.coop for this one.

    #9

    Subscribing, greetings, Martijn

    #10

    Just want to add a successful bit.ly implementation.

    I didn't use the shorten module; i just edited the July 2, 2009 (6.x-2.6) version of twitter.module

    the operative code, in function twitter_shorten_url is:

        //note the use of the txt format (it was simplest)
        $base = "http://api.bit.ly/v3/shorten?login=YOUR_BITLY_LOGIN_HERE&apiKey=YOUR_BITLY_API_HERE&format=txt&longUrl=";
        //$url is the sole param of this function coming in
        $toSubmit = $base . $url;
        $response = drupal_http_request($toSubmit);
        //the rest is the same
        if ($response->code == 200) {
          return $response->data;
        } else {
          return $url;
        }

    The bit.ly v# api is here:
    http://code.google.com/p/bitly-api/wiki/ApiDocumentation#/v3/shorten

    Thanks,
    m

    #11

    *edit -- nevermind

    #12

    Version:6.x-2.6» 7.x-3.x-dev
    nobody click here