php - Bitly API 成功缩短 URL 但长 URL 错误
问题描述
我尝试缩短 URL 并使用此代码来缩短 URL,它可以缩短但长 URL 是错误的
<?php
/* make a URL small */
function make_bitly_url($url,$login,$appkey,$format = 'xml',$version = '2.0.1')
{
//create the URL
$bitly = 'http://api.bit.ly/shorten?version='.$version.'&longUrl='.urlencode($url).'&login='.$login.'&apiKey='.$appkey.'&format='.$format;
//get the url
//could also use cURL here
$response = file_get_contents($bitly);
//parse depending on desired format
if(strtolower($format) == 'json')
{
$json = @json_decode($response,true);
return $json['results'][$url]['shortUrl'];
}
else //xml
{
$xml = simplexml_load_string($response);
return 'http://bit.ly/'.$xml->results->nodeKeyVal->hash;
}
}
?>
打电话,我用这个代码:
<?php /* usage */
$short = make_bitly_url('http://test.com./sub/<?php echo $idplus;?>','user','API Key','json');
echo 'Bitly : '.$short;
?>
$idplus 是我输入 url 时得到的唯一值。当我使用此代码时,网址可以缩短但长网址http://test.com/sub/<?php echo $idplus;?>
不会http://test.com/sub/idplus
,请帮我解决这个问题,因为我对编码一无所知:(
为了弄清楚这种情况,当我访问一个页面时,test.com/test/123
我想用这个长 URL 创建一个短 URLabc.com/com/123
解决方案
对于调用使用这个:
<?php /* usage */
$url = 'http://test.com./sub/' . $idplus;
$short = make_bitly_url($url,'user','API Key','json');
echo 'Bitly : '.$short;
?>
推荐阅读
- node.js - 尽管正在定义 CSP script-src-elem 错误,但如何修复它?
- r - 错误:案例 1 (`.`) 必须是双边公式,而不是 `data.frame` 对象
- php - 无法让 Apache 加载 PHP 的 HomeBrew 版本
- pandas - 使用 matplotlib 条形图在 pandas MultiIndex 数据帧上仅堆叠一个条形
- javascript - 单击按钮时可以加载不同的脚本吗?
- c# - 将 DataTable 转换为 Excel 不包括标题单元格
- .net - Bootstrap 是否完全集成到 Blazor 并准备好“开箱即用”使用,还是我需要配置我的项目才能充分利用 Bootstrap?
- python - 绘制堆积条形图时的问题
- curl - 如何将 kubectl create -f file.yaml 转换为 curl
- arrays - 最大选择数组的第一个或最后一个元素