如何用PHP调用新浪API生成基于长尾词的t.cn短网址链接?

2026-04-02 04:161阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1162个文字,预计阅读时间需要5分钟。

如何用PHP调用新浪API生成基于长尾词的t.cn短网址链接?

本文实例讲解了PHP通过调用新浪API生成t.cn格式短网址链接的方法。分享给广大用户和开发者参考,具体如下:

新浪提供了将长链接转换为短链接的API,用户可以通过该API将长链接生成形如`t.cn/xxx`的短链接。以下是使用PHP实现该功能的步骤:

1. 注册新浪API,获取API Key和API Secret Key。

2.在PHP代码中,使用以下代码调用新浪API:

php

// 长链接$longUrl='http://www.example.com';

// 构建请求参数$params=[ 'appkey'=> $apiKey, 'url'=> $longUrl, 'format'=> 'json' // 返回结果格式,可选值为json或xml];

// 对参数进行签名$sign=md5(urlencode($apiKey . $apiSecret . $longUrl));

// 构建请求URL$url=http://api.t.sina.com.cn/short_url/shorten.php? . http_build_query($params) . &sign= . $sign;

// 发送HTTP请求$ch=curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);$response=curl_exec($ch);curl_close($ch);

// 解析响应数据$data=json_decode($response, true);$shortUrl=$data['data']['short_url'];

// 输出短链接echo $shortUrl;?>

3. 运行PHP代码,即可将长链接生成短链接。

以上步骤展示了如何使用PHP调用新浪API生成t.cn格式短网址链接的方法。希望对您有所帮助。

本文实例讲述了PHP通过调用新浪API生成t.cn格式短网址链接的方法。分享给大家供大家参考,具体如下:

新浪提供了长链接转为短链接的API,可以把长链接转为 t.cn/xxx 这种格式的短链接。

API:

api.t.sina.com.cn/short_url/shorten.json (返回结果是JSON格式)
api.t.sina.com.cn/short_url/shorten.xml (返回结果是XML格式)

请求参数:

source 申请应用时分配的AppKey,调用接口时代表应用的唯一身份。
url_long 需要转换的长链接,需要URLencoded,最多不超过20个。
多个url参数需要使用如下方式请求:url_long=aaa&url_long=bbb

创建source方法

1.进入open.weibo.com/ ,选择菜单 微连接->网站接入。
2.点击立即接入,创建新应用,随便填写应用名称,点击创建。
3.创建成功后,AppKey就是source参数的值,可以用于请求创建短链接。

如何用PHP调用新浪API生成基于长尾词的t.cn短网址链接?

测试代码:

<?php $api = 'api.t.sina.com.cn/short_url/shorten.json'; // json // $api = 'api.t.sina.com.cn/short_url/shorten.xml'; // xml $source = '您申请的AppKey'; $url_long = 'www.jb51.net/'; $request_url = sprintf($api.'?source=%s&url_long=%s', $source, $url_long); $data = file_get_contents($request_url); echo $data; ?>

返回JSON格式

