Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Bitly API成功缩短URL,但长URL错误_Php_Api - Fatal编程技术网

Php Bitly API成功缩短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

我试图缩短一个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;
    }
}

?>
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
这样的页面时,我想用这个长URL
abc.com/com/123
创建一个短URL,用于调用:

<?php /* usage */
$url = 'http://test.com./sub/' . $idplus;
$short = make_bitly_url($url,'user','API Key','json');
echo 'Bitly :  '.$short; 
?>