Php Bitly API成功缩短URL,但长URL错误
我试图缩短一个URL,并使用此代码来缩短一个URL,缩短是一项工作,但长URL是错误的Php Bitly API成功缩短URL,但长URL错误,php,api,Php,Api,我试图缩短一个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.'&long
<?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;
}
}
?>
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时获得的唯一值。当我使用此代码时,url可以缩短,但长url将http://test.com/sub/
非http://test.com/sub/idplus
,请帮助我解决此问题,因为我对编码一无所知:(
为了明确这种情况,当我访问像test.com/test/123
这样的页面时,我想用这个长URLabc.com/com/123
创建一个短URL,用于调用:
<?php /* usage */
$url = 'http://test.com./sub/' . $idplus;
$short = make_bitly_url($url,'user','API Key','json');
echo 'Bitly : '.$short;
?>