Home > API > API Stuff / 短连接API

API Stuff / 短连接API

March 1st, 2009

We actually have an API for the LiURL site, although I haven’t really documented it that well as of yet.  As we’re a brand new site, we’re busy building in functionality, so this will get expanded in further posts as we add features.

To get a Shortened URL (aka Little URL) , the call is this:

http://liurl.cn/api-create.php?url=

Which returns the short URL. If you’ve been naughty and haven’t passed a valid URL, then it will return you a big fat -1 for your efforts.

Strangely enough, we also have the diametrically opposite call of that, which (suprise suprise)  gives you a long URL from a shortened one

http://liurl.cn/api-create.php?tinyurl=

This also returns a -1 failure if the URL isn’t one we know about.

Some sample PHP code after the break

虽然我目前仍未正式书面记录,不过在LiURL网站 我们已经使用了API。由于我们是新建的品牌网站,目前我们正忙于建设网站的功能性。所以这部分我们将在以后增加网站新特性时候加以扩充。

如果需要缩短后的链接 如下所示

http://liurl.cn/api-create.php?url=

它将返回缩短后的链接。如果您没有通过正规的链接那么将会出现大大的 -1错误提示。

奇怪的是,我们也遇到了完全相反的情况,有时它会从短链接提供一个长链接(非常奇怪)。

http://liurl.cn/api-create.php?tinyurl=

如果链接不正确 它同样也会出现 -1 错误提示。

以下是一些错误后的PHP编码样本。

Example PHP Code:

<?php

function process($url,$postargs=false) {
    $username=; $password=;
    $ch = curl_init($url);
    if($postargs !== false) {
        curl_setopt ($ch, CURLOPT_POST, true);
        curl_setopt ($ch, CURLOPT_POSTFIELDS, $postargs);
    }

    if(username !== false &amp;&amp; password !== false)
    curl_setopt($ch, CURLOPT_USERPWD, username.‘:’.password);
   
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_NOBODY, 0);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   
    $response = curl_exec($ch);
   
    $responseInfo=curl_getinfo($ch);
    curl_close($ch);
   
    if( intval( $responseInfo[‘http_code’] ) == 200 )
        return $response;
    else
        return false;
}

function shorturl( $url ) {
    $request = ‘http://liurl.cn/api-create.php?url=’ . $url;
    process( $request );
}

function longurl ($url) {
    $request = ‘http://liurl.cn/api-create.php?tinyurl=’.$url;
    process( $request );
}

$shorty = shorturl (‘http://www.liurl.cn’);

print "Show me the money: " . $shorty ;

$longy = longurl ($shorty);

print "Guess what this url is: ". $shorty. " = " . $longy;

?>
 

admin API , , ,

  1. Alex
    March 10th, 2009 at 10:45 | #1

    Can you check the process function again, I get

    Fatal error: Using $this when not in object context in /Users/alex/Sites/testtiny.php on line 10

    Can’t fix it myself, don’t really understand the script.

  1. No trackbacks yet.