PHP to achieve long link Sina converted into short links API interface code share

We may receive a message like this, and found that the link is not a conventional URL link, but a dapper short links, products often need such a demand, if issued to users of SMS is a very long connection, the user experience is certainly bad, so we need to achieve a long connection converted into short links. .

Today, I'll give a fight to share the Sina Tencent method call php short link / short link interface using the AP. Share to you for your use and reference.

interface address

Sina Short URL address of the interface: 

http://www.sinadwz.cn/sina.php?url_long=http://www.baidu.com

Description: the replacement section upper links marked red short URL api interface connector into the desired length can be shortened!


Document Interface

PHP calling code:

$url = 'http://www.baidu.com';
$api_url = ''.urlencode($url);
$short_url = file_get_contents($api_url);
echo $short_url;



Call documents in other languages

JAVA calling code:

 public static void main(String path[]) throws Exception {
    URL u = new URL("http%3A%2F%2Fwww.baidu.com");
    InputStream in = u.openStream();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
    byte buf[] = new byte[1024];
    int read = 0;
    while ((read = in .read(buf)) > 0) {
    out.write(buf, 0, read);
    }
    } finally {
    if ( in != null) {
    in .close();
    }
    }
    byte b[] = out.toByteArray();
    System.out.println(new String(b, "utf-8"));
    }

Python calling code:

  import urllib, urllib2, sys
    host = ''
    path = ''
    method = 'GET'
    querys = 'http%3A%2F%2Fwww.baidu.com'
    bodys = {}
    url = host + path + '?' + querys
    request = urllib2.Request(url)
    response = urllib2.urlopen(request)
    content = response.read()
    if (content):
    print(content)

Precautions:

1, the use of api interfaces, simply link the URL = replace his side need to shorten the long link.

2, the interface supports parameter passing, when the & symbol appears in the link, use 26% instead of (or use the url encoded), otherwise carry parameters will be lost.

3, when completing the link, you must use the http (s): // protocol, or API interface will not generate short links!


Guess you like

Origin www.cnblogs.com/asda/p/11976463.html