Sina Short link birth and reduction short links tutorial

Source: Internet
Author: User
Tags set time
Short link, in layman's terms, is to convert long URL urls into short URL strings through program calculations. About Sina Short link we can use the Sina Weibo short link generator, which is a gadget that can generate t.cn short links. But how do we do it if we can achieve Sina short link ourselves? In this article we will teach you how to use PHP to generate short links to Sina and restore the Sina Short link class.

<?php/** generate Sina Short link or restore Sina short link * Date November 22, 2017 * Author Www.phpernote.com*/class sinaurl{//Sina appkeyconst appkey= ' 31641035 ';//curlprivate static function curlquerystring ($url) {//Set additional HTTP header $addhead=array ("Content-type:application /json ");//Initialize curl, of course, you can also use Fsockopen instead of $curl_obj=curl_init ();//Set URL curl_setopt ($curl _obj,curlopt_url, $url);// Additional head content curl_setopt ($curl _obj,curlopt_httpheader, $addHead);//Output return header information curl_setopt ($curl _obj,curlopt_header,0) ;//Return the result of Curl_exec to curl_setopt ($curl _obj,curlopt_returntransfer,1);//Set time-out curl_setopt ($curl _obj,curlopt_ timeout,8);//execute $result=curl_exec ($curl _obj);//Turn Curl back curl_close ($curl _obj); return $result;} Handles the returned result private static function Dowithresult ($result, $field) {$result =json_decode ($result, True); return Isset ($ result[0][$field])? $result [0][$field]: ';} Gets the short link public static function Getshort ($url) {$url = ' http://api.t.sina.com.cn/short_url/shorten.json?source= '. Self: : APPKEY. ' &url_long= '. $url; $result =self::curlquerystring ($url); return self::d owithresult ($resulT, ' Url_short ');} Gets the long link public static function Getlong ($url) {$url = ' http://api.t.sina.com.cn/short_url/expand.json?source= '. Self:: APPKEY. ' &url_short= '. $url; $result =self::curlquerystring ($url); return self::d owithresult ($result, ' Url_long ');}} Use the example as follows: $result =sinaurl::getshort (' http://www.phpernote.com/'); echo $result;//http://t.cn/zyzbqau$result= Sinaurl::getlong (' Http://t.cn/zYzBqAU '); echo $result;//http://www.phpernote.com/

The above is to generate Sina short link and restore Sina Short link class (method) Summary, hope can help everyone.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.