[ { "url_short": "t.cn/RBclsRo</url_short> <url_long>www.jb51.net/</url_long> <type>0</type> </url></urls>

生成的短链接为 t.cn/RBclsRo ,访问会跳转到 www.jb51.net/

完整调用方法如下:

<?php/** * 调用新浪接口将长链接转为短链接 * @param string $source 申请应用的AppKey * @param array|string $url_long 长链接,支持多个转换(需要先执行urlencode) * @return array */function getSinaShortUrl($source, $url_long){ // 参数检查 if(empty($source) || !$url_long){<br> return false; } // 参数处理,字符串转为数组 if(!is_array($url_long)){<br> $url_long = array($url_long); } // 拼接url_long参数请求格式 $url_param = array_map(function($value){ return '&url_long='.urlencode($value); }, $url_long);<br> $url_param = implode('', $url_param); // 新浪生成短链接接口 $api = 'api.t.sina.com.cn/short_url/shorten.json'; // 请求url $request_url = sprintf($api.'?source=%s%s', $source, $url_param); <br> $result = array(); // 执行请求 $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_URL, $request_url); <br>  $data = curl_exec($ch);<br> if($error=curl_errno($ch)){<br> return false; } curl_close($ch); $result = json_decode($data, true); return $result; } //AppKey <br>$source = '您申请的AppKey';<br>// 单个链接转换 $url_long = 'www.jb51.net/';<br>$data = getSinaShortUrl($source, $url_long); print_r($data);<br>// 多个链接转换 $url_long = array('www.jb51.net/','www.jb51.net/','www.jb51.net/'); $data = getSinaShortUrl($source, $url_long); print_r($data); ?>

输出:

Array(
[0] => Array
(
[url_short] => t.cn/RBclsRo
[url_long] => www.jb51.net/
[type] => 0
)
)Array(
[0] => Array
(
[url_short] => t.cn/RBclsRo
[url_long] => www.jb51.net/
[type] => 0
)
[1] => Array
(
[url_short] => t.cn/RBclsRo
[url_long] => www.jb51.net/
[type] => 0
)
[2] => Array
(
[url_short] => t.cn/RBclsRo
[url_long] => www.jb51.net/
[type] => 0
)
)

经测试,这个生成接口还是比较稳定的!

PS:这里为大家推荐一款本站短网址生成工具(也是使用的第三方API接口生成的短网址)

短链(短网址)在线生成工具:
tools.jb51.net/password/dwzcreate

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php curl用法总结》、《PHP网络编程技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《PHP数据结构与算法教程》及《PHP中json格式数据操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

本文共计1162个文字,预计阅读时间需要5分钟。

如何用PHP调用新浪API生成基于长尾词的t.cn短网址链接?

本文实例讲解了PHP通过调用新浪API生成t.cn格式短网址链接的方法。分享给广大用户和开发者参考,具体如下:

新浪提供了将长链接转换为短链接的API,用户可以通过该API将长链接生成形如`t.cn/xxx`的短链接。以下是使用PHP实现该功能的步骤:

1. 注册新浪API,获取API Key和API Secret Key。

2.在PHP代码中,使用以下代码调用新浪API:

php

// 长链接$longUrl='http://www.example.com';

// 构建请求参数$params=[ 'appkey'=> $apiKey, 'url'=> $longUrl, 'format'=> 'json' // 返回结果格式,可选值为json或xml];

// 对参数进行签名$sign=md5(urlencode($apiKey . $apiSecret . $longUrl));

// 构建请求URL$url=http://api.t.sina.com.cn/short_url/shorten.php? . http_build_query($params) . &sign= . $sign;

// 发送HTTP请求$ch=curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);$response=curl_exec($ch);curl_close($ch);

// 解析响应数据$data=json_decode($response, true);$shortUrl=$data['data']['short_url'];

// 输出短链接echo $shortUrl;?>

3. 运行PHP代码,即可将长链接生成短链接。

以上步骤展示了如何使用PHP调用新浪API生成t.cn格式短网址链接的方法。希望对您有所帮助。

本文实例讲述了PHP通过调用新浪API生成t.cn格式短网址链接的方法。分享给大家供大家参考,具体如下:

新浪提供了长链接转为短链接的API,可以把长链接转为 t.cn/xxx 这种格式的短链接。

API:

api.t.sina.com.cn/short_url/shorten.json (返回结果是JSON格式)
api.t.sina.com.cn/short_url/shorten.xml (返回结果是XML格式)

请求参数:

source 申请应用时分配的AppKey,调用接口时代表应用的唯一身份。
url_long 需要转换的长链接,需要URLencoded,最多不超过20个。
多个url参数需要使用如下方式请求:url_long=aaa&url_long=bbb

创建source方法

1.进入open.weibo.com/ ,选择菜单 微连接->网站接入。
2.点击立即接入,创建新应用,随便填写应用名称,点击创建。
3.创建成功后,AppKey就是source参数的值,可以用于请求创建短链接。

如何用PHP调用新浪API生成基于长尾词的t.cn短网址链接?

测试代码:

<?php $api = 'api.t.sina.com.cn/short_url/shorten.json'; // json // $api = 'api.t.sina.com.cn/short_url/shorten.xml'; // xml $source = '您申请的AppKey'; $url_long = 'www.jb51.net/'; $request_url = sprintf($api.'?source=%s&url_long=%s', $source, $url_long); $data = file_get_contents($request_url); echo $data; ?>

返回JSON格式

[ { "url_short": "t.cn/RBclsRo</url_short> <url_long>www.jb51.net/</url_long> <type>0</type> </url></urls>

生成的短链接为 t.cn/RBclsRo ,访问会跳转到 www.jb51.net/

完整调用方法如下:

<?php/** * 调用新浪接口将长链接转为短链接 * @param string $source 申请应用的AppKey * @param array|string $url_long 长链接,支持多个转换(需要先执行urlencode) * @return array */function getSinaShortUrl($source, $url_long){ // 参数检查 if(empty($source) || !$url_long){<br> return false; } // 参数处理,字符串转为数组 if(!is_array($url_long)){<br> $url_long = array($url_long); } // 拼接url_long参数请求格式 $url_param = array_map(function($value){ return '&url_long='.urlencode($value); }, $url_long);<br> $url_param = implode('', $url_param); // 新浪生成短链接接口 $api = 'api.t.sina.com.cn/short_url/shorten.json'; // 请求url $request_url = sprintf($api.'?source=%s%s', $source, $url_param); <br> $result = array(); // 执行请求 $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_URL, $request_url); <br>  $data = curl_exec($ch);<br> if($error=curl_errno($ch)){<br> return false; } curl_close($ch); $result = json_decode($data, true); return $result; } //AppKey <br>$source = '您申请的AppKey';<br>// 单个链接转换 $url_long = 'www.jb51.net/';<br>$data = getSinaShortUrl($source, $url_long); print_r($data);<br>// 多个链接转换 $url_long = array('www.jb51.net/','www.jb51.net/','www.jb51.net/'); $data = getSinaShortUrl($source, $url_long); print_r($data); ?>

输出:

Array(
[0] => Array
(
[url_short] => t.cn/RBclsRo
[url_long] => www.jb51.net/
[type] => 0
)
)Array(
[0] => Array
(
[url_short] => t.cn/RBclsRo
[url_long] => www.jb51.net/
[type] => 0
)
[1] => Array
(
[url_short] => t.cn/RBclsRo
[url_long] => www.jb51.net/
[type] => 0
)
[2] => Array
(
[url_short] => t.cn/RBclsRo
[url_long] => www.jb51.net/
[type] => 0
)
)

经测试,这个生成接口还是比较稳定的!

PS:这里为大家推荐一款本站短网址生成工具(也是使用的第三方API接口生成的短网址)

短链(短网址)在线生成工具:
tools.jb51.net/password/dwzcreate

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php curl用法总结》、《PHP网络编程技巧总结》、《PHP数组(Array)操作技巧大全》、《php字符串(string)用法总结》、《PHP数据结构与算法教程》及《PHP中json格式数据操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。