Home  >  Article  >  Backend Development  >  Sina short link generation and restoration short link tutorial

Sina short link generation and restoration short link tutorial

小云云
小云云Original
2017-11-22 10:27:576845browse

Short link, in layman's terms, is to convert a long URL into a short URL string through program calculation and other methods. Regarding Sina short links, we can use the Sina Weibo short link generator, which is a small tool that can generate t.cn short links. But what if we implement Sina short links ourselves? In this article, we will teach you how to use php to generate Sina short links and restore Sina short link classes.

<?php
/*
* 生成新浪的短链接或还原新浪短链接
* date 2017年11月22日
* author www.phpernote.com
*/
class SinaUrl{
//新浪APPKEY
const APPKEY=&#39;31641035&#39;;
//CURL
private static function CURLQueryString($url){
//设置附加HTTP头
$addHead=array("Content-type: application/json");
//初始化curl,当然,你也可以用fsockopen代替
$curl_obj=curl_init();
//设置网址
curl_setopt($curl_obj,CURLOPT_URL,$url);
//附加Head内容
curl_setopt($curl_obj,CURLOPT_HTTPHEADER,$addHead);
//是否输出返回头信息
curl_setopt($curl_obj,CURLOPT_HEADER,0);
//将curl_exec的结果返回
curl_setopt($curl_obj,CURLOPT_RETURNTRANSFER,1);
//设置超时时间
curl_setopt($curl_obj,CURLOPT_TIMEOUT,8);
//执行
$result=curl_exec($curl_obj);
//关闭curl回话
curl_close($curl_obj);
return $result;
}
//处理返回结果
private static function doWithResult($result,$field){
$result=json_decode($result,true);
return isset($result[0][$field])?$result[0][$field]:&#39;&#39;;
}
//获取短链接
public static function getShort($url){
$url=&#39;http://api.t.sina.com.cn/short_url/shorten.json?source=&#39;.self::APPKEY.&#39;&url_long=&#39;.$url;
$result=self::CURLQueryString($url);
return self::doWithResult($result,&#39;url_short&#39;);
}
//获取长链接
public static function getLong($url){
$url=&#39;http://api.t.sina.com.cn/short_url/expand.json?source=&#39;.self::APPKEY.&#39;&url_short=&#39;.$url;
$result=self::CURLQueryString($url);
return self::doWithResult($result,&#39;url_long&#39;);
}
}

//使用示例,如下:

$result=SinaUrl::getShort(&#39;http://www.phpernote.com/&#39;);
echo $result;
//http://t.cn/zYzBqAU
$result=SinaUrl::getLong(&#39;http://t.cn/zYzBqAU&#39;);
echo $result;
//http://www.phpernote.com/

The above is a summary of the classes (methods) for generating Sina short links and restoring Sina short links. I hope it can help everyone.

Related recommendations:

PHP implements URL long connection generation short link operation

Link php short link algorithm collection and analysis

Weibo short link algorithm PHP version_PHP tutorial

The above is the detailed content of Sina short link generation and restoration short link tutorial. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn