2015-12-05 26 views
0

下面是我使用用於經由Bitly API縮短長的URL PHP代碼:如何發佈一個Bitlink縮短URL輸入值

<?php 
$bitly_access_token = 'my_api_key'; 
$deeplink = 'http://example.com/'; 

$curl = curl_init('https://api-ssl.bit.ly/v3/shorten?access_token='.$bitly_access_token.'&longUrl='.urlencode($deeplink)); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt($curl, CURLOPT_HEADER, 0); 
$return = json_decode(curl_exec($curl), true); 
curl_close($curl); 

print_r($return); 
?> 

上面的代碼的示例輸出:

Array [(status_code] => 200 [status_txt] => OK [data] => Array( [long_url] =>http://example.com [url] =>http://bit.ly/xxxxx [hash] => xxxxx [global_hash] => zzzzz [new_hash ] => 0))

它的工作原理和輸出縮短的網址爲:http://bit.ly/xxxxx樣本)。

但是,在PHP方面有一點經驗,我無法弄清楚如何將縮短的URL發佈到輸入值中。我試過<input type="text" value="<?php echo $return ?>" />,但沒有奏效。

此外,我有這個長輸出的問題,爲什麼它不顯示縮短的URL?

謝謝。

+1

僅打印縮短的URL使用echo $返回[ '鏈接']; – maxhb

+0

[在PHP中訪問關聯數組]可能的副本(http://stackoverflow.com/questions/3842111/accessing-associative-arrays-in-php) – cyfur01

回答

1

也許這:

<input type="text" value="<?php echo $return['data']['url'] ?>" /> 
+0

謝謝你,這個伎倆! –