在实际过程中我们会有这样的场景,就是发送短信的时候,里面需要嵌入我们的网址,可是网址都是很长的。
如果你一般都是在手机上操作的话,可以在微信小程序中搜索:短链接的生成
或者在文章底部扫描小程序二维码进行使用
但是短信内容,最多只能七十个字左右,多余就会出现发送两条的情况,然而这并不是我们想要的。所以,基于这种需求我们急需将长链接转为短链接。常用的可以看到新浪微博的分享地址。下面来看,如何实现?
新浪提供了长链接转为短链接的API,可以把长链接转为/xxx
这种格式的短链接。
百度提供了长链接转为短链接的API,可以把长链接转为dwz
.cn/xxx
这种格式的短链接。
百度老接口将于近期停止服务,请使用新接口
百度老接口:/admin/create(短网址生成接口)百度新接口:/admin/v2/create(短网址生成接口)
百度API:(网址需要被百度收录)
<?php$host = '';$path = '/admin/v2/create';$url = $host . $path;$method = 'POST';$content_type = 'application/json';// TODO: 设置Token$token = '你的Token';// TODO:设置待注册长网址$bodys = array('url'=>'你的长网址'); // 配置headers $headers = array('Content-Type:'.$content_type, 'Token:'.$token);// 创建连接$curl = curl_init($url);curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);curl_setopt($curl, CURLOPT_FAILONERROR, false);curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);curl_setopt($curl, CURLOPT_HEADER, false);curl_setopt($curl, CURLOPT_POST, true);curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($bodys));// 发送请求$response = curl_exec($curl);curl_close($curl);// 读取响应var_dump($response);?>
响应结果示例
{"Code": 0,"ShortUrl": "/de3rp2Fl","LongUrl": "","ErrMsg": ""}
响应参数说明
新浪API: (暂时新浪已停用该服务)
http://api./short_url/shorten.json(返回结果是JSON格式)http://api./short_url/shorten.xml(返回结果是XML格式)
请求参数:
source 申请应用时分配的AppKey,调用接口时代表应用的唯一身份。
url_long 需要转换的长链接,需要URLencoded,最多不超过20个。
多个url参数需要使用如下方式请求:url_long=aaa&url_long=bbb
创建source方法
1.进入/ ,选择菜单 微连接->网站接入。
2.点击立即接入,创建新应用,随便填写应用名称,点击创建。
3.创建成功后,AppKey就是source参数的值,可以用于请求创建短链接。
<?phpnamespace App\Services;class ShortUrlService{/*** 调用新浪接口将长链接转为短链接* @param string $source 申请应用的AppKey* @param array|string $urlLong 长链接,支持多个转换(需要先执行urlencode)* @return array*/public static function getSinaShortUrl($source, $urlLong){// 参数检查if(empty($source) || !$urlLong){return false;}// 参数处理,字符串转为数组if(!is_array($urlLong)){$urlLong = array($urlLong);}// 拼接url_long参数请求格式$url_param = array_map(function($value){return '&url_long='.urlencode($value);}, $urlLong);$url_param = implode('', $url_param);// 新浪生成短链接接口$api = 'http://api./short_url/shorten.json';// 请求url$request_url = sprintf($api.'?source=%s%s', $source, $url_param);$result = array();// 执行请求$ch = curl_init();curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);curl_setopt($ch, CURLOPT_URL, $request_url);$data = curl_exec($ch);if($error=curl_errno($ch)){return false;}curl_close($ch);$result = json_decode($data, true);return $result;}}
AppKey以下是公用API,暂时可用。如失效,注册新浪开发者帐号即可
$source = config('app.sina');
单个链接转换
$urlLong = config('app.url');$shortUrl = ShortUrlService::getSinaShortUrl($source, $urlLong);
返回结果
array:1 [0 => array:3 ["url_short" => "/*******""url_long" => "http://***********.com/#/***********""type" => 0]]
多个链接转换
$urlLong = ['http://www.***.com/article/1.html','http://www.***.com/article/2.html','http://www.***.com/index.html'];$shortUrl = ShortUrlService::getSinaShortUrl($source, $urlLong);
返回结果
array:3 [0 => array:3 ["url_short" => "/RD12""url_long" => "http://www.***.com/article/1.html""type" => 0]1 => array:3 ["url_short" => "/RD134KV""url_long" => "http://www.***.com/article/2.html""type" => 0]2 => array:3 ["url_short" => "/RA8u231F""url_long" => "http://www.***.com/index.html""type" => 0]]
通过上面的方法,可以很轻松的实现用php生成短链接网址的功能。
项目源码中含有短连接生成的api接口:/WXiangQian/laravel-api